我正在尝试在InputGesture
上设置RoutedUICommand
,并将其 Hook 以在用户按下Ctrl + =
时捕获。我正在使用KeyGesture
对象,但在System.Windows.Input.Key
枚举中找不到等号('=')键的条目。
我所期望的是能够执行以下操作:
ZoomIn = new RoutedUICommand("Zoom In", "ZoomIn", typeof(Window),
new InputGestureCollection {
new KeyGesture(Key.Equals, ModifierKeys.Control)
});
有人能指出我正确的方向吗?
最佳答案
一种技巧是捕获换档按键,如果OemPlus换档键在换档按键之前,您将获得“OemEqual”。
代码可能看起来像这样:
private bool shift = false;
private void Window_KeyDown(object sender, KeyEventArgs e)
{
Key key = e.Key;
switch (key) {
case Key.LeftShift: this.shift = true; break;
case Key.RightShift: this.shift = true; break;
case Key.C: this.helper.Command(CMD.CLEAR); break;
case Key.Enter: this.helper.Command(CMD.EVAL); break;
case Key.OemMinus: this.helper.Operator(OP.SUB); break;
case Key.OemPlus:
if (this.shift) {
this.helper.Operator(OP.ADD);
} else {
this.helper.Command(CMD.EVAL);
} break;
case Key.OemPeriod: this.helper.Number(NumberPad.DECIMAL); break;
case Key.D0: this.helper.Number(NumberPad.ZERO); break;
case Key.D1: this.helper.Number(NumberPad.ONE); break;
case Key.D2: this.helper.Number(NumberPad.TWO); break;
:
}
}
private void Window_KeyUp(object sender, KeyEventArgs e)
{
Key key = e.Key;
switch (key) {
case Key.LeftShift: this.shift = false; break;
case Key.RightShift: this.shift = false; break;
}
}
关于.net - System.Windows.Input.Key枚举中没有等于键的条目?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5170800/