What's the difference between getting a key press with:
GetKeyState()GetAsyncKeyState()getch()?
When should I use one over the other?
GetKeyState() 和 GetAsyncKeyState() 是 Windows 特定的 API,而 getch() 适用于其他非 Windows 特定的 C 编译器。
GetKeyState() 获取从线程的消息队列返回的键状态。该状态不反映与硬件相关的中断级状态。
GetAsyncKeyState() 指定自上次调用 GetAsyncKeyState() 以来是否按下了键,以及该键当前是 up 还是 down。如果设置了最高有效位,则按键按下,如果设置了最低有效位,则在上一次调用 GetAsyncKeyState() 后按下了按键。
我在实践中看到的是,如果您按住一个键并在按下该键时分配一个行为,如果您使用 GetKeyState(),则该行为将被调用更多次,而不是使用 GetAsyncKeyState()。
在游戏中,我更喜欢使用 GetAsyncKeyState()。
(您也可以在 MSDN 博客上查看更多详细信息)。
想想异步是什么意思。
GetAsyncKeyState()异步获取密钥状态,即无需等待任何东西,即NOW。
GetKeyState()同步获取key state ,就是你要读取的key的key state getch()。它与按键本身一起在键盘缓冲区中排队。
例如,假设已输入以下内容,但尚未阅读:
GetAsyncKeyState()将返回ctrl pressed
GetKeyState() will returnH 按下until you callgetch()`
GetKeyState()然后将返回I pressed,直到您致电getch()
GetKeyState()然后将返回,shift pressed, 1 pressed直到您调用getch(),这将返回!(按shift+的结果1)
GetKeyState()然后将返回ctrl pressed