Getting keyboard state using GetKeys function

蓝咒 提交于 2020-01-03 05:20:33

问题


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);

Thankyou.


回答1:


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.

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
}


来源:https://stackoverflow.com/questions/11466294/getting-keyboard-state-using-getkeys-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!