Win32 WM_KEYDOWN和WM_KEYUP以及状态越来越"stuck"
Win32 WM_KEYDOWN and WM_KEYUP and statuses getting "stuck"
下面是我用来捕获键被按下或未按下的代码,并根据它们的状态更新键的状态。我将雕像保存在值为0,1,2,3的简单数组中。格式为:keyboardmap[256] = {0};
问题是,无论我怎么做,钥匙总是卡在某个点上。它们永远不会被重置回零,就好像WM_KEYUP不能正常启动一样。
while (true)
{
if ( PeekMessage(&msg, 0, 0, 0, PM_REMOVE) )
{
TranslateMessage(&msg);
DispatchMessage(&msg);
if (msg.message == WM_QUIT)
{
break;
}
// Check for keystates and update them.
if (msg.message == WM_KEYDOWN)
{
// Fetch the key state.
unsigned int keycode = msg.wParam;
unsigned int cstate = engine.GetKeyState(msg.wParam);
if (engine.GetKeyState(keycode) == 0)
{
engine.SetKeyState(keycode, 1); // Just started pressing.
}
else
{
engine.SetKeyState(keycode, 2); // Actively pressed down.
}
}
else if (msg.message == WM_KEYUP)
{
// Fetch the key state.
unsigned int keycode = msg.wParam;
unsigned int cstate = engine.GetKeyState(msg.wParam);
if ( engine.GetKeyState(keycode) == 2)
{
engine.SetKeyState(keycode, 3);
}
else
{
engine.SetKeyState(keycode, 0);
}
}
}
}
消息循环不应该是这样的。使用下面的游戏引擎的例子,你需要不断更新游戏/屏幕:
WNDCLASSEX wc = { sizeof(WNDCLASSEX) };
wc.lpfnWndProc = WndProc;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.lpszClassName = L"WindowClass";
RegisterClassEx(&wc);
CreateWindow(...);
MSG msg = { 0 };
//while (msg.message != WM_QUIT) <=== removed in edit
while(true) //<=== **** edit ****
{
if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
if(msg.message == WM_QUIT) //<=== **** edit ****
break;
TranslateMessage(&msg);
DispatchMessage(&msg);
}
else
{
engine.update();
}
}
窗口的消息应该在单独的窗口过程中处理:
LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch (msg)
{
case WM_KEYDOWN:
break;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hWnd, msg, wParam, lParam);
}
相关文章:
- Constexpr替代了新的放置方式,可以让内存中的对象保持未初始化状态
- 我不断收到 [错误] ID 返回 1 退出状态错误,但看不到问题所在
- OSX MetalKit CVMetalTextureCacheCreateTextureFromImage失败,状态:
- std::future_error:无关联状态
- 如何避免LED在循环状态变化中闪烁?
- boost 是否有按特殊类型值编码状态"compact optional"?
- 为什么系统函数总是在C++中返回已转移的退出状态?
- C++ 中的编译错误:未定义对"主"的引用 collect2:错误:ld 返回 1 个退出状态
- 当可输入框在窗口中处于活动状态时获得通知的任何方法
- 检查两个节点在子节点上是否具有相同状态的更优雅的方法
- 将有状态的 lambda 传递到 C 样式函数中,而无需上下文参数
- 在 nullptr 上调用无状态类的非静态成员函数是否合法?
- 编译问题:在函数"_start"中:未定义对"主"的引用 collect2:错误:ld 返回 1 个退出状态
- C++部分概念 id:显式模板规范顺序/第一个参数的特殊状态的原因是什么?
- 试图在崇高中奔跑. 错误 已发生: 收集2.exe: 错误: ld 返回 1 退出状态
- 在容器上移动分配:以前包含的对象的状态
- collect2:错误:ld 返回 1 个退出状态未定义的引用
- 具有动态大小的特征矩阵的默认初始状态
- 如何从Clojure调用C++程序,以使程序保持打开状态?
- q网络回复 无状态码或错误,但失败