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 演算は、GetKeyStatesから返されたKeyStatesDown状態を比較するために使用されます。 キーがダウンしている場合、 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 クラスには、キーの状態情報を取得するために使用できる静的メソッドが多数用意されています。 メソッドは、 IsKeyDownIsKeyUp、および IsKeyToggledです。

適用対象

こちらもご覧ください