Keyboard.GetKeyStates(Key) 方法

定義

取得指定按鍵的按鍵狀態集。

public:
 static System::Windows::Input::KeyStates GetKeyStates(System::Windows::Input::Key key);
public static System.Windows.Input.KeyStates GetKeyStates (System.Windows.Input.Key key);
static member GetKeyStates : System.Windows.Input.Key -> System.Windows.Input.KeyStates
Public Shared Function GetKeyStates (key As Key) As KeyStates

參數

key
Key

指定的索引鍵。

傳回

KeyStates 值的位元組合。

範例

下列範例示範如何使用 GetKeyStates 方法來判斷索引鍵是否 Return 處於 Down 狀態。 位 AND 作業可用來比較 KeyStates 傳回的 GetKeyStatesDown 狀態。 如果機碼關閉,則會變更的背景 Button

// Uses the Keyboard.GetKeyStates to determine if a key is down.
// A bitwise AND operation is used in the comparison. 
// e is an instance of KeyEventArgs.
if ((Keyboard.GetKeyStates(Key.Return) & KeyStates.Down) > 0)
{
    btnNone.Background = Brushes.Red;
}
' Uses the Keyboard.GetKeyStates to determine if a key is down.
' A bitwise AND operation is used in the comparison. 
' e is an instance of KeyEventArgs.
If (Keyboard.GetKeyStates(Key.Return) And KeyStates.Down) > 0 Then
    btnNone.Background = Brushes.Red

備註

KeyStates 是位欄位欄位列舉;因此,索引鍵可能處於多個狀態。 例如,按鍵可能處於已按下的狀態,以及處於切換狀態。 使用位比較作業來判斷索引鍵所在的確切狀態或狀態。

類別 Keyboard 提供許多靜態方法,也可用來取得密鑰狀態資訊。 方法包括: IsKeyDownIsKeyUpIsKeyToggled

適用於

另請參閱