开发者

How to assign Ctrl+, (control plus comma) as the keyboard shortcut to a WPF menu item?

开发者 https://www.devze.com 2023-01-09 09:37 出处:网络
Question I would like to assign the keyboard shortcut Ctrl + , (control plus comma) to the \"Preferences...\" menu item. How do I do that?

Question

I would like to assign the keyboard shortcut Ctrl + , (control plus comma) to the "Preferences..." menu item. How do I do that?

There is a Key.OemComma in the Key enumeration. I have used Key.OemComma as shown in the code sample below. That works fine functionality-wise. But GUI-wise: the menu item is displayed as

  • Preferences (Ctrl+OemComma)

instead of

  • Preferences (Ctrl+,)

Sample Code

InputGestureCollection keyInputs = new InputGestureColle开发者_JAVA百科ction();
keyInputs.Add(new KeyGesture(Key.OemComma, ModifierKeys.Control));
preferencesCommand = new RoutedUICommand("Preferences...", "Preferences", typeof(MyCommands), keyInputs);


I think the KeyGesture constructor that takes a display string would work. You could call it like this:

InputGestureCollection keyInputs = new InputGestureCollection();
keyInputs.Add(new KeyGesture(Key.OemComma, ModifierKeys.Control, "Ctrl+,"));
preferencesCommand = new RoutedUICommand("Preferences...", "Preferences", typeof(MyCommands), keyInputs);
0

精彩评论

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