Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

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
)
public static KeyStates GetKeyStates (
	Key key
)
public static function GetKeyStates (
	key : Key
) : KeyStates
You cannot use methods in XAML.

Parameters

key

The specified key.

Return Value

The set of key states.

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

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

Show:
© 2015 Microsoft