.net - System.Windows.Input.Key枚举中没有等于键的条目?

标签 .net wpf keyboard

我正在尝试在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/

相关文章:

.net - 手动编辑packages.config

c# - 如何彻底清除/设置WinRT 的RichEditBox 的文本?

c# - 在 WPF MVVM 中通信两个 View 模型

.net - Winform->WPF MVVM 键绑定(bind)错误?

windows - 使用 AutoHotKey(或其他工具)创建自定义键盘布局

android - 如何在屏幕底部创建一个不是矩形的屏幕安卓键盘?

c# - 为什么 Expression.And 代表 "&"而不是 "&&"

c# - 如何将 GUID 的一部分转换为长的?

c# - 如何检查 wpf C# 中的鼠标按钮是左键还是右键?

mobile - 如何在没有 Home/End 键和 Fn 键的键盘上导航代码?