1

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);
    }
Ron
  • 3,975
  • 17
  • 80
  • 130
  • Why not be a bit more ambitious and go for 5 or 6 key combinations? – David Heffernan Jan 20 '11 at 21:18
  • possible duplicate of [Register more than one hotkey with RegisterHotKey](http://stackoverflow.com/questions/4704134/register-more-than-one-hotkey-with-registerhotkey) – Hans Passant Jan 20 '11 at 21:38
  • @David Heffernan, I would go for 5 or 6 key combination but windows doesnt allow to click on more than 4 keys at the same time :P. anyway, I just want to cover all the options... – Ron Jan 20 '11 at 21:56
  • @Ron Windows is just so lame!!! ;-) – David Heffernan Jan 20 '11 at 21:57
  • 2
    @David Heffernan, @Ron Pretty sure this limitation of the hardware not a windows issue ;) – MerickOWA Jan 20 '11 at 22:10
  • @David Heffernan, Indeed windows is lame but what can I do? most of the games support only windows and I am a gamer.. @MerickOWA, I dont really care since no one will use more than 4 keys as combination (its hard to press). – Ron Jan 20 '11 at 22:11
  • @Ron Actually I was being sarcastic! For what it's worth I think Windows is awesome. – David Heffernan Jan 20 '11 at 22:14
  • @David Heffernan, But I wasnt sarcastic.. linux is way much better than windows, but as I said, most of the games support only windows. Lets not continue this conversation because its not related to my question, thank you and sorry. – Ron Jan 20 '11 at 22:16
  • 1
    You still have that `GetHashCode` nonsense in there. I already explained in the question Hans linked why that's wrong. – CodesInChaos Jan 23 '11 at 15:20

2 Answers2

5

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

Zooba
  • 11,221
  • 3
  • 37
  • 40
  • if I need to attach a keyboard hook to detect combination that has more than 1 character then I have no use in this "hooker" at all.. I already got keyboard hooker which I wanted to abandon because it slowed down my program when I tried to close it. anyway, thank you for your explanation. – Ron Jan 20 '11 at 20:55
  • 1
    how do you know how to extract the low and high order words respectively using those bitwise operators? want to learn more, thanks. – User 12345678 Jan 15 '14 at 04:12
0

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)

GitaarLAB
  • 14,536
  • 11
  • 60
  • 80
Ron
  • 3,975
  • 17
  • 80
  • 130