Edit

Share via


KeyGesture Constructors

Definition

Initializes a new instance of the KeyGesture class.

Overloads

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.

KeyGesture(Key)

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

public KeyGesture (System.Windows.Input.Key key);

Parameters

key
Key

The key associated with this gesture.

Exceptions

key is not a valid Key.

key is not a valid KeyGesture.

Examples

The following example shows how to use this constructor to create a KeyGesture and associate it with a KeyBinding. The KeyBinding is then added to the InputBindingCollection of a Window.

KeyGesture OpenCmdKeyGesture = new KeyGesture(Key.F12);
KeyBinding OpenKeyBinding = new KeyBinding(
    ApplicationCommands.Open,
    OpenCmdKeyGesture);

this.InputBindings.Add(OpenKeyBinding);

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.

See also

Applies to

.NET Framework 4.8.1 and other versions
Product Versions
.NET Framework 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9

KeyGesture(Key, ModifierKeys)

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

public KeyGesture (System.Windows.Input.Key key, System.Windows.Input.ModifierKeys modifiers);

Parameters

key
Key

The key associated with the gesture.

modifiers
ModifierKeys

The modifier keys associated with the gesture.

Exceptions

modifiers is not a valid ModifierKeys

-or-

key is not a valid Key.

key and modifiers do not form a valid KeyGesture.

Examples

The following example shows how to use this constructor to create a KeyGesture and associate it with a KeyBinding. The KeyBinding is then added to the InputBindingCollection of a Window.

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

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

this.InputBindings.Add(CloseKeyBinding);

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.

See also

Applies to

.NET Framework 4.8.1 and other versions
Product Versions
.NET Framework 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9

KeyGesture(Key, ModifierKeys, String)

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

public KeyGesture (System.Windows.Input.Key key, System.Windows.Input.ModifierKeys modifiers, string displayString);

Parameters

key
Key

The key associated with the gesture.

modifiers
ModifierKeys

The modifier keys associated with the gesture.

displayString
String

A string representation of the KeyGesture.

Exceptions

modifiers is not a valid ModifierKeys

-or-

key is not a valid Key.

displayString is null.

key and modifiers do not form a valid KeyGesture.

Remarks

The DisplayString property is set to the displayString parameter.

See also

Applies to

.NET Framework 4.8.1 and other versions
Product Versions
.NET Framework 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9