registerhotkey doesnt triggers for shift or control keys, but works for letter keys

StackOverflow https://stackoverflow.com/questions/20198721

  •  04-08-2022
  •  | 
  •  

문제

I have simple app which can execute any code by any hotkey with any letter, i mean:

C, Q, Ctrl-C, Alt-Q, Ctrl-Alt-Q

And this hotkey triggers whenever i press it, and in any form state (even minimized it tray). But if i need to register hotkey for single key like SHIFT or CONTROL (ctrl) - nope, doesn't works. Here is code:

 //at form load
 RegisterHotKey(this.Handle, 0, 0, (int)Keys.Shift); // doesnt works
 RegisterHotKey(this.Handle, 1, 0, (int)Keys.C); //works

 protected override void WndProc(ref Message m)
        {
            if (m.Msg == 0x0312)
            {
                int id = m.WParam.ToInt32();
                if (id < 2) //testing these two hotkeys
                {
                    MessageBox.Show("trigger");
                }
            }
            base.WndProc(ref m);
        }

I am really need to register global hotkey for single shift and single ctrl keys, but it seems i cant do it like this. How can i do this? I believe ProcessCmdKey works only when form is focused, so it will not work for me, however it will capture ctrl or shift or space keys.

도움이 되었습니까?

해결책

You can only use a modifier key in conjunction with another key. You cannot use a modifier key by itself. This is the same in every language that I have come across. The modifier key modifies the action of one or more further keys.

Even if it were possible, it would be unadvisable because if it were used on its own, then you would not be able to use that modifier key with any other key combinations as it would be swallowed up by the first action as soon as it was pressed.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top