物理密钥状态 [英] Physical Key State

查看:110
本文介绍了物理密钥状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

WINdows API中的两个键状态函数GetKeyState()GetAsyncKeyState()都基于上/下键消息而不是键的物理状态来确定键状态.

The two key-state functions in the WIndows API, GetKeyState() and GetAsyncKeyState(), both determine key state based on key up/down messages rather than the physical state of the key.

我正在开发一个程序,该程序使用SendInput()操纵输入,以释放修饰键(alt,ctrl等),发送输入,然后重新按下修饰键.

I am working on a program which manipulates input, using SendInput(), to release modifier keys (alt, ctrl, etc...), send input, and then re-press the modifier keys.

问题是我不知道发送输入后是否仍然按下修饰键,因为我已经发送了按下事件,并且上面提到的两个函数都返回了按下状态的键,而与状态无关物理密钥.因此,如果我假设它们仍然处于按下状态,则用户将悬空ctrl-down留给用户,从而导致问题,直到用户再次按下并释放cntl(或任何修改键)为止.否则,即使物理按键仍处于按下状态,按键也可能会向上键.

The problem is that I don't know if the modifier keys are still pressed after the input is sent because I have sent the key-up event and both of the above mentioned functions return that the key is up regardless of the state of the physical key. So if I assume they are still down, the user is left with a dangling ctrl-down causing problems until the user presses and releases cntl again (or any modifier key). Otherwise the key may be left up even when the physical key is still down.

那么有什么方法(最好没有太低的水平)来检测物理键状态.仅限Windows的方法很好.真的不可能(或者至少真的,真的不希望)进行按键监视(侦听按键事件).

So is there any way (preferably without anything too low level) to detect the physical key state. Windows-only methods are fine. Key monitoring (listening for key up events) really isn't possible (or at least really, really not preferable).

推荐答案

经过大量测试,我似乎已经知道了. MSDN声明有关GetKeyState()的信息:

After much testing, I seem to have figured it out. MSDN states about GetKeyState() :

此函数返回的键状态随着线程从其消息队列中读取键消息而改变.

The key status returned from this function changes as a thread reads key messages from its message queue.

GetAsyncKeyState()仍然适用于按键上/下键消息(不是物理键状态),但是,它只是不等待消息被读取.因此,如果键事件消息是通过SendInput()发送的,则它仍将返回错误的消息-实际上,它在GetKeyState()之前是错误的,因为在调用后立即是不正确的.

GetAsyncKeyState() still works on key up/down messages (not physical key state) however, it just doesn't wait for the message to be read. So if a key event message is sent via SendInput(), it will still return incorrectly - in fact, it will be incorrect before GetKeyState() because it will be incorrect immediately after the call.

此处(VS2010解决方案),或者是演示该功能的简单测试只是源此处.

A simple test to demonstrate this functionality is here (VS2010 solution) or just the source here.

这篇关于物理密钥状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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