Redigera

Dela via


KeyGesture Class

Definition

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

public ref class KeyGesture : System::Windows::Input::InputGesture
[System.ComponentModel.TypeConverter(typeof(System.Windows.Input.KeyGestureConverter))]
public class KeyGesture : System.Windows.Input.InputGesture
[<System.ComponentModel.TypeConverter(typeof(System.Windows.Input.KeyGestureConverter))>]
type KeyGesture = class
    inherit InputGesture
Public Class KeyGesture
Inherits InputGesture
Inheritance
KeyGesture
Attributes

Examples

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);
Dim CloseCmdKeyGesture As New KeyGesture(Key.L, ModifierKeys.Alt)

Dim CloseKeyBinding As New KeyBinding(ApplicationCommands.Close, CloseCmdKeyGesture)

Me.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 parameterless 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>

Remarks

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.

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.

XAML Attribute Usage

<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.

Constructors

KeyGesture(Key)

Initializes a new instance of the KeyGesture class with the specified Key.

KeyGesture(Key, ModifierKeys)

Initializes a new instance of the KeyGesture class with the specified Key and ModifierKeys.

KeyGesture(Key, ModifierKeys, String)

Initializes a new instance of the KeyGesture class with the specified Key, ModifierKeys, and display string.

Properties

DisplayString

Gets a string representation of this KeyGesture.

Key

Gets the key associated with this KeyGesture.

Modifiers

Gets the modifier keys associated with this KeyGesture.

Methods

Equals(Object)

Determines whether the specified object is equal to the current object.

(Inherited from Object)
GetDisplayStringForCulture(CultureInfo)

Returns a string that can be used to display the KeyGesture.

GetHashCode()

Serves as the default hash function.

(Inherited from Object)
GetType()

Gets the Type of the current instance.

(Inherited from Object)
Matches(Object, InputEventArgs)

Determines whether this KeyGesture matches the input associated with the specified InputEventArgs object.

MemberwiseClone()

Creates a shallow copy of the current Object.

(Inherited from Object)
ToString()

Returns a string that represents the current object.

(Inherited from Object)

Applies to

See also