0
我已經thew下面的代碼:GetasyncKeyState()不工作在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之後的語句不運行沒有關係。我試過大寫和小寫,但GetKeyState似乎沒有做任何事情。
我使用Visual C++ 2008 Express的在Windows 8.1
UPDATE: 這裏是我對消息泵代碼:
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具有相同的結果。
['GetKeyState'](https://msdn.microsoft.com/en-us/library/windows/desktop/ms646301%28v=vs.85%29.aspx)引用說:「應用程序調用* * GetKeyState **響應鍵盤輸入消息。「否則,如果您嘗試取回它可能不起作用。 – 2015-01-21 08:54:41
GetAsyncKeyState怎麼樣?它似乎適用於其他項目,但這似乎並不奏效。 – 2015-01-21 09:02:50
以這種方式使用'GetKeyState'將不起作用。您可能應該使用'GetKeyboardState',每幀獲得一次全鍵盤狀態,並測試您在此處執行的各個鍵。 'GetAsyncKeyState'也可能工作,我不知道。但是,請記住,測試鍵盤事件,同時打破和關閉調試器可能會有點令人沮喪,因爲調試器中的幀之間發生了所有關鍵狀態更改。 – yzt 2015-01-21 09:19:14