C++ 键盘输入和 Win32 消息循环
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2441457/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Keyboard Input & the Win32 message loop
提问by Tom J Nowell
How do I handle key presses and key up events in the windows message loop? I need to be able to call two functions OnKeyUp(char c);
and OnKeyDown(char c);
.
如何处理 Windows 消息循环中的按键和按键事件?我需要能够调用两个函数OnKeyUp(char c);
和OnKeyDown(char c);
.
Current literature I've found from googling has lead me to confusion over WM_CHAR or WM_KEYUP and WM_KEYDOWN, and is normally targeted at PDA or Managed code, whereas I'm using C++.
我从谷歌搜索中发现的当前文献使我对 WM_CHAR 或 WM_KEYUP 和 WM_KEYDOWN 感到困惑,并且通常针对 PDA 或托管代码,而我使用的是 C++。
回答by John Knoeller
A typical C++ message loop looks like this
典型的 C++ 消息循环如下所示
MSG msg;
while (GetMessage(&msg, null, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
The function of TranslateMessage is to generate WM_CHAR messages from WM_KEYDOWN messages, so if you want to see WM_CHAR messages you need to be sure to pass WM_KEYDOWN messages to it. If you don't care about WM_CHAR messages, you can skip that, and do something like this.
TranslateMessage 的作用是从 WM_KEYDOWN 消息中生成 WM_CHAR 消息,所以如果你想看到 WM_CHAR 消息,你需要确保将 WM_KEYDOWN 消息传递给它。如果您不关心 WM_CHAR 消息,您可以跳过它,并执行类似的操作。
extern void OnKeyDown(WPARAM key);
extern void OnKeyUp(WPARAM key);
MSG msg;
while (GetMessage(&msg, null, 0, 0))
{
if (msg.message == WM_KEYDOWN)
OnKeyDown (msg.wParam);
else if (msg.message == WM_KEYUP)
OnKeyUp(msg.wParam);
else
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
Notice that OnKeyDown and OnKeyUp messages are defined as taking a WPARAM rather than a char. That's because the values for WM_KEYDOWN and WM_KEYUP aren't limited to values that fit in a char. See WM_KEYDOWN
请注意, OnKeyDown 和 OnKeyUp 消息被定义为采用 WPARAM 而不是字符。这是因为 WM_KEYDOWN 和 WM_KEYUP 的值不限于适合字符的值。见WM_KEYDOWN
More:
Using Messages and Message Queues
https://docs.microsoft.com/en-us/windows/win32/winmsg/using-messages-and-message-queues
更多:
使用消息和消息队列
https://docs.microsoft.com/en-us/windows/win32/winmsg/using-messages-and-message-queues
回答by Tom J Nowell
Use char c = MapVirtualKey(param,MAPVK_VK_TO_CHAR);
to convert virtual key codes to char, and process WM_KEYUP and WM_KEYDOWN and their wParams.
使用char c = MapVirtualKey(param,MAPVK_VK_TO_CHAR);
虚拟键代码转换为字符,并处理WM_KEYUP和WM_KEYDOWN及其wParams。
if (PeekMessage (&mssg, hwnd, 0, 0, PM_REMOVE))
{
switch (mssg.message)
{
case WM_QUIT:
PostQuitMessage (0);
notdone = false;
quit = true;
break;
case WM_KEYDOWN:
WPARAM param = mssg.wParam;
char c = MapVirtualKey (param, MAPVK_VK_TO_CHAR);
this->p->Input ()->Keyboard ()->Listeners ()->OnKeyDown (c);
break;
case WM_KEYUP:
WPARAM param = mssg.wParam;
char c = MapVirtualKey (param, MAPVK_VK_TO_CHAR);
this->p->Input ()->Keyboard ()->Listeners ()->OnKeyUp (c);
break;
}
// dispatch the message
TranslateMessage (&mssg);
DispatchMessage (&mssg);
}