Keyboard Keyboard Keyboard Keyboard Class

Definition

Represents the keyboard device.

public ref class Keyboard abstract sealed
public static class Keyboard
type Keyboard = class
Public Class Keyboard
Inheritance
KeyboardKeyboardKeyboardKeyboard

Examples

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;
}
' Uses the Keyboard.IsToggled to determine if a key is toggled.
If Keyboard.IsKeyToggled(Key.Return) Then
	btnIsToggle.Background = Brushes.Red
Else
	btnIsToggle.Background = Brushes.AliceBlue
End If

Remarks

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.

Fields

GotKeyboardFocusEvent GotKeyboardFocusEvent GotKeyboardFocusEvent GotKeyboardFocusEvent

Identifies the GotKeyboardFocus attached event.

KeyboardInputProviderAcquireFocusEvent KeyboardInputProviderAcquireFocusEvent KeyboardInputProviderAcquireFocusEvent KeyboardInputProviderAcquireFocusEvent

Identifies the KeyboardInputProviderAcquireFocus attached event.

KeyDownEvent KeyDownEvent KeyDownEvent KeyDownEvent

Identifies the KeyDown attached event.

KeyUpEvent KeyUpEvent KeyUpEvent KeyUpEvent

Identifies the KeyUp attached event.

LostKeyboardFocusEvent LostKeyboardFocusEvent LostKeyboardFocusEvent LostKeyboardFocusEvent

Identifies the LostKeyboardFocus attached event.

PreviewGotKeyboardFocusEvent PreviewGotKeyboardFocusEvent PreviewGotKeyboardFocusEvent PreviewGotKeyboardFocusEvent

Identifies the PreviewGotKeyboardFocus attached event.

PreviewKeyboardInputProviderAcquireFocusEvent PreviewKeyboardInputProviderAcquireFocusEvent PreviewKeyboardInputProviderAcquireFocusEvent PreviewKeyboardInputProviderAcquireFocusEvent

Identifies the PreviewKeyboardInputProviderAcquireFocus attached event.

PreviewKeyDownEvent PreviewKeyDownEvent PreviewKeyDownEvent PreviewKeyDownEvent

Identifies the PreviewKeyDown attached event.

PreviewKeyUpEvent PreviewKeyUpEvent PreviewKeyUpEvent PreviewKeyUpEvent

Identifies the PreviewKeyUp attached event.

PreviewLostKeyboardFocusEvent PreviewLostKeyboardFocusEvent PreviewLostKeyboardFocusEvent PreviewLostKeyboardFocusEvent

Identifies the PreviewLostKeyboardFocus attached event.

Properties

DefaultRestoreFocusMode DefaultRestoreFocusMode DefaultRestoreFocusMode DefaultRestoreFocusMode

Gets or sets the behavior of Windows Presentation Foundation (WPF) when restoring focus.

FocusedElement FocusedElement FocusedElement FocusedElement

Gets the element that has keyboard focus.

Modifiers Modifiers Modifiers Modifiers

Gets the set of ModifierKeys that are currently pressed.

PrimaryDevice PrimaryDevice PrimaryDevice PrimaryDevice

Gets the primary keyboard input device.

Methods

AddGotKeyboardFocusHandler(DependencyObject, KeyboardFocusChangedEventHandler) AddGotKeyboardFocusHandler(DependencyObject, KeyboardFocusChangedEventHandler) AddGotKeyboardFocusHandler(DependencyObject, KeyboardFocusChangedEventHandler) AddGotKeyboardFocusHandler(DependencyObject, KeyboardFocusChangedEventHandler)

Adds a handler for the GotKeyboardFocus attached event.

AddKeyboardInputProviderAcquireFocusHandler(DependencyObject, KeyboardInputProviderAcquireFocusEventHandler) AddKeyboardInputProviderAcquireFocusHandler(DependencyObject, KeyboardInputProviderAcquireFocusEventHandler) AddKeyboardInputProviderAcquireFocusHandler(DependencyObject, KeyboardInputProviderAcquireFocusEventHandler) AddKeyboardInputProviderAcquireFocusHandler(DependencyObject, KeyboardInputProviderAcquireFocusEventHandler)

Adds a handler for the KeyboardInputProviderAcquireFocus attached event.

AddKeyDownHandler(DependencyObject, KeyEventHandler) AddKeyDownHandler(DependencyObject, KeyEventHandler) AddKeyDownHandler(DependencyObject, KeyEventHandler) AddKeyDownHandler(DependencyObject, KeyEventHandler)

Adds a handler for the KeyDown attached event.

AddKeyUpHandler(DependencyObject, KeyEventHandler) AddKeyUpHandler(DependencyObject, KeyEventHandler) AddKeyUpHandler(DependencyObject, KeyEventHandler) AddKeyUpHandler(DependencyObject, KeyEventHandler)

Adds a handler for the KeyUp attached event.

