[英]GetKeyState() vs. GetAsyncKeyState() vs. getch()?
按下按键有什么区别:
GetKeyState()
GetAsyncKeyState()
getch()
?我什么时候应该使用一个?
GetKeyState() 和 GetAsyncKeyState() 是 Windows 特定的 API,而 getch() 可用于其他非 Windows 特定的 C 编译器。
GetKeyState() 获取从线程的消息队列返回的键状态。 该状态不反映与硬件关联的中断级状态。
GetAsyncKeyState() 指定自上次调用 GetAsyncKeyState() 以来是否按下了键,以及键当前是 up 还是 down 。 如果设置了最高有效位,则键处于按下状态,如果设置了最低有效位,则在上一次调用 GetAsyncKeyState() 后按下了键。
我在实践中看到的是,如果您按住某个键并在该键被按下时分配一个行为,如果您使用 GetKeyState(),则该行为将比您使用 GetAsyncKeyState() 被调用更多次。
在游戏中,我更喜欢使用 GetAsyncKeyState()。
(您也可以在 MSDN 博客上查看更多详细信息)。
想想异步意味着什么。
GetAsyncKeyState()
异步获取密钥状态,即无需等待任何东西,即NOW 。
GetKeyState()
同步获取键状态,它是您将要使用getch()
读取的键的键状态。 它与按键本身一起在键盘缓冲区中排队。
例如,假设以下内容已被输入,但尚未被阅读:
GetAsyncKeyState()
将返回ctrl pressed
GetKeyState() will return
H 按下, until you call
getch()`
GetKeyState()
然后将返回I pressed
直到你调用getch()
GetKeyState()
然后将返回shift pressed, 1 pressed
直到您调用getch()
,它将返回!
(按shift + 1 的结果)
GetKeyState()
然后将返回ctrl pressed
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.