如何正确检索WPF KeyDown事件中的修饰键? [英] How to correctly retrieve modifier keys in a WPF KeyDown event?

查看:93
本文介绍了如何正确检索WPF KeyDown事件中的修饰键?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看到许多答案,建议使用Keyboard.Modifiers来确定KeyDown事件是否用于设置了修饰符的键.不幸的是,因为Keyboard.Modifiers返回修饰符的 current 状态(而不是按下键时的修饰符状态),所以对于快速打字员来说,这会导致一个非常烦人的间歇性错误.

I've seen a number of answers suggesting the use of Keyboard.Modifiers to determine whether a KeyDown event is for a key that had a modifier set. Unfortunately, because Keyboard.Modifiers returns the current state of the modifiers (instead of the state of the modifier when the key was pressed), this results in a really annoying intermittent bug for quick typists.

具体来说,假设有人按下Ctrl + A,然后在按下A后仅几毫秒内释放Ctrl.密钥处理程序开始执行,但被抢占了50ms.到密钥处理程序再次执行时,Ctrl的 current 状态已释放".密钥处理程序现在会认为在没有Ctrl的情况下按下了"A",这是 bad .

Specifically, imagine someone presses Ctrl+A, and releases Ctrl only a few milliseconds after pressing A. Now imagine that the system was under heavy load; the key handler started executing but was preempted for 50ms. By the time the key handler is executing again, the current state of Ctrl is "released". The key handler will now think that "A" was pressed without Ctrl, and this is bad.

类似地,如果快速打字员输入A,然后按Ctrl + End,而我的应用程序使用Keyboard.Modifiers,则它可能最终会观察到Ctrl + A ...

Similarly, if a fast typist enters A, Ctrl+End and my application uses Keyboard.Modifiers, it could instead end up observing Ctrl+A...

在WinForms中,KeyDown事件告诉我Ctrl 完全的状态,即使它在处理该事件时已经释放.如何在WPF中获得相同的行为?

In WinForms, the KeyDown event tells me the state of Ctrl exactly, even if it's already released by the time the event is being handled. How can I get this same behaviour in WPF?

编辑:Keyboard.Modifiers可能实际上未检索到当前"修饰键,而是与当前正在处理的按键按下消息相关的修饰键.在WinAPI中,这是异步"和非异步键状态函数之间的区别.不幸的是,文档没有提到当前"到底是什么意思.如果有人知道,请这么说.

Edit: it's possible that Keyboard.Modifiers doesn't actually retrieve the "current" modifier keys, but instead the modifier keys as related to the key down message currently being processed. In WinAPI, this was the distinction between "async" and non-async key state functions. Unfortunately, the documentation doesn't mention what exactly "current" means. If anyone knows, please say so.

推荐答案

抱歉,这个破坏性的答案是,但是...

sorry for this destructive answer but...

经过一番研究,对我来说很清楚... 该事件称为"KeyDown",而不是"KeyCombinationDown",因此它完全独立于在...之前按下的任何修饰键.

after a little bit of research it becomes clear to me... the event is called "KeyDown" not "KeyCombinationDown" so it is totally independent of any Modifiers pressed BEFORE...

实际上,有一种正确的方法可以实现您的目标: 使用命令模式.

Actually there is a right way for achieve your goal: Using the Commanding-Pattern.

您定义一个COMMAND(有关WPF-Commanding,请参见google),并向您的应用程序添加KeyBinding,在其中您定义键或键/键组合,以启动命令...

You define a COMMAND (see google for WPF-Commanding) and add a KeyBinding to your application, where you define the key or the keys/key-combination which will fire up the command...

在此处查看示例: http://msdn.microsoft.com/en-us /library/system.windows.input.keybinding.aspx

恕我直言,这是 only 的方式,在语义上也更加优雅.

IMHO, this is the only way and semantically more elegant, too.

(如果这种模式在一般情况下对您不起作用,则可能必须将本机api与pinvoke一起使用.)

(if this pattern will not be working for you in GENERAL, you maybe have to use native api with pinvoke).

欢呼.

这篇关于如何正确检索WPF KeyDown事件中的修饰键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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