Note

Access to this page requires authorization. You can try signing in or .

Access to this page requires authorization. You can try .

Keyboard.GetKeyStates(Key) Method

Definition

Namespace:
System.Windows.Input
Assembly:
PresentationCore.dll

Important

Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.

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
Public Shared Function GetKeyStates (key As Key) As KeyStates

Parameters

key
Key

The specified key.

Returns

A bitwise combination of the KeyStates values.

Examples

The following example shows how to use the GetKeyStates method to determine if the Return key is in the Down state. A bit AND operation is used to compare the KeyStates returned from GetKeyStates and the Down state. 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

Remarks

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.

The Keyboard class provides a number of static methods which can also be used to obtain key state information. The methods are: IsKeyDown, IsKeyUp, and IsKeyToggled.

Applies to

See also


Feedback

Was this page helpful?