none
getting key state without useing system.windows.input.keyboard? RRS feed

  • Question

  • The reson why I don't want to use System.Windows.Input.Keyboard is becuse I get the System.InvalidOperationException: 'The calling thread must be STA, because many UI components require this.'. I don't know if this problem accures because of the fact that I'm trying to use IskeyPressed in an event or because my program is technicly not a windows forms application as this seems to be used for. (meaning that the thread trying to use this, is not the same thread who runs my windows form)

    Anyhow I'm looking for an ortherway to get if a key is pressed, and I can't seem to find any?

    Wednesday, April 3, 2019 7:29 PM

All replies

  • GetKeyState

    [DllImport("User32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
          public static extern short GetKeyState(int nVirtKey);

    Wednesday, April 3, 2019 7:41 PM
  • Wednesday, April 3, 2019 7:58 PM
  • It works great put after playing around with it this exception came I'm not sure if its anything with getkeysate to do but I'm sure its not nessearyly a good thing seems its crying over the sta thing again :P

    The application is in break mode (what ever that means?)

    Managed Debugging Assistant 'ContextSwitchDeadlock' : 'The CLR has been unable to transition from COM context 0x770a30d0 to COM context 0x770a2fa8 for 60 seconds. The thread that owns the destination context/apartment is most likely either doing a non pumping wait or processing a very long running operation without pumping Windows messages. This situation generally has a negative performance impact and may even lead to the application becoming non responsive or memory usage accumulating continually over time. To avoid this problem, all single threaded apartment (STA) threads should use pumping wait primitives (such as CoWaitForMultipleHandles) and routinely pump messages during long running operations.'

    Wednesday, April 3, 2019 8:39 PM
  • Hi tacti tacoz,

    For your question, when you get this error? What is the message of this error?

    If you do not get it, do the following steps.

    VS 2017 Menu> Debug -> Options -> Debugging -> General -> Uncheck Enable Just My Code Options

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, April 8, 2019 7:29 AM
    Moderator
  • Well I've said the error I'm getting?
    ContextSwitchDeadlock?
    Thursday, April 11, 2019 9:09 AM
  • Hi tacti tacoz,

    Based on my search, the link below would be helpful.

    https://stackoverflow.com/questions/578357/visual-studio-contextswitchdeadlock

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, April 15, 2019 7:51 AM
    Moderator