我正在编写一个 Windows 程序,我需要注册一个热键。但是,我担心 RegisterHotKey()
引起的冲突。
此外,当您触发热键消息时,按键事件会被阻止。例如,您调用 RegisterHotkey(0, 0, MOD_NOREPEAT | MOD_ALT, VK_UP)
来注册热键 ALT + ↑。如果在 explorer.exe 中按 ALT + ↑,explorer.exe 将不会收到此消息(原来是 ALT + ↑ 在 explorer.exe 中上一级)。
有没有办法不冲突不阻塞地注册热键?
RegisterHotKey
的 MSDN 页面说:
RegisterHotKey
fails if the keystrokes specified for the hot key have
already been registered by another hot key.
连同:
Return value
Type: BOOL
If the function succeeds, the return value is nonzero.
If the function fails, the return value is zero. To get extended error
information, call GetLastError
.
换句话说,if(!RegisterHotKey(...)) ...处理错误...
会捕获各种形式的“无效”,包括热键是already in use - 你需要查看 GetLastError
来区分“key already in use”和其他。
我认为注册热键的全部意义在于,无论发生什么,您的应用程序总能获得该按键。如果它恰好是某些其他应用程序中的流行键,那当然有点烦人,但这仍然是注册热键的要点。如果您不喜欢这种效果,则不要注册热键,或使用不太可能被其他应用程序使用的更隐蔽的组合。不确定如何解决这个问题 - 如果您将 IE 与您的应用程序一起运行并按下 ALT-uparrow,您究竟希望系统做什么?