Creating KeyBinding in WPF with Multiple Modifier Keys

The way to create KeyBinding was something like this:

 <KeyBinding Modifiers="Ctrl" Key="S" Command="{Binding SaveCommand}" /> 

But what if I need two modifier keys? For example, Ctrl + Shift .

+59
wpf
Oct 29 '10 at 8:02
source share
5 answers

The documentation states that you can simply separate modifiers with the + symbol:

 <KeyBinding Modifiers="Ctrl+Shift" Key="S" Command="{Binding SaveCommand}" /> 

See here for details, with the corresponding bits extracted below in case the link ever disappears:




Xaml

 <object property="oneOrMoreModifierKeys"/> 

XAML Values

oneOrMoreModifierKeys - One or more modifier keys defined by the ModifierKeys enumeration, separated by a + .




You can also use the gesture yourself, rather than a combination of keys / modifiers:

 <KeyBinding Gesture="Ctrl+Shift+S" Command="{Binding SaveCommand}" /> 

according to the same link:

When defining KeyBinding in XAML, there are two ways to specify KeyGesture.

The first way to set KeyBinding in XAML is to define the Gesture attribute of the KeyBinding element, which allows the syntax to specify keys and modifiers as one line, such as "CTRL + P".

The second way is to define the Key attribute and the Modifiers attributes of the KeyBinding element.

Both methods of installing KeyGesture are equivalent and modify the same base object, but there will be a conflict if both are used. In the case when all Key, Modifiers and Gesture attributes are set, the attribute defined last will be used for KeyGesture.

+110
Oct 29 '10 at 8:24
source share
 <KeyBinding Command="{Binding SaveCommand}" Gesture="Ctrl+Shift+S" /> 

See MSDN Documentation, Key Binding Class .

+13
Oct 29 '10 at 8:15
source share

I know that the question is about XAML, but here is a sample if you want to do this in code (several ModifierKeys can be specified via logical OR):

 new KeyBinding( SaveCommand, Key.S, ModifierKeys.Control | ModifierKeys.Shift ) 
+5
Jun 10 '13 at 13:55 on
source share

Here is my code for implementing multiple character shortcuts like Alt + P + A in WPF MVVM.

Add this to XAML (attached behavior for the KeyDown event):

 cb:ShortCutBehavior.Command="{Binding Shortcuts.CmdKeyPressed}" 

Add this to your view model:

 ShortCuts Shortcuts = new ShortCuts( this ); //Add Plenty of shortcuts here until your heart is desired Shortcuts.AddDoubleLetterShortCut( AddOrganization, Key.P, Key.A, ModifierKeys.Alt, true); Shortcuts.AddSingleLetterShortCut( CmdAddNewAgreement, Key.A, ModifierKeys.Alt); 

These are two examples of adding shortcuts. The first is a double label with a letter: Alt + P + A , which launches the AddOrganization () method, and the second is a label with a single letter: Alt + A , which runs ICommand CmdAddNewAgreemnt.

Both AddDoubleLetterShortCut and AddSingleLetterShortCut are overloaded to accept actions or ICommands.

This is one of my first attempts to generalize something, so you can accept this idea and make it suitable for you.

+3
Apr 03 '13 at 16:14
source share

It may be too late, but here is the simplest and shortest solution.

 private void Window_KeyDown(object sender, KeyEventArgs e) { if (Keyboard.Modifiers == ModifierKeys.Control && e.Key == Key.S) { // Call your method here } } <Window x:Class="Test.MainWindow" KeyDown="Window_KeyDown" > 
0
Apr 12 '19 at 6:36
source share



All Articles