Creating KeyBinding in WPF with more than one modifier key

Wpf

Wpf Problem Overview


The way I created KeyBinding was something like:

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

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

Wpf Solutions


Solution 1 - Wpf

The documentation states that you can just separate the modifiers with the + character:

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

See here for the gory details, with the relevant 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, delimited with a + character.


You can also use a gesture on its own rather than a key/modifier combo:

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

as per that same documentation link:

> When defining a KeyBinding in XAML, there are two ways to specify the KeyGesture. > >The first way to establish a KeyBinding in XAML is to define the Gesture attribute of the KeyBinding element, which enables a syntax to specify keys and modifiers as a single string, for example "CTRL+P". > > The second way is to define the Key attribute and the Modifiers attributes of the KeyBinding element. > > Both ways of setting the KeyGesture are equivalent and modify the same underlying object, but there will be a conflict if both are used. In the case when the Key, Modifiers, and the Gesture attributes are all set, the attribute which is defined last will be used for the KeyGesture.

Solution 2 - Wpf

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

See the MSDN documentation, KeyBinding Class.

Solution 3 - Wpf

I know the question is for XAML, but here's a sample if you want to do it in code (multiple ModifierKeys can be specified via logical OR):

new KeyBinding( SaveCommand, Key.S, ModifierKeys.Control | ModifierKeys.Shift )

Solution 4 - Wpf

Here is my code to implement multiple character shortcut keys, such as Alt + P + A in WPF MVVM.

Add this to your 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 letter shortcut: Alt + P + A which runs the method AddOrganization() and the second is a single letter shortcut: Alt + A which executes the ICommand CmdAddNewAgreemnt.

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

This is one of my first attempts at generisizing something, so you can take the idea and make it suitable for you.

Solution 5 - Wpf

It might 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" >

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJiew MengView Question on Stackoverflow
Solution 1 - WpfpaxdiabloView Answer on Stackoverflow
Solution 2 - WpfAurelien RibonView Answer on Stackoverflow
Solution 3 - WpfKornMuffinView Answer on Stackoverflow
Solution 4 - WpfTerenceView Answer on Stackoverflow
Solution 5 - WpfJitendra PancholiView Answer on Stackoverflow