Keyboard.GetKeyStates Method (Key)


The .NET API Reference documentation has a new home. Visit the .NET API Browser on to see the new experience.

Gets the set of key states for the specified key.

Namespace:   System.Windows.Input
Assembly:  PresentationCore (in PresentationCore.dll)

static member GetKeyStates : 
        key:Key -> KeyStates


Type: System.Windows.Input.Key

The specified key.

Return Value

Type: System.Windows.Input.KeyStates

A bitwise combination of the KeyStates values.

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.

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.

No code example is currently available or this language may not be supported.

.NET Framework
Available since 3.0
Return to top