在Win32中确定按键和按键的最快方法是什么? [英] What is the fastest way to determine a key press and key holding in Win32?

查看:178
本文介绍了在Win32中确定按键和按键的最快方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

确定按键的最快方法是什么,以及如何确定按键是否被按住?看来窗口消息传递速度很慢。请提供一个示例,说明如何执行此操作,以及为什么它比另一个更快。

What is the fastest way to determine a key press and also how to determine if a key is being held? It appears that window messaging is slow. Please provide an example of how to do so, and why it is faster than an alternative.

要清楚,这是一个实时循环(模拟)

To be clear, this for a real time loop (a simulation) so I am looking for the fastest way to determine if a key has been pressed and also to check to see if it is being held.

推荐答案

GetAsyncKeyState()是什么?重新寻找。它读取键盘的物理状态,而不考虑输入队列状态。如果设置了高位,则在调用时该键被关闭。

GetAsyncKeyState() is what you're looking for. It reads the physical state of the keyboard, regardless of the input queue state. If the high-bit is set, then the key was down at the time of the call.

// Fetch tab key state.
SHORT tabKeyState = GetAsyncKeyState( VK_TAB );

// Test high bit - if set, key was down when GetAsyncKeyState was called.
if( ( 1 << 16 ) & tabKeyState )
{
    // TAB key down... 
}

此外,为了记录,Windows不是实时操作系统。如果您的应用程序需要实时精度,您可能需要选择另一个平台。

Also, for the record, Windows is not a real-time operating system. If your application requires real-time precision, you may want to select another platform.

这篇关于在Win32中确定按键和按键的最快方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