获取当前密钥状态?

Get current keystate?

本文关键字:状态 密钥 获取      更新时间:2023-10-16

我正在尝试获取密钥 ALT 的当前密钥状态? 但它不起作用,为什么?

这是代码

#include <iostream>
#include <Windows.h>

bool KeyPressed(short p_key)
{
if (GetAsyncKeyState(p_key) & 0x8000)
{
std::cout << "KEYPRESSED";
return true;
}
else return false;
}
int main()
{
std::cout << "Test";
while (true)
{
KeyPressed(0x4A);
}
std::cout << "";
}

它应该每次检查时都得到状态,但它根本没有

尝试使用这样的东西:

if (GetKeyState(VK_MENU) & 0x8000))
{
// ALT key is down.
}

VK_MENU是 ALT 键的虚拟键代码

在以下文章中找到了此信息: https://learn.microsoft.com/en-us/windows/win32/learnwin32/keyboard-input