WPF KeyGestures - Binding non alphanumeric keys
Should be a simple one, but I can't work out how to do it. Using WPF4 I want to Bind Ctrl + - to Zoom Out and Ctrl + = to Zoom In:
<KeyBi开发者_如何学Pythonnding Command="{Binding Content.ZoomInCommand}" Gesture="Ctrl+="/>
<KeyBinding Command="{Binding Content.ZoomOutCommand}" Gesture="Ctrl+-"/>
However, I'm getting errors: in the case of Ctrl + =:
Requested value '=' was not found.
Any ideas?
Okay - it turns out that the = key does not exist (you can check this through the Key
-enumeration - there is no entry for Equal or EqualSign)... I use an international keyboard, so you have to find which key sequence you hit to enter = (for me it's Shift + D0 on a danish keyboard) - and use that key-sequence.
So your XAML should be (in Denmark):
<KeyBinding Command="{Binding Content.ZoomInCommand}" Gesture="Ctrl+Shift+D0"/>
EDIT: I believe on an American system it is the OemPlus
key - but you can check it by console-writeline'ing the e.Key
argument in a key-down event handler)
EDIT2: the - key is OemMinus
on my system.
Hint for Users with a German keyboard (maybe some other countries as well):
The numpad keys "+" and "-" are "Add" and "Subtract"
The normal keys "+" and "-" are "OemPlus" and "OemMinus"
So
<KeyBinding Gesture = "OemPlus" Command="myCommand" />
will fire the command if you press "+" on the main keyboard.
精彩评论