gpt4 book ai didi

c++ - winapi GetAsyncKeyState() 不按描述工作?

转载 作者:行者123 更新时间:2023-11-28 04:52:24 24 4
gpt4 key购买 nike

根据 microsoft's documentation , 据说是 GetAsyncKeyState()

Determines whether a key is up or down at the time the function is called

我一直在构建一个 UI 自动化库,问题归结为这个

#include <Windows.h>
#include <iostream>
#include <chrono>
#include <thread>
using namespace std;

bool IsKeydownAsync(int key) {
return GetAsyncKeyState(key) & 0x8000;
}

int main(){
while (1) {
if (IsKeydownAsync('A')) {
cout << "triggered" << endl;
}
this_thread::sleep_for(chrono::milliseconds(10));
}
}

所以我的理解是,我的应用程序是否处于焦点并不重要,GetAsyncKeyState() 在被调用时应该始终返回物理键是向上还是向下。

我已经在各种应用程序上对此进行了测试,大多数时候它的行为与描述的一样。但是,在某些游戏中,这种行为会中断,并且不再报告按键是向上还是向下。 cout << "triggered" << endl按住键时不会调用。

我是否忽略了什么?

最佳答案

自从我在 Windows 中使用 native 输入以来已经有一段时间了,但根据经验,Windows API 函数仅报告键状态,这些键状态也使用同步的 Windows API 功能报告,也就是说正常的应用程序消息/事件输入。

一些较旧的游戏使用以前版本的 DirectX 和其他方式来捕获输入,例如使用自 Windows 8.1/10 以来已弃用的名为 XInput(2) 的库。虽然支持轮询和事件/消息,但输入是使用 DirectX 线程捕获的,并且与 Windows API 相比处理方式完全不同。这样做的主要原因是操作系统试图迎合所有制造商的需求,而 DirectX API 并未专门解决该输入问题。

关于c++ - winapi GetAsyncKeyState() 不按描述工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47866271/

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