如何检测 WPF 中的修饰键状态?
是否有一些全局构造可供我在需要访问 Control、Shift、Alt 按钮是否按下时使用?例如,在 TreeView
的 MouseDown
事件中。
如果是这样怎么办?
Is there some global constructs that I can use whenever I need to access whether the Control, Shift, Alt buttons are down? For instance inside MouseDown
event of a TreeView
.
If so how?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
使用类
键盘
。使用Keyboard.IsKeyDown
您可以检查 Control、Shift、Alt 现在是否已按下。对于 Shift:
对于 Control:
对于 Alt:
Use class
Keyboard
. UsingKeyboard.IsKeyDown
you can check if Control, Shift, Alt is down now.For Shift:
For Control:
For Alt:
还有:
There's also:
这就是我处理它的方式(使用 PreviewKeyDown),假设我们正在寻找 Alt + R...
也许有人可以澄清为什么我必须使用 e.SystemKey 而不仅仅是 e.Key,也许是由于修饰符?但这在搜索修饰符+键时对我来说完美无缺。
This is how I handle it (using PreviewKeyDown), let's say we are looking for Alt + R...
Maybe someone can clear up why I had to use e.SystemKey and not just e.Key, maybe due to the modifier? but this has worked flawlessly for me when searching for modifier+key.
部分借用@Josh,有点类似于@Krushik,还引用了有关 KeyEventArgs.systemKey 和 KeyEventArgs.Key 之间的区别(回答为什么 Josh 必须使用 SystemKey);其中,使用修饰键(例如 Alt)时,e.Key 返回 Key.System,因此“真实”键位于 e.SystemKey 内。
解决这个问题的方法是首先获取“真实”密钥,然后执行条件:
Partly borrowing from @Josh, and somewhat similar to @Krushik, and also referencing a question about the Difference between KeyEventArgs.systemKey and KeyEventArgs.Key (answering why Josh has to use SystemKey); wherein, with modifier keys (such as Alt), e.Key returns Key.System, and thus the 'real' key is within e.SystemKey.
A way around this, is to first fetch the 'real' key, and then do your conditional:
还有:
如果 My.Computer.Keyboard.ShiftKeyDown 那么 ...
My.Computer.Keyboard.CtrlKeyDown
My.Computer.Keyboard.AltKeyDown
and as well:
if My.Computer.Keyboard.ShiftKeyDown then ...
My.Computer.Keyboard.CtrlKeyDown
My.Computer.Keyboard.AltKeyDown