Keyboard Input amp; the Win32 message loop(键盘输入Win32 消息循环)
问题描述
如何处理 Windows 消息循环中的按键和按键事件?我需要能够调用两个函数 OnKeyUp(char c);
和 OnKeyDown(char c);
.
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);
.
我从谷歌搜索中发现的当前文献让我对 WM_CHAR 或 WM_KEYUP 和 WM_KEYDOWN 感到困惑,并且通常针对 PDA 或托管代码,而我使用的是 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++.
推荐答案
使用char c = MapVirtualKey(param,MAPVK_VK_TO_CHAR);
将虚拟键码转换为char,并处理WM_KEYUP和WM_KEYDOWN及其wParams.
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.
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);
}
这篇关于键盘输入Win32 消息循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:键盘输入Win32 消息循环


基础教程推荐
- 如何“在 Finder 中显示"或“在资源管理器中显 2021-01-01
- 管理共享内存应该分配多少内存?(助推) 2022-12-07
- 在 C++ 中循环遍历所有 Lua 全局变量 2021-01-01
- 为什么语句不能出现在命名空间范围内? 2021-01-01
- 如何在不破坏 vtbl 的情况下做相当于 memset(this, ...) 的操作? 2022-01-01
- 从 std::cin 读取密码 2021-01-01
- 使用从字符串中提取的参数调用函数 2022-01-01
- Windows Media Foundation 录制音频 2021-01-01
- 如何使图像调整大小以在 Qt 中缩放? 2021-01-01
- 为 C/C++ 中的项目的 makefile 生成依赖项 2022-01-01