17

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.

Mars
  • 4,197
  • 11
  • 39
  • 63
  • 3
    I've been writing Windows apps for nearly 20 years and I didn't know this API call existed - so thanks, and +1. :) – Rob Nov 30 '09 at 21:46

4 Answers4

11

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 :)

Magnus Hoff
  • 21,529
  • 9
  • 63
  • 82
Mars
  • 4,197
  • 11
  • 39
  • 63
8

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.

Community
  • 1
  • 1
Yan Sultanov
  • 146
  • 1
  • 3
4

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.

Dan Byström
  • 9,067
  • 5
  • 38
  • 68
4

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; } }

Michael Haephrati
  • 3,660
  • 1
  • 33
  • 56