【发布时间】:2018-11-21 21:15:36
【问题描述】:
我一直在使用 MouseKeyHook NuGet 包,它非常适合捕获大多数输入。但是我在捕捉某些键 + 修饰符的组合时遇到了一些问题。
public static class InputHandler
{
private static IKeyboardMouseEvents _GlobalHook;
public static IKeyboardMouseEvents GlobalHook => _GlobalHook;
public static void Subscribe()
{
_GlobalHook = Hook.AppEvents();
_GlobalHook.KeyDown += KeyDown;
}
private static void KeyDown(object sender, KeyEventArgs e)
{
Console.WriteLine("Output: " + e.Modifiers + " + " + e.KeyCode);
}
}
让我们试着敲几个键,看看输出是什么:
Key: A
> Output: None + A
Key: Shift & A
> Output: Shift + A
Key: Shift & Control & Alt & A
> Output: Shift, Control, Alt + A
太棒了!正是你所期望的。那么键盘顶部的数字栏呢?
Key: 1
> Output: None + D1
Key: Shift & Control & Alt & 3
> Output: Shift, Control, Alt + D3
好的,再说一遍,这正是您所期望的。没问题... 0 键呢?
Key: 0
> Output: None + D0
Key: Shift & 0
> Output: Shift + D0
Key: Shift & Control & 0
> Output: Shift, Control + ShiftKey <---- What????
Key: Shift & Control & Alt & 0
> Output: Shift, Control, Alt + D0
那么这里发生了什么?为什么当 exactly D0 + Control + Shift 被按下时事件没有正确触发?另外值得注意的是,这是一个 KeyDown 事件,因此只要您按住键,输出就会重复,但是当打印“ShiftKey”输出时,它永远不会重复,这很奇怪。
最坏的情况,我总是可以切换我的绑定,但我注意到许多不同的键 + 修饰符组合(主要是 oem 键、数字键盘和数字键)的这种奇怪之处,所以很高兴知道为什么发生这种情况。
【问题讨论】:
-
按下 Shift 键时会收到通知,这很正常。您不能假设每种组合都可用。还有其他应用程序使用 RegisterHotKey() 来侦听击键,通常使用 Ctrl+Shift。这些钥匙在你看到它们之前就被劫持了。 Intel shovelware 尤其臭名昭著。
标签: c# keyboard-events mousekeyhook