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
屬性

範例

下列範例顯示如何Close KeyGesture使用KeyBinding, 將命令系結至。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)

下列範例顯示如何在中KeyGesture XAMLXAML使用。The 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. 此規則的例外狀況是函式索引鍵和數位鍵臺上鍵, 其本身可能是KeyGesture有效的。The 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

您可以使用將KeyBinding系結KeyGestureICommand, 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 Gesture使用方式, 通常在中設定的屬性是, 在手勢同時代表標準索引鍵和輔助按鍵的情況下。 XAMLXAMLFor 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 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)

適用於

另請參閱