Global hotkey with WIN32 API?
Asked Answered
R

4

18

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.

Reaper answered 30/11, 2009 at 16:24 Comment(1)
I've been writing Windows apps for nearly 20 years and I didn't know this API call existed - so thanks, and +1. :)Unsex
R
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 :)

Reaper answered 30/11, 2009 at 16:33 Comment(1)
Cool. So it's guaranteed to be global?Apotheosize
B
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.

Bogusz answered 5/2, 2013 at 7:45 Comment(0)
L
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.

Lajoie answered 30/11, 2009 at 16:27 Comment(1)
I don't destroy it I just hide it like this ShowWindow(hwndDlg, SW_HIDE);Reaper
B
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; } }

Bromal answered 8/7, 2017 at 12:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.