Search code examples
c#multithreadingdirectinput

Retrieving keyboard state in C# using DirectInput?


I need to retrieve the keyboard state when a key is pressed on a particular form rather quickly - fast enough that the standard KeyUp/KeyDown events for windows forms are not fast enough. I was told to try out DirectInput, but I have no experience working with the library or any threading experience, so I am unsure of how to start at all.

How would I set up a thread which called a function on the form back each time DirectInput picks up changes in keyboard state? I know how to initialize the device and that SetEventNotification is pretty much what I want, but I do not know how to use it.

Any help would be greatly appreciated!


Solution

  • This may help you as in not having to use DirectInput:

    [DllImport("user32.dll")]
    private static 
    extern short GetAsyncKeyState(System.Int32 vKey);
    

    http://msdn.microsoft.com/en-us/library/ms646293(v=vs.85).aspx

    hope it helps