用着wxWidgets写界面一直很爽,偶然想给自己的程序加个快捷键用于隐藏界面,结果就和这RegisterHotKey发生了这不得不说的故事。
先看文档的原文
wxWindow::RegisterHotKey
bool RegisterHotKey(int hotkeyId, int modifiers, int virtualKeyCode)
Registers a system wide hotkey. Every time the user presses the hotkey registered here, this window will receive a hotkey event. It will receive the event even if the application is in the background and does not have the input focus because the user is working with some other application.
Parameters
hotkeyId
- Numeric identifier of the hotkey. For applications this must be between 0 and 0xBFFF. If this function is called from a shared DLL, it must be a system wide unique identifier between 0xC000 and 0xFFFF. This is a MSW specific detail.
modifiers
- A bitwise combination of wxMOD_SHIFT, wxMOD_CONTROL, wxMOD_ALT or wxMOD_WIN specifying the modifier keys that have to be pressed along with the key.
virtualKeyCode
- The virtual key code of the hotkey.
Return value
true if the hotkey was registered successfully. false if some other application already registered a hotkey with this modifier/virtualKeyCode combination.
Remarks
Use EVT_HOTKEY(hotkeyId, fnc) in the event table to capture the event. This function is currently only implemented under Windows. It is used in the Windows CE port for detecting hardware button presses.
不得不说的就是virtualKeyCode 了,用惯了wx都知道WXK_*这一系列的值,平时注册快捷键时用个A啊F啊什么的也没感觉有什么不妥,但是到了F1~F12就遇到问题了,用WXK_F1就不灵,这是为什么捏?
最终还是在WIKI上找到了答案原来这个virtualKeyCode 要用winuser.h里面定义的VK_*那堆宏……当时吐血……
RegisterHotKey
Eric Jensen writes on wx-users::
i was playing around with wxWindow::RegisterHotKey() and noticed that the virtualKeyCode parameter that this function uses, expects the Window keycodes and not the wxWidgets keycodes (e.g VK_F9 rather than WXK_F9). Although this was not difficult to find, it should be mentioned in the documetation. However, if this function ever gets implemented on other platforms, using the 'normalized' wxWidgets keycode might become necessary.
译文
Eric Jensen致广大wx用户:
我最近和wxWindow::RegisterHotKey() 打交道发现virtualKeyCode 参数需求是的Window keycodes而不是wxWidgets keycodes (例如 用F9时,需要VK_F9而不是WXK_F9)。
虽然这不难察觉,但是也该在文档中提及吧。不管怎么着,当这个函数被移植到其他平台的时候应使用正常的wxWidgets keycodes 就是必须的了吧。