Keyboard Class

 

Represents the keyboard device.

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

System.Object
  System.Windows.Input.Keyboard

public static class Keyboard

NameDescription
System_CAPS_pubpropertySystem_CAPS_staticDefaultRestoreFocusMode

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

System_CAPS_pubpropertySystem_CAPS_staticFocusedElement

Gets the element that has keyboard focus.

System_CAPS_pubpropertySystem_CAPS_staticModifiers

Gets the set of ModifierKeys that are currently pressed.

System_CAPS_pubpropertySystem_CAPS_staticPrimaryDevice

Gets the primary keyboard input device.

NameDescription
System_CAPS_pubmethodSystem_CAPS_staticAddGotKeyboardFocusHandler(DependencyObject, KeyboardFocusChangedEventHandler)

Adds a handler for the Keyboard.GotKeyboardFocus attached event.

System_CAPS_pubmethodSystem_CAPS_staticAddKeyboardInputProviderAcquireFocusHandler(DependencyObject, KeyboardInputProviderAcquireFocusEventHandler)

Adds a handler for the Keyboard.KeyboardInputProviderAcquireFocus attached event.

System_CAPS_pubmethodSystem_CAPS_staticAddKeyDownHandler(DependencyObject, KeyEventHandler)

Adds a handler for the Keyboard.KeyDown attached event.

System_CAPS_pubmethodSystem_CAPS_staticAddKeyUpHandler(DependencyObject, KeyEventHandler)

Adds a handler for the Keyboard.KeyUp attached event.

System_CAPS_pubmethodSystem_CAPS_staticAddLostKeyboardFocusHandler(DependencyObject, KeyboardFocusChangedEventHandler)

Adds a handler for the Keyboard.LostKeyboardFocus attached event.

System_CAPS_pubmethodSystem_CAPS_staticAddPreviewGotKeyboardFocusHandler(DependencyObject, KeyboardFocusChangedEventHandler)

Adds a handler for the Keyboard.PreviewGotKeyboardFocus attached event.

System_CAPS_pubmethodSystem_CAPS_staticAddPreviewKeyboardInputProviderAcquireFocusHandler(DependencyObject, KeyboardInputProviderAcquireFocusEventHandler)

Adds a handler for the Keyboard.PreviewKeyboardInputProviderAcquireFocus attached event.

System_CAPS_pubmethodSystem_CAPS_staticAddPreviewKeyDownHandler(DependencyObject, KeyEventHandler)

Adds a handler for the Keyboard.PreviewKeyDown attached event.

System_CAPS_pubmethodSystem_CAPS_staticAddPreviewKeyUpHandler(DependencyObject, KeyEventHandler)

Adds a handler for the Keyboard.PreviewKeyUp attached event.

System_CAPS_pubmethodSystem_CAPS_staticAddPreviewLostKeyboardFocusHandler(DependencyObject, KeyboardFocusChangedEventHandler)

Adds a handler for the Keyboard.PreviewLostKeyboardFocus attached event.

System_CAPS_pubmethodSystem_CAPS_staticClearFocus()

Clears focus.

System_CAPS_pubmethodSystem_CAPS_staticFocus(IInputElement)

Sets keyboard focus on the specified element.

System_CAPS_pubmethodSystem_CAPS_staticGetKeyStates(Key)

Gets the set of key states for the specified key.

System_CAPS_pubmethodSystem_CAPS_staticIsKeyDown(Key)

Determines whether the specified key is pressed.

System_CAPS_pubmethodSystem_CAPS_staticIsKeyToggled(Key)

Determines whether the specified key is toggled.

System_CAPS_pubmethodSystem_CAPS_staticIsKeyUp(Key)

Determines whether the specified key is released.

System_CAPS_pubmethodSystem_CAPS_staticRemoveGotKeyboardFocusHandler(DependencyObject, KeyboardFocusChangedEventHandler)

Removes a handler for the Keyboard.GotKeyboardFocus attached event.

System_CAPS_pubmethodSystem_CAPS_staticRemoveKeyboardInputProviderAcquireFocusHandler(DependencyObject, KeyboardInputProviderAcquireFocusEventHandler)

Removes a handler for the Keyboard.KeyboardInputProviderAcquireFocus attached event.

System_CAPS_pubmethodSystem_CAPS_staticRemoveKeyDownHandler(DependencyObject, KeyEventHandler)

Removes a handler for the Keyboard.KeyDown attached event.

System_CAPS_pubmethodSystem_CAPS_staticRemoveKeyUpHandler(DependencyObject, KeyEventHandler)

Removes a handler for the Keyboard.KeyUp attached event.

System_CAPS_pubmethodSystem_CAPS_staticRemoveLostKeyboardFocusHandler(DependencyObject, KeyboardFocusChangedEventHandler)

Removes a handler for the Keyboard.LostKeyboardFocus attached event.

System_CAPS_pubmethodSystem_CAPS_staticRemovePreviewGotKeyboardFocusHandler(DependencyObject, KeyboardFocusChangedEventHandler)

Removes a handler for the Keyboard.PreviewGotKeyboardFocus attached event.

System_CAPS_pubmethodSystem_CAPS_staticRemovePreviewKeyboardInputProviderAcquireFocusHandler(DependencyObject, KeyboardInputProviderAcquireFocusEventHandler)

Removes a handler for the Keyboard.PreviewKeyboardInputProviderAcquireFocus attached event.

System_CAPS_pubmethodSystem_CAPS_staticRemovePreviewKeyDownHandler(DependencyObject, KeyEventHandler)

Removes a handler for the Keyboard.PreviewKeyDown attached event.

System_CAPS_pubmethodSystem_CAPS_staticRemovePreviewKeyUpHandler(DependencyObject, KeyEventHandler)

Removes a handler for the Keyboard.PreviewKeyUp attached event.

System_CAPS_pubmethodSystem_CAPS_staticRemovePreviewLostKeyboardFocusHandler(DependencyObject, KeyboardFocusChangedEventHandler)

Removes a handler for the Keyboard.PreviewLostKeyboardFocus attached event.

NameDescription
System_CAPS_pubeventSystem_CAPS_staticGotKeyboardFocus

Occurs when an element receives keyboard focus.

System_CAPS_pubeventSystem_CAPS_staticKeyboardInputProviderAcquireFocus

Occurs when the keyboard input provider acquires focus.

System_CAPS_pubeventSystem_CAPS_staticKeyDown

Occurs when a key on the keyboard is pressed.

System_CAPS_pubeventSystem_CAPS_staticKeyUp

Occurs when a key on the keyboard is released.

System_CAPS_pubeventSystem_CAPS_staticLostKeyboardFocus

Occurs when an element loses keyboard focus.

System_CAPS_pubeventSystem_CAPS_staticPreviewGotKeyboardFocus

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

System_CAPS_pubeventSystem_CAPS_staticPreviewKeyboardInputProviderAcquireFocus

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

System_CAPS_pubeventSystem_CAPS_staticPreviewKeyDown

Occurs when a key on the keyboard is pressed.

System_CAPS_pubeventSystem_CAPS_staticPreviewKeyUp

Occurs when a key on the keyboard is released.

System_CAPS_pubeventSystem_CAPS_staticPreviewLostKeyboardFocus

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

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
Available since 3.0
Silverlight
Available since 2.0
Windows Phone Silverlight
Available since 7.0

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Return to top
Show: