简体   繁体   English

WIN32 API的全球热键?

[英]Global hotkey with WIN32 API?

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. 即使我的窗户被隐藏,我希望它在那里。

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 :) 如果你先注册热键,然后隐藏窗口......由于某种原因,它忽略了热键...哦......它现在正在工作:)

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

hWnd [in, optional] hWnd [in,optional]

Type: HWND 类型: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. <...>如果此参数为NULL,则WM_HOTKEY消息将发布到调用线程的消息队列中,并且必须在消息循环中处理。

That is a better way for registering global hotkeys. 这是注册全局热键的更好方法。

It desn't matter if your window is visible or not. 你的窗户是否可见是无关紧要的。 You should not use a hWnd you plan to destory (like a dialog). 你不应该使用你计划破坏的hWnd(如对话框)。 Create a separate (invisible) window if you have no other suitable window. 如果没有其他合适的窗口,请创建一个单独的(不可见)窗口。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM