GetasyncKeyState() 在 C++ 中不起作用

GetasyncKeyState() not working in C++

本文关键字:不起作用 C++ GetasyncKeyState      更新时间:2023-10-16

我有以下代码:

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,结果相同。

我重置了计算机,现在代码可以工作了! 不知道为什么,我想有些东西只是需要刷新或其他东西。