KeyGesture 類別

定義

定義可以用來叫用 (Invoke) 命令的鍵盤組合。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
屬性

範例

下列範例顯示如何使用 KeyBinding,將 Close 命令系結至 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 中通常設定的屬性會 GestureFor 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 屬性會比較常見,如果預期的命令系結僅適用于輔助按鍵,則會是 ModifiersHowever, 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 UsageXAML 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)

使用指定的 KeyGesture, Key 和顯示字串初始化 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()

傳回代表目前物件的字串。Returns a string that represents the current object.

(繼承來源 Object)

適用於

另請參閱