当应用程序失去焦点时,由GetKeyState修改的WINAPI GetKeyboardState行为吗? [英] WINAPI GetKeyboardState behavior modified by GetKeyState when application is out of focus?

查看:68
本文介绍了当应用程序失去焦点时,由GetKeyState修改的WINAPI GetKeyboardState行为吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从WPF应用程序(假定还包括其他应用程序)调用WINAPI命令 GetKeyboardState(aByteArray)时,只有在以下情况下,才能正确检测到按住 SHIFT 键的情况应用程序具有重点.当应用程序没有焦点时,方法调用后 aByteArray [VK_SHIFT] 0 .

When calling the WINAPI command GetKeyboardState(aByteArray) from a WPF application (and assumedly other applications as well), holding down the SHIFT key is correctly detected only when the application has focus. When the application does not have focus, aByteArray[VK_SHIFT] is 0 after the method call.

但是,如果在 GetKeyboardState(aByteArray)之前紧接调用 GetKeyState(aVKCode),则即使 aVKCode 的任何值都被调用,返回值被丢弃,那么当应用程序不处于焦点位置时, GetKeyboardState(aByteArray)将提供所按住的 SHIFT 键的正确非零状态.

However, if GetKeyState(aVKCode) is called immediately prior to GetKeyboardState(aByteArray), for any value of aVKCode, even if the return value is discarded, then GetKeyboardState(aByteArray) will provide the correct non-zero state of the held SHIFT key when the application is not in focus.

这种行为是违反直觉的,并且似乎引起了许多程序员的悲伤.我相信 GetKeyboardState 文档中有相关信息,内容为:

This behavior is counterintuitive and appears to have caused many programmers grief. I believe that there is relevant information in the documentation for GetKeyboardState which reads:

状态随着线程从其消息队列中删除键盘消息而改变.当键盘消息发布到线程的消息队列中时,状态不会改变,键盘消息发布到其他线程的消息队列中或从其他线程的消息队列中检索时,状态也不会更改.

The status changes as a thread removes keyboard messages from its message queue. The status does not change as keyboard messages are posted to the thread's message queue, nor does it change as keyboard messages are posted to or retrieved from message queues of other threads.

可能, GetKeyState()与消息队列进行交互,以使其必须在 GetKeyboardState()之前被调用,以使 GetKeyboardState()表现理想.但是,我不熟悉消息队列的概念以及Windows如何添加和删除项目,因此我想在这里问一下.谁能解释为什么仅对 GetKeyboardState()的调用无法捕获没有应用程序焦点的情况下按住的 SHIFT 键吗?

Potentially, GetKeyState() interacts with the message queue in such a way that it must be called prior to GetKeyboardState() in order for GetKeyboardState() to behave as desired. However, I am unfamiliar with the concept of the message queue and how items are added and removed by Windows, and so I thought I'd ask here. Can anyone explain why a call to GetKeyboardState() alone cannot capture the SHIFT key being held without application focus?

推荐答案

GetKeyState is always associated to the last processed message of a thread. So when you use GetKeyState inside a message handler you get the keystate when this message was placed into the message queue.

如果消息处理被延迟,则可以保证程序员可以处理每条消息,并且他可以在那一刻检测出关键状态.I.E .:发生鼠标单击时,是否将Shift键按下?"

If message processing is delayed, this guarantees, that each message can be processed by the programmer and he can detect the key states in that moment. I.E.: "Was the Shift key down when the mouse click happened?"

如果在其他消息之间进行了处理,则仅使用 GetKeyState 可能会跳过状态或消息.

Just using GetKeyState may skip a state or message if other messages were processed in between.

要检测当前键状态,必须使用

To detect the current key state you must use GetAsyncKeyState

这些功能都有详细记录.

The functions are well documented.

最后:GetKeyState和GetAsyncKeyState都不是检测是否在程序内部按下了Shift键的好方法.处理消息并检测WM_KEYDOWN.

Finally: Neither GetKeyState nor GetAsyncKeyState is a good way to detect if the shift key was pressed inside a program. Process messages and detect WM_KEYDOWN.

这篇关于当应用程序失去焦点时,由GetKeyState修改的WINAPI GetKeyboardState行为吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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