Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 InputGestureCollection();
keyInputs.Add(new KeyGesture(Key.OemComma, ModifierKeys.Control));
preferencesCommand = new RoutedUICommand("Preferences...", "Preferences", typeof(MyCommands), keyInputs);
like image 472
Lernkurve Avatar asked Oct 16 '25 14:10

Lernkurve


1 Answers

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);
like image 131
Andy Avatar answered Oct 18 '25 03:10

Andy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!