views:

268

answers:

1

How would I capture the user pressing CTRL twice (CTRL + CTRL) globally. I want to be able to have my application window hidden and then make it visible when the user invokes it with the CTRL+CTRL key presses similar to Google Quick Search Box. The user may have focus on a different window. I've looked at RegisterHotKey, but that seems to be for MODIFIERS + character key unless I'm mistaken.

+2  A: 

To create such a hotkey, do this:

ATOM hotkey = GlobalAddAtom("Your hotkey atom name");
if(hotkey) RegisterHotKey(hwnd, hotkey, MOD_CONTROL, VK_CONTROL);
else { ...error... }

And then handle the WM_HOTKEY message:

case WM_HOTKEY:
    if(wParam == hotkey)
    {
       // CTRL pressed!!!
    }
break;

I guess you'll figure out yourself that you need to store whether the CTRL key was pressed before. For example, if it was pressed once in the last 500 ms, and the user presses it again, you have a CTRL+CTRL press.

AndiDog
Where does key debouncing take place?
Thomas Matthews
Better to use GetDoubleClickTime (http://msdn.microsoft.com/en-us/library/ms646258(VS.85).aspx) than hard-code in 500.
Bill
@Thomas Matthews: I don't quite see what you mean, we only need the pressed-down event here, which is `WM_HOTKEY`.
AndiDog
@Bill: Yes, that's worth considering. Good idea ;)
AndiDog
What's the difference between these 2 calls?RegisterHotKey(hwnd, hotkey, MOD_CONTROL, 0); RegisterHotKey(hwnd, hotkey, MOD_CONTROL, VK_CONTROL);From what I've seen, I get repeated WM_HOTKEY message when the Control key is held down or if pressed in combination with another key (e.g. CTRL+space) in the 2nd case. In the 1st case, I only get WM_HOTKEY messages when the control is pressed and released without any other key combination.
simplecoder
My solution will trigger a message when you press CTRL. If you press another key after that, like together (= CTRL+Space), it will send another message. And of course you get repeated messages when you hold down the CTRL key - just like any other key it gets repeated as defined in the control panel settings for the keyboard.
AndiDog