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.
RegisterHotkey (Handle, 1, MOD_CONTROL, 0) works perfectly
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With