Export (0) Print
Expand All

KeyBinding Class

Binds a KeyGesture and an ICommand.

Namespace: System.Windows.Input
Assembly: PresentationCore (in presentationcore.dll)
XML Namespace:  http://schemas.microsoft.com/winfx/2006/xaml/presentation

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

A KeyBinding associates a KeyGesture with a ICommand, such as a RoutedCommand. When the KeyGesture is executed the command is invoked. For more information on commanding, see the Commanding Overview.

With the exception of the function keys and the numeric keypad keys, a KeyGesture must contain a 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.

When defining a KeyBinding in Extensible Application Markup Language (XAML) there are two ways to specify the KeyGesture. The first is to define the Key and the Modifiers attributes of the KeyBinding element. The second way to create a KeyBinding in XAML is to define the Gesture attribute 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 set, the attribute which is defined last will be used for the KeyGesture.

An KeyBinding can be defined on a specific object or at the class level by registering a RegisterClassInputBinding on the CommandManager class.

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

<Window.InputBindings>
  <KeyBinding Command="ApplicationCommands.Open"
              Gesture="CTRL+R" />
</Window.InputBindings>

// Creating a KeyBinding between the Open command and Ctrl-R
KeyBinding OpenCmdKeyBinding = new KeyBinding(
    ApplicationCommands.Open, 
    Key.R, 
    ModifierKeys.Control);

this.InputBindings.Add(OpenCmdKeyBinding);

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 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0

Community Additions

ADD
Show:
© 2015 Microsoft