質問

I need to handle keys combination (CTRL+SHIFT+UP) in WinAPI even if window is not focused/active.

How can I do this (if possible, I'd prefer solution without using WinApi Hooks)?

役に立ちましたか?

解決

First, you need to register a "system-wide" hot key with RegisterHotKey function. It works even if your application is minimized, not focused, or hidden.

RegisterHotKey(hWnd, KEY_ID, MOD_CONTROL | MOD_SHIFT, VK_UP);
// check for errors, the function will fail if the hot key is already registered

hWnd is the handle of the window that will receive WM_HOTKEY message. KEY_ID is the identifier for the hot key. MOD_CONTROL | MOD_SHIFT for both Ctrl+Shift. VK_UP for the Up arrow key.

Second, you need to handle the WM_HOTKEY message in your window procedure.

LRESULT CALLBACK WndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
    switch (uMsg) {
        ...
        WM_HOTKEY:
        // handle the hot key here
        ...
    }
}

If your application does not have a window and hWnd is NULL, then you need to handle the WM_HOTKEY in your message loop.

MSG msg = {0};
while (GetMessage(&msg, NULL, 0, 0) != 0)
{
    if (msg.message == WM_HOTKEY) {
        // handle the hot key here
    }
} 
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top