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

适用于

另请参阅