开发者

No entry for the Equals key in the System.Windows.Input.Key enum?

开发者 https://www.devze.com 2023-02-14 15:03 出处:网络
I\'m trying to set an InputGesture on a RoutedUICommand, hooking it up to catch when the user presses Ctrl + =. I\'m using a KeyGesture object, but I开发者_StackOverflow can\'t see an entry in the Sys

I'm trying to set an InputGesture on a RoutedUICommand, hooking it up to catch when the user presses Ctrl + =. I'm using a KeyGesture object, but I开发者_StackOverflow can't see an entry in the System.Windows.Input.Key enum for the equals ('=') key.

What I was expecting was to be able to do the following:

ZoomIn = new RoutedUICommand("Zoom In", "ZoomIn", typeof(Window),
    new InputGestureCollection { 
        new KeyGesture(Key.Equals, ModifierKeys.Control) 
    });

Can anyone point me in the right direction, please?


As pointed out by ChrisF, I needed to engage my brain a little. For what it's worth, the value generated when handling the KeyDown event is Key.OemPlus.

Update:
One consequence of this is that, if you're doing the same same as me and you're going to use the command in a menu, you'll probably want to use the overloaded constructor for KeyGesture that takes a 3rd parameter of displayString. For example:

new KeyGesture(Key.Equals, ModifierKeys.Control, "Ctrl+=")

Otherwise, you'll see the keyboard shortcut displayed as (in my case) "Ctrl+OemPlus", which isn't exactly desirable. Admittedly, the above still isn't great, but it's better than "OemPlus".


One hack is to catch the shift keydown, and if the OemPlus keydown comes before the shift keyup, you have a "OemEqual". The code may look like this:

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;
    }
}


if i test it, i get Key.Unknown as the key code, and PlatformKeyCode= = 0x000000bb (187)


No entry for the Equals key in the System.Windows.Input.Key

This isn't true.

You use Key.OemPlus for the +/= key

You use Key.OemMinus for the -/_ key

This pattern stays the same for other keys that are dual use, it wouldn't make sense to have two key codes for the same key.

You use a Key Modifier like Shift to determine what the user intended.

As an example of what I mean by this, traditionally when you press Key.OemPlus we assume you are pressing =, We don't count it as + unless you are holding Shift down.

0

精彩评论

暂无评论...
验证码 换一张
取 消