This topic has not yet been rated - Rate this topic

KeyGesture Class

Defines a keyboard combination that can be used to invoke a command.

System.Object
  System.Windows.Input.InputGesture
    System.Windows.Input.KeyGesture

Namespace:  System.Windows.Input
Assembly:  PresentationCore (in PresentationCore.dll)
XMLNS for XAML: http://schemas.microsoft.com/winfx/2006/xaml/presentation, http://schemas.microsoft.com/netfx/2007/xaml/presentation
[TypeConverterAttribute(typeof(KeyGestureConverter))]
public class KeyGesture : InputGesture
<object property="oneOrMoreModifierKeys+key"/>
- or -
<object property="functionKey"/>

XAML Values

functionKey

Key

A single key value, which must be one of the function keys (F1-12) or numeric keypad keys.

oneOrMoreModifierKeys

One or more modifier keys, defined by the ModifierKeys enumeration, delimited with a "+" character.

key

Key

A single key value.

The KeyGesture type exposes the following members.

  Name Description
Public method KeyGesture(Key) Initializes a new instance of the KeyGesture class with the specified Key.
Public method KeyGesture(Key, ModifierKeys) Initializes a new instance of the KeyGesture class with the specified Key and ModifierKeys.
Public method KeyGesture(Key, ModifierKeys, String) Initializes a new instance of the KeyGesture class with the specified Key, ModifierKeys, and display string.
Top
  Name Description
Public property DisplayString Gets a string representation of this KeyGesture.
Public property Key Gets the key associated with this KeyGesture.
Public property Modifiers Gets the modifier keys associated with this KeyGesture.
Top
  Name Description
Public method Equals(Object) Determines whether the specified Object is equal to the current Object. (Inherited from Object.)
Protected method Finalize Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public method GetDisplayStringForCulture Returns a string that can be used to display the KeyGesture.
Public method GetHashCode Serves as a hash function for a particular type. (Inherited from Object.)
Public method GetType Gets the Type of the current instance. (Inherited from Object.)
Public method Matches Determines whether this KeyGesture matches the input associated with the specified InputEventArgs object. (Overrides InputGesture.Matches(Object, InputEventArgs).)
Protected method MemberwiseClone Creates a shallow copy of the current Object. (Inherited from Object.)
Public method ToString Returns a string that represents the current object. (Inherited from Object.)
Top

In most cases, a KeyGesture must be associated with one or more ModifierKeys. The exceptions to this rule are the function keys and the numeric keypad keys, which can be a valid KeyGesture by themselves. For example, you can create a KeyGesture by using only the F12 key, but to use the X key in a KeyGesture it must be paired with a modifier key.

In general, you can

You can use a KeyBinding to bind a KeyGesture to an ICommand, so that the command is invoked when the KeyGesture occurs.

For KeyGesture XAML usages, the property that is generally set in XAML is Gesture, in cases where the gesture represents both a standard key and a modifier key. You can also set the Gesture property to be just a function key, or just a modifier key combination. However, it is more common to set the Key property if the intended command binding is a function key with no modifiers, or Modifiers if the intended command binding is for modifier keys only.

The following example shows how to bind the Close command to a KeyGesture using a KeyBinding.


KeyGesture CloseCmdKeyGesture = new KeyGesture(
    Key.L, ModifierKeys.Alt);

KeyBinding CloseKeyBinding = new KeyBinding(
    ApplicationCommands.Close, CloseCmdKeyGesture);

this.InputBindings.Add(CloseKeyBinding);


The following example shows how to use KeyGesture in XAML. Note that the XAML usage does not directly declare a <KeyGesture> element. That object element usage is not possible because KeyGesture does not expose a public default constructor. Instead, the XAML usage uses the typeconverter behavior to declare an entire KeyGesture inline as the Gesture attribute value.


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


.NET Framework

Supported in: 4, 3.5, 3.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
Keys without modifiers
The KeyGesture class validates (in most cases) that a key has some modifier. Basically may key cannot be used without modifiers or just with Shift modifier. If you attempt to create keyboard binding like "A" (aka "None+A") an exception is thrown.
This is an artificial limitation of the KeyGesture class which is enforced in class CTor. You can workaround this limitation and create keyboard "shortcuts" like "A" or "Shift+V" by:
  • Using reflection to invoke internal (friend in VB) CTor that can skip this validation.
  • Creating your own InputGesture-derived class that will allow keyboard "shortcuts" like "A". Following sample demonstrates such class based on original KeyGesture class: http://code.msdn.microsoft.com/FreeKeyGesture-48e44dd8
Typo in Remarks section

In general, you can

You can use ...