Search code examples
windowskeyboardfocushotkeysdirectinput

Catching "global hotkeys" (Windows)


Now I've been trying to create a similar functionality as there is in Push-To-Talk voice chat applications, but so far I couldn't find any fitting solutions to this. I am not using MFC or CLR.

The problem is quite simple. My window should be usually out of focus (ie minimized etc), but I need to react to keypresses (basically, I don't even want to know if the button is being held down or not). Unfortunately, WM_KEYDOWN only works if the window has keyboard focus. I do know that for example Teamspeak uses DirectInput for this, but I'm also aware that it can definitely be done without it as well, which I'd highly prefer.

The only other solution that I could make work is polling with GetAsyncKeyState, but it looks like this is far from being a good solution either. If at all possible, I'd still prefer using Windows messages.


Solution

  • The problem can be solved either with RegisterHotKey or with a global low-level keyboard hook.

    RegisterHotKey (which Cody Gray suggested in the commends) is probably the more suitable choice here.