gpt4 book ai didi

c++ - 获取键状态函数?

转载 作者:行者123 更新时间:2023-11-30 00:50:35 24 4
gpt4 key购买 nike

为什么我按下方向箭头 ON 后,函数 GetKeyState 继续给我一个大于 0 的值?

#include <iostream>
#include <windows.h>
using namespace std;

int main()
{
for(int i = 0; i < 1000; ++i)
{
if(GetKeyState(VK_UP))
{
cout << "UP pressed" << endl;
}
else
cout << "UP not pressed" << endl;

Sleep(150);
}

return 0;
}

最佳答案

来自documentation :

The key status returned from this function changes as a thread reads key messages from its message queue. The status does not reflect the interrupt-level state associated with the hardware. Use the GetAsyncKeyState function to retrieve that information.

由于您不处理消息,因此您需要调用 GetAsyncKeyState

像这样测试按下的键:

if (GetAsyncKeyState(VK_UP) < 0)
// key is pressed

关于c++ - 获取键状态函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24506004/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com