Keyboard.GetKeyStates Method

Gets the set of key states for the specified key.

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

public static KeyStates GetKeyStates(
	Key key
)

Parameters

key
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.

// 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;
}

.NET Framework

Supported in: 4.5, 4, 3.5, 3.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft