Keyboard.IsKeyDown(Key) 方法

定义

确定指定的键是否处于按下状态。

public:
 static bool IsKeyDown(System::Windows::Input::Key key);
public static bool IsKeyDown (System.Windows.Input.Key key);
static member IsKeyDown : System.Windows.Input.Key -> bool
Public Shared Function IsKeyDown (key As Key) As Boolean

参数

key
Key

指定的键。

返回

如果 key 处于按下状态,则为 true;否则为 false

示例

以下示例演示如何使用 IsKeyDown 方法确定特定键的状态。 密钥 Return 将传递给 IsKeyDown 方法。 如果方法返回 true,则会更改 的背景 Button

// Uses the Keyboard.IsKeyDown to determine if a key is down.
// e is an instance of KeyEventArgs.
if (Keyboard.IsKeyDown(Key.Return))
{
    btnIsDown.Background = Brushes.Red;
}
else
{
    btnIsDown.Background = Brushes.AliceBlue;
}
' Uses the Keyboard.IsKeyDown to determine if a key is down.
' e is an instance of KeyEventArgs.
If Keyboard.IsKeyDown(Key.Return) Then
    btnIsDown.Background = Brushes.Red
Else
    btnIsDown.Background = Brushes.AliceBlue
End If

注解

方法 GetKeyStates 可用于确定特定键的状态集。

适用于

另请参阅