使用GetKeys函数获取键盘状态 [英] Getting keyboard state using GetKeys function
问题描述
有人知道如何通过GetKeys函数获取任何键状态(按下或否)吗?
换句话说,该函数的处理方式:
Does anyone know how to get any key state (pressed or no) by GetKeys function? In other words how to handle this function:
bool result = isPressed(kVK_LeftArrow);
谢谢。
推荐答案
KeyMap
类型是整数数组,但其实际布局是一系列位,每个键代码一个。特定密钥的位数比虚拟密钥代码少 。
The KeyMap
type is an array of integers but its real layout is a series of bits, one per key code. The bit number for a particular key is one less than the virtual key code.
因为对于很大的值,移位是不合法的(例如,您不能只要求编译器移位74位), KeyMap
类型分为4部分。您需要使用虚拟键控代码的位数,然后将整数除以32,以找到该位数的正确整数;然后用其余的值找出应该设置的位。
Since bit-shifting isn't legal for very large values (e.g. you can't just ask the compiler to shift 74 bits), the KeyMap
type is broken into 4 parts. You need to take the virtual key code's bit number and integer-divide by 32 to find the correct integer for the bit; then take the remainder to figure out which bit should be set.
因此,请尝试以下操作:
So, try this:
uint16_t vKey = kVK_LeftArrow;
uint8_t index = (vKey - 1) / 32;
uint8_t shift = ((vKey - 1) % 32);
KeyMap keyStates;
GetKeys(keyStates);
if (keyStates[index] & (1 << shift))
{
// left arrow key is down
}
这篇关于使用GetKeys函数获取键盘状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!