This web site uses cookies. By using the site you accept the cookie policy.This message is for compliance with the UK ICO law.

Windows Presentation Foundation
.NET 4.0+

WPF Commanding - Keyboard Shortcuts

The one hundred and sixty-sixth part of the Windows Presentation Foundation Fundamentals tutorial takes another look at WPF commanding. This article describes how keyboard shortcuts can be used to execute commands, using key bindings.

Creating a Basic Key Binding

To link a shortcut key to a command, you need a KeyBinding object. The simplest configuration requires that you specify a command, using the Command property, and the key to detect, using the Key property. As with other commands that we've seen, you can also specify a parameter using the CommandParameter property.

To attach a key binding to a user interface element, you add it to the item's InputBindings property. This holds a collection of such bindings. Key bindings are applied to an item and inherited by its children; if you want keys to be available generally, you can apply them to the entire window.

Let's add a basic key binding to the window. Add the following XAML within the Window element:

    <KeyBinding Key="F5"
                Command="{Binding MessageCommand}"
                CommandParameter="You pressed 'F5'"/>

Run the program and press the F5 key whilst the window is active. A message should be displayed. Note that the command only runs when you press F5 by itself. If you hold down a modifier key, such as Alt, Shift, Ctrl or the Windows key, whilst pressing F5, the command is not executed.

Modifier Keys

Creating a key binding for a single key can be useful but is quite limited. It is common to bind to keyboard combinations that include modifier keys. These are specified by setting the Modifiers property to a value from the ModifierKeys enumeration. The enumeration includes the following values:

  • None. The default option, specifying that no modifier keys are used in the key binding.
  • Alt. Requires that an ALT modifier key is pressed.
  • Control. Requires that a CTRL modifier key is pressed.
  • Shift. Requires that a SHIFT modifier key is pressed.
  • Windows. Requires that the Windows logo modifier key is pressed.

The enumeration is a bit field, allowing you to combine multiple values when you want a key binding to require that several modifier keys are pressed simultaneously. In XAML, you can combine the modifiers using the constant names, separated by plus (+) symbols.

Let's add two more bindings to the Window.InputBindings element. These capture Ctrl-Z and Ctrl-Shift-Z:

<KeyBinding Key="Z" Modifiers="Control"
            Command="{Binding MessageCommand}"
            CommandParameter="You pressed 'Ctrl-Z'"/>
<KeyBinding Key="Z" Modifiers="Control+Shift"
            Command="{Binding MessageCommand}"
            CommandParameter="You pressed 'Ctrl-Shift-Z'"/>

NB: There are many key combinations that can be linked to commands. The most common keys are listed at the end of this article. However, some key combinations are reserved for operating system functions and will not be captured when bound to commands.

Using Gestures

Another way to identify a keyboard combination is to use the Gesture property. This allows you to combine the main key and the modifiers in a single string. For example, instead of specifying a Key of "F5" and a Modifier of "Control+Shift", you can use the Gesture, "Control+Shift+F5".

Try adding a key binding for this combination, as follows:

<KeyBinding Gesture="Control+Shift+F5"
            Command="{Binding MessageCommand}"
            CommandParameter="You pressed Ctrl-Shift-F5'"/>
18 June 2015