Low level keyboard input from Windows

后端 未结 5 1298
时光取名叫无心
时光取名叫无心 2021-01-06 01:19

What win32 calls can be used to detect key press events globally (not just for 1 window, I\'d like to get a message EVERY time a key is pressed), from a windows service?

5条回答
  •  北海茫月
    2021-01-06 02:11

    Just use the native function GetKeyState, or GetAsyncKeyState, or GetKeyboardState from user32 DLL.

    You can read about each function I mentioned above in the msdn site:

    h ttp://msdn.microsoft.com/en-us/library/windows/desktop/ms646301(v=vs.85).aspx

    for the GetKeyState, and

    h ttp://msdn.microsoft.com/en-us/library/windows/desktop/ms646293(v=vs.85).aspx

    for the GetAsyncKeyState, and

    h ttp://msdn.microsoft.com/en-us/library/windows/desktop/ms646299(v=vs.85).aspx

    for the GetKeyboardState.

    If you program in C# then:

    Your code must include the following line out of any class, struct or enum declaration:

    using System.Runtime.InteropServices;
    

    Then in the class that has the method where you want to detect the keys, define one of three functions above for your choice, or what works and what not.

    [DllImport("user32.dll")]
    static uint GetKeyState(byte virtualKeyCode);
    
    //or
    
    [DllImport("user32.dll")]
    static uint GetAsyncKeyState(byte virtualKeyCode);
    
    //or
    
    [DllImport("user32.dll")]
    static uint GetKeyboardState(byte[] virtualKeyCodes);
    //NOTE: The length of the byte array parameter must be always 256 bytes!
    

    If you dislike uint as return type, you can also change it to int.

    If the native functions always return 1 as true or 0 as false, then you can change their return type to bool instead, but if you do so, then I think you should add another line of code above the native function definition, which is:

    [MarshalAs(UnmanagedType.Bool)]
    

    OR you can add the following line in the middle of the native function definition (below the line where the DllImport word is, and above the line, where the 'extern' word is):

    [return: MarshalAs(UnmanagedType.Bool)]
    

    Pinvoke site offers better definition for these functions, so you don't have to use integers to mention a particular key of your keyboard, but more understandable enum.

    The following link leads to the definition of the GetKeyState in C#:

    h ttp://www.pinvoke.net/default.aspx/user32.getkeystate

    The following link leads to the definition of the GetAsyncKeyState in C#

    h ttp://www.pinvoke.net/default.aspx/user32.getasynckeystate

    The following link leads to the definition of the GetKeyboardState in C#

    h ttp://www.pinvoke.net/default.aspx/user32.getkeyboardstate

    To detect which key is held down (at any time, anywhere) then call GetKeyState or GetAsyncKeyState function, in the single parameter, mention the key you want to detect and then add: & 0x8000, or & 0x80 after the call. If the mentioned key is held down, then the return value of the expression is not zero, but otherwise it is zero. According to the returned integer of the expression, you can determine which key held down or not. Note that if you put code inside if statement with that expression != 0 as enter condition, which is inside a loop, the code in that if statement will be executed more than once, actually many times, when you held down that key. If you want some code to be executed once When you press a key and then release it, then the following code is a trick that achieves this goal:

    while (boolean exression that will return false when you want to quit this loop)
    {
        if (boolean expression that will return true when particular key is **held down**) //This boolean expression calls either GetKeyState or GetAsyncKeyState with the & 0x8000 or & 0x80 after the call, and then != 0 to check for **held down** key
           while (true) //It's purpose is to wait for the same key that was held down to be released. After code execution, it will encounter the break keyword that will finish him, even though that his enter condition is true.
               if (boolean expression that will return true when the **same** particular key is **NOT** held down! (NOT held down, means that at that time, that key was released). You can copy the same condition from the first if statement, and just change **!=**, which is next to 0, to **==**, or instead add brackets to the condition '(' and ')', and in left of '(' add '!').
               {
                   //Put here the code that you want to execute once, when paticular key was pressed and then released.
                   break; //the while (true), which is the only way to get out of it
               }
    }
    

提交回复
热议问题