2 out of 3 rated this helpful - Rate this topic

Keyboard Class

Represents the keyboard device.

System.Object
  System.Windows.Input.Keyboard

Namespace:  System.Windows.Input
Assembly:  PresentationCore (in PresentationCore.dll)
public static class Keyboard

The Keyboard type exposes the following members.

  Name Description
Public property Static member DefaultRestoreFocusMode Gets or sets the behavior of Windows Presentation Foundation (WPF) when restoring focus.
Public property Static member FocusedElement Gets the element that has keyboard focus.
Public property Static member Modifiers Gets the set of ModifierKeys that are currently pressed.
Public property Static member PrimaryDevice Gets the primary keyboard input device.
Top
  Name Description
Public method Static member AddGotKeyboardFocusHandler Adds a handler for the Keyboard.GotKeyboardFocus attached event.
Public method Static member AddKeyboardInputProviderAcquireFocusHandler Adds a handler for the Keyboard.KeyboardInputProviderAcquireFocus attached event.
Public method Static member AddKeyDownHandler Adds a handler for the Keyboard.KeyDown attached event.
Public method Static member AddKeyUpHandler Adds a handler for the Keyboard.KeyUp attached event.
Public method Static member AddLostKeyboardFocusHandler Adds a handler for the Keyboard.LostKeyboardFocus attached event.
Public method Static member AddPreviewGotKeyboardFocusHandler Adds a handler for the Keyboard.PreviewGotKeyboardFocus attached event.
Public method Static member AddPreviewKeyboardInputProviderAcquireFocusHandler Adds a handler for the Keyboard.PreviewKeyboardInputProviderAcquireFocus attached event.
Public method Static member AddPreviewKeyDownHandler Adds a handler for the Keyboard.PreviewKeyDown attached event.
Public method Static member AddPreviewKeyUpHandler Adds a handler for the Keyboard.PreviewKeyUp attached event.
Public method Static member AddPreviewLostKeyboardFocusHandler Adds a handler for the Keyboard.PreviewLostKeyboardFocus attached event.
Public method Static member ClearFocus Clears focus.
Public method Static member Focus Sets keyboard focus on the specified element.
Public method Static member GetKeyStates Gets the set of key states for the specified key.
Public method Static member IsKeyDown Determines whether the specified key is pressed.
Public method Static member IsKeyToggled Determines whether the specified key is toggled.
Public method Static member IsKeyUp Determines whether the specified key is released.
Public method Static member RemoveGotKeyboardFocusHandler Removes a handler for the Keyboard.GotKeyboardFocus attached event.
Public method Static member RemoveKeyboardInputProviderAcquireFocusHandler Removes a handler for the Keyboard.KeyboardInputProviderAcquireFocus attached event.
Public method Static member RemoveKeyDownHandler Removes a handler for the Keyboard.KeyDown attached event.
Public method Static member RemoveKeyUpHandler Removes a handler for the Keyboard.KeyUp attached event.
Public method Static member RemoveLostKeyboardFocusHandler Removes a handler for the Keyboard.LostKeyboardFocus attached event.
Public method Static member RemovePreviewGotKeyboardFocusHandler Removes a handler for the Keyboard.PreviewGotKeyboardFocus attached event.
Public method Static member RemovePreviewKeyboardInputProviderAcquireFocusHandler Removes a handler for the Keyboard.PreviewKeyboardInputProviderAcquireFocus attached event.
Public method Static member RemovePreviewKeyDownHandler Removes a handler for the Keyboard.PreviewKeyDown attached event.
Public method Static member RemovePreviewKeyUpHandler Removes a handler for the Keyboard.PreviewKeyUp attached event.
Public method Static member RemovePreviewLostKeyboardFocusHandler Removes a handler for the Keyboard.PreviewLostKeyboardFocus attached event.
Top
  Name Description
Public attached event GotKeyboardFocus Occurs when an element receives keyboard focus.
Public attached event KeyboardInputProviderAcquireFocus Occurs when the keyboard input provider acquires focus.
Public attached event KeyDown Occurs when a key on the keyboard is pressed.
Public attached event KeyUp Occurs when a key on the keyboard is released.
Public attached event LostKeyboardFocus Occurs when an element loses keyboard focus.
Public attached event PreviewGotKeyboardFocus Occurs when an element is in the process of acquiring keyboard focus.
Public attached event PreviewKeyboardInputProviderAcquireFocus Occurs when the keyboard input provider is in the process of acquiring focus.
Public attached event PreviewKeyDown Occurs when a key on the keyboard is pressed.
Public attached event PreviewKeyUp Occurs when a key on the keyboard is released.
Public attached event PreviewLostKeyboardFocus Occurs when an element is in the process of losing keyboard focus.
Top
  Name Description
Public field Static member GotKeyboardFocusEvent Identifies the Keyboard.GotKeyboardFocus attached event.
Public field Static member KeyboardInputProviderAcquireFocusEvent Identifies the Keyboard.KeyboardInputProviderAcquireFocus attached event.
Public field Static member KeyDownEvent Identifies the Keyboard.KeyDown attached event.
Public field Static member KeyUpEvent Identifies the Keyboard.KeyUp attached event.
Public field Static member LostKeyboardFocusEvent Identifies the Keyboard.LostKeyboardFocus attached event.
Public field Static member PreviewGotKeyboardFocusEvent Identifies the Keyboard.PreviewGotKeyboardFocus attached event.
Public field Static member PreviewKeyboardInputProviderAcquireFocusEvent Identifies the Keyboard.PreviewKeyboardInputProviderAcquireFocus attached event.
Public field Static member PreviewKeyDownEvent Identifies the Keyboard.PreviewKeyDown attached event.
Public field Static member PreviewKeyUpEvent Identifies the Keyboard.PreviewKeyUp attached event.
Public field Static member PreviewLostKeyboardFocusEvent Identifies the Keyboard.PreviewLostKeyboardFocus attached event.
Top

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


.NET Framework

Supported in: 4, 3.5, 3.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