Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
547 views
in Technique[技术] by (71.8m points)

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

I got stuck.

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

    [DllImport("user32.dll")]
    public static extern bool RegisterHotKey(IntPtr hWnd,
      int id, int fsModifiers, int vlc);
    [DllImport("user32.dll")]
    public static extern bool UnregisterHotKey(IntPtr hWnd, int id);


    protected override void WndProc(ref Message m)
    {
        if (m.Msg == 0x0312)
        {
            // whatever i need
        }
        base.WndProc(ref m);
    }

and this function to register hotkey:

Form1.RegisterHotKey(this.Handle, this.GetType().GetHashCode(), 0, (int)chr);

it works perfectly. my question is how do I register multiple hotkeys as the same combination, for example:

  1. A+B+C+D
  2. ALT+SHIFT+B
  3. CTRL+ALT+SHIFT+X

edit: I found out (like Zooba said) how to "decrypt" which hotkey was sent and here's the solution:

    protected override void WndProc(ref Message m)
    {
        if (m.Msg == 0x0312)
        {
            Keys key = (Keys)(((int)m.LParam >> 16) & 0xFFFF);
            ModifierKeys modifier = (ModifierKeys)((int)m.LParam & 0xFFFF);
            if ((modifier + "+" + key == "Alt+S"))
            {
                //do what ever I need.
            }
        }
        base.WndProc(ref m);
    }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...