語言

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 中。 使用位元與運算來比較 KeyStates 回傳的來源 GetKeyStates 與 Down 狀態。 如果調性按下,A 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 類別提供多種靜態方法,也可用來取得金鑰狀態資訊。 這些方法分別為:IsKeyDown、、 IsKeyUpIsKeyToggled和 。

適用於

另請參閱