WPF KeyGestures - Binding non alphanumeric keys

佐手、 提交于 2019-12-08 16:49:25

问题


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:

    <KeyBinding 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?


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/3366424/wpf-keygestures-binding-non-alphanumeric-keys

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!