AddLostKeyboardFocusHandler(DependencyObject, KeyboardFocusChangedEventHandler) AddLostKeyboardFocusHandler(DependencyObject, KeyboardFocusChangedEventHandler) AddLostKeyboardFocusHandler(DependencyObject, KeyboardFocusChangedEventHandler) AddLostKeyboardFocusHandler(DependencyObject, KeyboardFocusChangedEventHandler)

Adds a handler for the LostKeyboardFocus attached event.

AddPreviewGotKeyboardFocusHandler(DependencyObject, KeyboardFocusChangedEventHandler) AddPreviewGotKeyboardFocusHandler(DependencyObject, KeyboardFocusChangedEventHandler) AddPreviewGotKeyboardFocusHandler(DependencyObject, KeyboardFocusChangedEventHandler) AddPreviewGotKeyboardFocusHandler(DependencyObject, KeyboardFocusChangedEventHandler)

Adds a handler for the PreviewGotKeyboardFocus attached event.

AddPreviewKeyboardInputProviderAcquireFocusHandler(DependencyObject, KeyboardInputProviderAcquireFocusEventHandler) AddPreviewKeyboardInputProviderAcquireFocusHandler(DependencyObject, KeyboardInputProviderAcquireFocusEventHandler) AddPreviewKeyboardInputProviderAcquireFocusHandler(DependencyObject, KeyboardInputProviderAcquireFocusEventHandler) AddPreviewKeyboardInputProviderAcquireFocusHandler(DependencyObject, KeyboardInputProviderAcquireFocusEventHandler)

Adds a handler for the PreviewKeyboardInputProviderAcquireFocus attached event.

AddPreviewKeyDownHandler(DependencyObject, KeyEventHandler) AddPreviewKeyDownHandler(DependencyObject, KeyEventHandler) AddPreviewKeyDownHandler(DependencyObject, KeyEventHandler) AddPreviewKeyDownHandler(DependencyObject, KeyEventHandler)

Adds a handler for the PreviewKeyDown attached event.

AddPreviewKeyUpHandler(DependencyObject, KeyEventHandler) AddPreviewKeyUpHandler(DependencyObject, KeyEventHandler) AddPreviewKeyUpHandler(DependencyObject, KeyEventHandler) AddPreviewKeyUpHandler(DependencyObject, KeyEventHandler)

Adds a handler for the PreviewKeyUp attached event.

AddPreviewLostKeyboardFocusHandler(DependencyObject, KeyboardFocusChangedEventHandler) AddPreviewLostKeyboardFocusHandler(DependencyObject, KeyboardFocusChangedEventHandler) AddPreviewLostKeyboardFocusHandler(DependencyObject, KeyboardFocusChangedEventHandler) AddPreviewLostKeyboardFocusHandler(DependencyObject, KeyboardFocusChangedEventHandler)

Adds a handler for the PreviewLostKeyboardFocus attached event.

ClearFocus() ClearFocus() ClearFocus() ClearFocus()

Clears focus.

Focus(IInputElement) Focus(IInputElement) Focus(IInputElement) Focus(IInputElement)

Sets keyboard focus on the specified element.

GetKeyStates(Key) GetKeyStates(Key) GetKeyStates(Key) GetKeyStates(Key)

Gets the set of key states for the specified key.

IsKeyDown(Key) IsKeyDown(Key) IsKeyDown(Key) IsKeyDown(Key)

Determines whether the specified key is pressed.

IsKeyToggled(Key) IsKeyToggled(Key) IsKeyToggled(Key) IsKeyToggled(Key)

Determines whether the specified key is toggled.

IsKeyUp(Key) IsKeyUp(Key) IsKeyUp(Key) IsKeyUp(Key)

Determines whether the specified key is released.

RemoveGotKeyboardFocusHandler(DependencyObject, KeyboardFocusChangedEventHandler) RemoveGotKeyboardFocusHandler(DependencyObject, KeyboardFocusChangedEventHandler) RemoveGotKeyboardFocusHandler(DependencyObject, KeyboardFocusChangedEventHandler) RemoveGotKeyboardFocusHandler(DependencyObject, KeyboardFocusChangedEventHandler)

Removes a handler for the GotKeyboardFocus attached event.

RemoveKeyboardInputProviderAcquireFocusHandler(DependencyObject, KeyboardInputProviderAcquireFocusEventHandler) RemoveKeyboardInputProviderAcquireFocusHandler(DependencyObject, KeyboardInputProviderAcquireFocusEventHandler) RemoveKeyboardInputProviderAcquireFocusHandler(DependencyObject, KeyboardInputProviderAcquireFocusEventHandler) RemoveKeyboardInputProviderAcquireFocusHandler(DependencyObject, KeyboardInputProviderAcquireFocusEventHandler)

Removes a handler for the KeyboardInputProviderAcquireFocus attached event.

RemoveKeyDownHandler(DependencyObject, KeyEventHandler) RemoveKeyDownHandler(DependencyObject, KeyEventHandler) RemoveKeyDownHandler(DependencyObject, KeyEventHandler) RemoveKeyDownHandler(DependencyObject, KeyEventHandler)

Removes a handler for the KeyDown attached event.

