Register hotkeys in .NET - combination of three/four keys

后端 未结 2 1089
抹茶落季
抹茶落季 2020-12-20 08:22

I got stuck.

Right now, I am using the following code to listen to hotkeys:

    [DllImport(\"user32.dll\")]
    public static extern bool RegisterHot         


        
相关标签:
2条回答
  • 2020-12-20 09:13

    From the documentation for WM_HOTKEY:

    lParam The low-order word specifies the keys that were to be pressed in combination with the key specified by the high-order word to generate the WM_HOTKEY message. This word can be one or more of the following values. The high-order word specifies the virtual key code of the hot key.

    So you can read the LParam member of m to determine the keys that were pressed (alternatively, if you assign more sensible identifiers than GetHashCode you can check WParam).

    The 'high-order word' and 'low-order word' refer to parts of the integer (actually an IntPtr) contained in LParam, so you will need to extract these. The low-order word is i & 0xFFFF, while the high-order word is (i >> 16) & 0xFFFF.

    To detect which key combination was pressed, check the lowest four bits of the low-order word for the modifiers (shift, alt, control) and compare the high-order word against the virtual key code - which for letters is equal to the character value of the capital (for example, the virtual key code for A is (int)'A', but not (int)'a').

    Your 'A+B+C+D' combination is not valid, since WM_HOTKEY hotkeys only support a single character. You will need to attach a keyboard hook to detect that combination from anywhere (or handle messages if you only want to detect it while your application is active).

    0 讨论(0)
  • 2020-12-20 09:14

    I found the answer. Instead of using registerhotkey, I used KeyState and it solved all my problems. If anyone is interested, you can go here (backup on archive.org)

    0 讨论(0)
提交回复
热议问题