将密钥转换为VirtualKeyCode

时间:2013-04-02 10:08:59

标签: c# wpf .net-4.5 keyboard-events

在我的C#/ WPF / .NET 4.5应用程序中,我试图通过KeyEventHandler捕获按键,然后使用优秀的Windows Input Simulator模拟按键(将手势,语音等命令映射到键盘)。

问题是,我从Key的{​​{1}}获得KeyEventHandler枚举的成员,但稍后我需要将RoutedEventArgs传递给{{1} }}

如何从VirtualKeyCode转到SimulateKeyPress()

Key

1 个答案:

答案 0 :(得分:7)

似乎我正在寻找KeyInterop.VirtualKeyFromKey方法。因此上面代码中的麻烦部分变成:

// Read key press from keyboard
private void Editor_HandleKeyDownEvent(object sender, KeyEventArgs e) {
  // The rehabilitated culprit
  VirtualKeyCode CodeOfKeyToEmulate = (VirtualKeyCode)KeyInterop.VirtualKeyFromKey(e.Key);
  // /rehabilitated culprit
  PressKeyModal.Visibility = System.Windows.Visibility.Hidden;
  RemoveHandler(Keyboard.KeyDownEvent, (KeyEventHandler)Editor_HandleKeyDownEvent);
}

值得注意的是,KeyInterop.VirtualKeyFromKey方法不返回VirtualKeyCode,而是int32,必须将其转换为VirtualKeyCode