Global hotkey with WIN32 API?

13.9k Views Asked by At

I've been able to set local hotkeys like this

 RegisterHotKey(hwndDlg, 100, MOD_ALT | MOD_CONTROL, 'S');

How can I set the hotkey to be global? I want it to be there even when my window is hidden.

4

There are 4 best solutions below

1
Mars On BEST ANSWER

I solved it myself but thanks for your reply here's what was wrong...

ShowWindow(hwndDlg, SW_HIDE);
RegisterHotKey(hwndDlg, 100, MOD_ALT | MOD_CONTROL, 'S');

if you register the hotkey first then hide the window... it ignores the hotkey for some reason... oh well.. it's working now :)

1
Dan Byström On

It desn't matter if your window is visible or not. You should not use a hWnd you plan to destory (like a dialog). Create a separate (invisible) window if you have no other suitable window.

0
Yan Sultanov On

http://msdn.microsoft.com/ru-RU/library/windows/desktop/ms646309(v=vs.85).aspx

hWnd [in, optional]

Type: HWND

<...> If this parameter is NULL, WM_HOTKEY messages are posted to the message queue of the calling thread and must be processed in the message loop.

That is a better way for registering global hotkeys.

0
Michael Haephrati On

First you define one or more constants for your hotkeys

#define HOTKEY1 1000
#define HOTKEY2 1002

Then you register these hot keys

RegisterHotKey(NULL, HOTKEY1, MOD_ALT + MOD_SHIFT, 0x53); // ALT+SHIFT+s
RegisterHotKey(NULL, HOTKEY2, MOD_ALT + MOD_SHIFT, 0x51); // ALT+SHIFT+q

Finally in the main event loop you monitor these hot keys and respond to them:

if (msg.message == HOTKEY1) { switch (LOWORD(msg.wParam)) { case HOTKEY1: // do such and such break; case HOTKEY2: // do such and such break; } }