在MFC应用程序中设置全局热键的正确方法是什么?
我知道如何使用加速器表在我的应用程序中设置热键。但是,我如何在全球范围内获得热键呢?
即我的应用程序正在运行,但最小化。但是我仍然希望它检测并处理一个热键。
I know how to use the accelerator table to setup hot keys within my app. But how do I get a hot key at global level?
I.e. My app is running, but minimised. Yet I still want it to detect and process a hot key.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您需要一个系统范围的热键,则应使用
registerHotkey
,传递窗口手柄的窗口过程负责处理事件。在应用程序级别上,这通常是cframewnd
/cframewndex
衍生的窗口实现。一旦注册了热键,接收窗口就可以在其自定义
cwnd :: onhotkey
覆盖。确保添加on_wm_hotkey(on_wm_hotkey( )
消息处理程序到接收窗口实现的消息图。
对于标准,默认生成的SDI/MDI应用程序,您需要应用以下更改。
mainfrm.h:
mainfrm.cpp:
请注意,接收
wm_hotkey
消息授予接收线前景激活许可。如果您希望您的应用程序在收到注册的热键后进入前景,则可以使用以下onHotkey
实现:无需刷新任务栏按钮,一切都按预期工作。
If you need a system-wide hotkey then you should use
RegisterHotKey
, passing in the window handle whose window procedure is responsible for handling the event. At the application level this is commonly theCFrameWnd
/CFrameWndEx
-derived window implementation.Once the hotkey is registered, the receiving window can observe the event in its custom
CWnd::OnHotKey
override. Make sure to add theON_WM_HOTKEY()
message handler to the message map of the receiving window implementation.For a standard, default-generated SDI/MDI application you'd need to apply the following changes.
MainFrm.h:
MainFrm.cpp:
Note that receiving a
WM_HOTKEY
message grants the receiving thread foreground activation permission. If you want your application to come to the foreground upon receiving a registered hot key, you can use the followingOnHotKey
implementation:No flashing the taskbar button, everything just works as intended.