This topic has not yet been rated - Rate this topic

Keyboard Class

Represents the keyboard device.

Namespace:  System.Windows.Input
Assembly:  PresentationCore (in PresentationCore.dll)
public final class Keyboard
You cannot directly create an instance of this class in XAML.

The Keyboard class provides keyboard-related events, methods, and properties that provide information regarding the state of the keyboard.

Each of the events that Keyboard defines as an attached event is also re-exposed by the base element classes UIElement and ContentElement as a new routed event. Generally, it is more convenient to handle keyboard events for an application on UIElement and ContentElement, rather than using the Keyboard events. For details, see Input Overview.

In order for an element to receive keyboard input, the element must be focusable. Most UIElement derived objects are focusable by default. Otherwise, to make an element focusable, set the Focusable property on the base element to true. For more information on the base elements, see Base Elements Overview.

Panel classes, such as StackPanel and Canvas, set the default value of Focusable to false. Therefore, for these objects to obtain keyboard focus, Focusable must be set to true.

Keyboard focus refers to the object that is receiving keyboard input. The element with keyboard focus has IsKeyboardFocused set to true. There can be only one element with keyboard focus on the entire desktop. Logical focus refers to the object within a focus scope that has focus. For more information on focus, keyboard focus, and logical focus, see Input Overview and Focus Overview.

The static members of the Keyboard class delegate to the primary KeyboardDevice of the calling thread, so they are not necessarily thread-safe.

The following example shows how to use the IsKeyToggled method to determine if a key is in the toggled state. If the Key passed to IsKeyToggled is toggled, the background of a button is changed.

// Uses the Keyboard.IsToggled to determine if a key is toggled. 
if (Keyboard.IsKeyToggled(Key.Return))
{
    btnIsToggle.Background = Brushes.Red;
}
else
{
    btnIsToggle.Background = Brushes.AliceBlue;
}
System.Object
  System.Windows.Input.Keyboard
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 7, Windows Vista, Windows XP SP2, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003

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

.NET Framework

Supported in: 3.5, 3.0
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.