RemoveKeyUpHandler(DependencyObject, KeyEventHandler) RemoveKeyUpHandler(DependencyObject, KeyEventHandler) RemoveKeyUpHandler(DependencyObject, KeyEventHandler) RemoveKeyUpHandler(DependencyObject, KeyEventHandler)

Removes a handler for the KeyUp attached event.

RemoveLostKeyboardFocusHandler(DependencyObject, KeyboardFocusChangedEventHandler) RemoveLostKeyboardFocusHandler(DependencyObject, KeyboardFocusChangedEventHandler) RemoveLostKeyboardFocusHandler(DependencyObject, KeyboardFocusChangedEventHandler) RemoveLostKeyboardFocusHandler(DependencyObject, KeyboardFocusChangedEventHandler)

Removes a handler for the LostKeyboardFocus attached event.

RemovePreviewGotKeyboardFocusHandler(DependencyObject, KeyboardFocusChangedEventHandler) RemovePreviewGotKeyboardFocusHandler(DependencyObject, KeyboardFocusChangedEventHandler) RemovePreviewGotKeyboardFocusHandler(DependencyObject, KeyboardFocusChangedEventHandler) RemovePreviewGotKeyboardFocusHandler(DependencyObject, KeyboardFocusChangedEventHandler)

Removes a handler for the PreviewGotKeyboardFocus attached event.

RemovePreviewKeyboardInputProviderAcquireFocusHandler(DependencyObject, KeyboardInputProviderAcquireFocusEventHandler) RemovePreviewKeyboardInputProviderAcquireFocusHandler(DependencyObject, KeyboardInputProviderAcquireFocusEventHandler) RemovePreviewKeyboardInputProviderAcquireFocusHandler(DependencyObject, KeyboardInputProviderAcquireFocusEventHandler) RemovePreviewKeyboardInputProviderAcquireFocusHandler(DependencyObject, KeyboardInputProviderAcquireFocusEventHandler)

Removes a handler for the PreviewKeyboardInputProviderAcquireFocus attached event.

RemovePreviewKeyDownHandler(DependencyObject, KeyEventHandler) RemovePreviewKeyDownHandler(DependencyObject, KeyEventHandler) RemovePreviewKeyDownHandler(DependencyObject, KeyEventHandler) RemovePreviewKeyDownHandler(DependencyObject, KeyEventHandler)

Removes a handler for the PreviewKeyDown attached event.

RemovePreviewKeyUpHandler(DependencyObject, KeyEventHandler) RemovePreviewKeyUpHandler(DependencyObject, KeyEventHandler) RemovePreviewKeyUpHandler(DependencyObject, KeyEventHandler) RemovePreviewKeyUpHandler(DependencyObject, KeyEventHandler)

Removes a handler for the PreviewKeyUp attached event.

RemovePreviewLostKeyboardFocusHandler(DependencyObject, KeyboardFocusChangedEventHandler) RemovePreviewLostKeyboardFocusHandler(DependencyObject, KeyboardFocusChangedEventHandler) RemovePreviewLostKeyboardFocusHandler(DependencyObject, KeyboardFocusChangedEventHandler) RemovePreviewLostKeyboardFocusHandler(DependencyObject, KeyboardFocusChangedEventHandler)

Removes a handler for the PreviewLostKeyboardFocus attached event.

Attached Events

GotKeyboardFocus GotKeyboardFocus GotKeyboardFocus GotKeyboardFocus

Occurs when an element receives keyboard focus.

KeyboardInputProviderAcquireFocus KeyboardInputProviderAcquireFocus KeyboardInputProviderAcquireFocus KeyboardInputProviderAcquireFocus

Occurs when the keyboard input provider acquires focus.

KeyDown KeyDown KeyDown KeyDown

Occurs when a key on the keyboard is pressed.

KeyUp KeyUp KeyUp KeyUp

Occurs when a key on the keyboard is released.

LostKeyboardFocus LostKeyboardFocus LostKeyboardFocus LostKeyboardFocus

Occurs when an element loses keyboard focus.

PreviewGotKeyboardFocus PreviewGotKeyboardFocus PreviewGotKeyboardFocus PreviewGotKeyboardFocus

Occurs when an element is in the process of acquiring keyboard focus.

PreviewKeyboardInputProviderAcquireFocus PreviewKeyboardInputProviderAcquireFocus PreviewKeyboardInputProviderAcquireFocus PreviewKeyboardInputProviderAcquireFocus

Occurs when the keyboard input provider is in the process of acquiring focus.

PreviewKeyDown PreviewKeyDown PreviewKeyDown PreviewKeyDown

Occurs when a key on the keyboard is pressed.

PreviewKeyUp PreviewKeyUp PreviewKeyUp PreviewKeyUp

Occurs when a key on the keyboard is released.

PreviewLostKeyboardFocus PreviewLostKeyboardFocus PreviewLostKeyboardFocus PreviewLostKeyboardFocus

Occurs when an element is in the process of losing keyboard focus.

Applies to

See also