This documentation is archived and is not being maintained.

KeyBinding Class

Binds a KeyGesture to a RoutedCommand (or another ICommand implementation).

Namespace:  System.Windows.Input
Assembly:  PresentationCore (in PresentationCore.dll)

public class KeyBinding : InputBinding
<KeyBinding .../>

A KeyBinding associates a KeyGesture with a ICommand, such as a RoutedCommand. RoutedCommand is the primary implementation of the ICommand interface for the WPF commanding system .  In general, when the KeyGesture is executed the command is invoked, although the command behavior is further influenced by command-specific factors such as the CanExecute value. For more information on commanding, see the Commanding Overview.

With the exception of the function keys and the numeric keypad keys, a valid KeyGesture must contain exactly one Key and one or more ModifierKeys. Function keys and numeric keypad keys do not require a modifier key in order to be a valid KeyGesture. It is possible to specify an invalid KeyGesture and a KeyBinding with an invalid associated gesture, either through XAML or code. For instance, there is no validation that prevents creating and binding a KeyGesture  that contains only a nonfunction key, or only modifiers but no key. Such a KeyBinding will never attempt to invoke its associated command.

When defining a KeyBinding in Extensible Application Markup Language (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. You can potentially have situations where for example a Key set last will overwrite just the Key component of a preceding Gesture but leave the Gesture's modifiers the same. In general, it is recommended that you use only the Gesture attribute from XAML; this will avoid ambiguity, provides the most streamlined syntax, and provides the most straightforward representation for serialization.

A KeyBinding can be defined on a specific object or at the class level by registering a RegisterClassInputBinding on the CommandManager class. The most typical way to define a KeyBinding is within the control template, setting the InputBindings property in XAML by declaring one or more MouseBinding or KeyBinding elements.

The following example creates a KeyBinding and adds it to the InputBindingCollection of the root window.

  <KeyBinding Command="ApplicationCommands.Open"
              Gesture="CTRL+R" />
// Creating a KeyBinding between the Open command and Ctrl-R
KeyBinding OpenCmdKeyBinding = new KeyBinding(


Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 7, Windows Vista, Windows XP SP2, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0