KeyGesture 类

定义

定义可用来调用命令的组合键。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
type KeyGesture = class
    inherit InputGesture
Public Class KeyGesture
Inherits InputGesture
继承
KeyGesture
属性

示例

下面的示例演示如何使用 KeyBindingClose 命令绑定到 KeyGestureThe 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)

下面的示例演示如何在 XAMLXAML中使用 KeyGestureThe following example shows how to use KeyGesture in XAMLXAML. 请注意,XAMLXAML 使用不会直接声明 <KeyGesture> 元素。Note that the XAMLXAML usage does not directly declare a <KeyGesture> element. 由于 KeyGesture 不公开公共无参数构造函数,因此不能使用该对象元素。That object element usage is not possible because KeyGesture does not expose a public parameterless constructor. 相反,XAMLXAML 用法使用 typeconverter 行为将整个 KeyGesture 作为 Gesture 特性值进行声明。Instead, the XAMLXAML 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>

注解

在大多数情况下,KeyGesture 必须与一个或多个 ModifierKeys相关联。In most cases, a KeyGesture must be associated with one or more ModifierKeys. 此规则的例外情况是函数键和数字键盘键,它们本身可以是有效的 KeyGestureThe exceptions to this rule are the function keys and the numeric keypad keys, which can be a valid KeyGesture by themselves. 例如,可以仅使用 F12 键创建 KeyGesture,但要在 KeyGesture 中使用 X 键,必须将其与修改键配对。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

您可以使用 KeyBindingKeyGesture 绑定到 ICommand,以便在发生 KeyGesture 时调用该命令。You can use a KeyBinding to bind a KeyGesture to an ICommand, so that the command is invoked when the KeyGesture occurs.

对于 KeyGesture XAMLXAML 用法,通常在 XAMLXAML 中设置的属性是 Gesture,在这种情况下,该笔势同时表示标准键和修改键。For KeyGesture XAMLXAML usages, the property that is generally set in XAMLXAML is Gesture, in cases where the gesture represents both a standard key and a modifier key. 你还可以将 Gesture 属性设置为仅是一个功能键,或者只是一个修改键组合。You can also set the Gesture property to be just a function key, or just a modifier key combination. 但是,如果预期的命令绑定是不含修饰符的函数键,则设置 Key 属性更常见 Modifiers,如果预期的命令绑定仅用于修改键,则更常见。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 属性用法XAML Attribute Usage

<object property="oneOrMoreModifierKeys+key"/>  
- or -  
<object property="functionKey"/>  

XAML 值XAML Values

functionKey
Key

单个键值,必须是功能键(F1-12)或数字键盘键之一。A single key value, which must be one of the function keys (F1-12) or numeric keypad keys.

oneOrMoreModifierKeys
ModifierKeys 枚举定义的一个或多个修改键,用 "+" 字符分隔。One or more modifier keys, defined by the ModifierKeys enumeration, delimited with a "+" character.

key
Key

单个键值。A single key value.

构造函数

KeyGesture(Key)

使用指定的 KeyGesture 初始化 Key 类的新实例。Initializes a new instance of the KeyGesture class with the specified Key.

KeyGesture(Key, ModifierKeys)

使用指定的 KeyGestureKey 初始化 ModifierKeys 类的新实例。Initializes a new instance of the KeyGesture class with the specified Key and ModifierKeys.

KeyGesture(Key, ModifierKeys, String)

使用指定的 KeyGestureKey 和显示字符串初始化 ModifierKeys 类的新实例。Initializes a new instance of the KeyGesture class with the specified Key, ModifierKeys, and display string.

属性

DisplayString

获取此 KeyGesture 的字符串表示形式。Gets a string representation of this KeyGesture.

Key

获取与此 KeyGesture 关联的键。Gets the key associated with this KeyGesture.

Modifiers

获取与此 KeyGesture 关联的修改键。Gets the modifier keys associated with this KeyGesture.

方法

Equals(Object)

确定指定的对象是否等于当前对象。Determines whether the specified object is equal to the current object.

(继承自 Object)
GetDisplayStringForCulture(CultureInfo)

返回可用于显示 KeyGesture 的字符串。Returns a string that can be used to display the KeyGesture.

GetHashCode()

用作默认哈希函数。Serves as the default hash function.

(继承自 Object)
GetType()

获取当前实例的 TypeGets the Type of the current instance.

(继承自 Object)
Matches(Object, InputEventArgs)

确定此 KeyGesture 是否和与指定的 InputEventArgs 对象关联的输入匹配。Determines whether this KeyGesture matches the input associated with the specified InputEventArgs object.

MemberwiseClone()

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(继承自 Object)
ToString()

返回一个表示当前对象的 string。Returns a string that represents the current object.

(继承自 Object)

适用于

另请参阅