GetasyncKeyState() 在 C++ 中不起作用
GetasyncKeyState() not working in C++
我有以下代码:
void InitDirect3DApp::updateScene(float dt)
{
D3DApp::updateScene(dt);
// Update angles based on input to orbit camera around box.
if(GetKeyState('A') & 0x8000) mTheta -= 2.0f*dt;
if(GetKeyState('D') & 0x8000) mTheta += 2.0f*dt;
if(GetKeyState('W') & 0x8000) mPhi -= 2.0f*dt;
if(GetKeyState('S') & 0x8000) mPhi += 2.0f*dt;
// Restrict the angle mPhi.
if( mPhi < 0.1f ) mPhi = 0.1f;
if (mPhi > PI - 0.1f) mPhi = PI-0.1f;
// Convert Spherical to Cartesian coordinates: mPhi measured from +y
// and mTheta measured counterclockwise from -z.
float x = 5.0f * sinf(mPhi) * sinf(mTheta);
float z = -5.0f * sinf(mPhi) * cosf(mTheta);
float y = 5.0f * cosf(mPhi);
// Build the view matrix.
D3DXVECTOR3 pos(x, y, z);
D3DXVECTOR3 target(0.0f, 0.0f, 0.0f);
D3DXVECTOR3 up(0.0f, 1.0f, 0.0f);
D3DXMatrixLookAtLH(&mView, &pos, &target, &up);
}
问题是,无论是否按下相关键,GetKeyState 之后的 if 语句中的代码都不会运行。 我已经尝试了大写和小写,但 GetKeyState 似乎什么也没做。
我在 Windows 8.1 上使用 Visual C++ 2008 Express
更新:这是我为消息泵准备的代码:
MSG msg = { 0 };
mTimer.reset();
while (msg.message != WM_QUIT)
{
// If there are Window messages then process them.
if (PeekMessage(&msg, 0, 0, 0, PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
// Otherwise, do animation/game stuff.
else
{
mTimer.tick();
if (!mAppPaused)
updateScene(mTimer.getDeltaTime());
else
Sleep(50);
drawScene();
}
}
return (int)msg.wParam;
我也尝试了getasynckeystate,结果相同。
我重置了计算机,现在代码可以工作了! 不知道为什么,我想有些东西只是需要刷新或其他东西。
相关文章:
- 我的神经网络不起作用 [XOR 问题]
- 在C++程序中输入的文本文件将不起作用,除非文本被复制和粘贴
- C++映射:具有自定义类的运算符[]不起作用(总是返回0)
- C++为什么尽管我调用了void函数,它却不起作用
- 为什么在保护模式下继承升级不起作用
- 循环在计数器中不起作用
- 在其他文件中创建类时在 c++ 项目中不起作用
- Visual studio代码重构似乎不起作用(例如,重命名符号-f2)
- 为什么二进制搜索在我的测试中不起作用
- 我的代码中有错误吗?使用BGI图形的C++代码对我不起作用
- 为什么 const std::p air<K,V>& 在 std::map 上基于范围的 for 循环不起作用?
- 带有指定长度字符* 参数的 std::regex_search 在 VS2017 中不起作用?
- Bjarne Stroustrup Book - std_lib_facilities.h - 不起作用(未知类型名称)
- 为什么简单的算术减法在"if"条件下不起作用?
- 为什么Stroustup书中的has_f不起作用
- 你能检查一下为什么在这个代码中从链接列表中删除项目不起作用吗
- 嵌套While循环不起作用(C++问题)
- C++Matching Brackets 2解决方案不起作用
- 为什么这段代码不起作用,我该如何解决?
- 我正在开发服务器,ip作为参数传递不起作用