Keyboard.GetKeyStates(Key) 方法

定义

获取指定键的键状态集。Gets the set of key states for the specified 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

参数

key
Key

指定的键。The specified key.

返回

KeyStates 值的按位组合。A bitwise combination of the KeyStates values.

示例

下面的示例演示如何使用 GetKeyStates 方法来确定 Return 键是否处于 Down 状态。The following example shows how to use the GetKeyStates method to determine if the Return key is in the Down state. 位和运算用于比较从 GetKeyStatesDown 状态返回的 KeyStatesA bit AND operation is used to compare the KeyStates returned from GetKeyStates and the Down state. 如果键已关闭,则会更改 Button 的背景。If the key is down, the background of a Button is changed.

// 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 是位域枚举;因此,键可能处于多个状态。KeyStates is a bit field enumeration; therefore, it is possible for a key to be in multiple states. 例如,键可以处于按下状态,也可以处于切换状态。For example, a key could be in the pressed state as well as in the toggled state. 使用位比较操作来确定密钥所在的确切状态或状态。Use bit comparison operations to determine the exact state or states the key is in.

Keyboard 类提供了许多静态方法,这些方法还可用于获取密钥状态信息。The Keyboard class provides a number of static methods which can also be used to obtain key state information. 方法为: IsKeyDownIsKeyUpIsKeyToggledThe methods are: IsKeyDown, IsKeyUp, and IsKeyToggled.

适用于

另请参阅