Export (0) Print
Expand All

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.

  NameDescription
Public propertyStatic memberDefaultRestoreFocusModeGets or sets the behavior of Windows Presentation Foundation (WPF) when restoring focus.
Public propertyStatic memberFocusedElementGets the element that has keyboard focus.
Public propertyStatic memberModifiersGets the set of ModifierKeys that are currently pressed.
Public propertyStatic memberPrimaryDeviceGets the primary keyboard input device.
Top

  NameDescription
Public methodStatic memberAddGotKeyboardFocusHandlerAdds a handler for the Keyboard.GotKeyboardFocus attached event.
Public methodStatic memberAddKeyboardInputProviderAcquireFocusHandlerAdds a handler for the Keyboard.KeyboardInputProviderAcquireFocus attached event.
Public methodStatic memberAddKeyDownHandlerAdds a handler for the Keyboard.KeyDown attached event.
Public methodStatic memberAddKeyUpHandlerAdds a handler for the Keyboard.KeyUp attached event.
Public methodStatic memberAddLostKeyboardFocusHandlerAdds a handler for the Keyboard.LostKeyboardFocus attached event.
Public methodStatic memberAddPreviewGotKeyboardFocusHandlerAdds a handler for the Keyboard.PreviewGotKeyboardFocus attached event.
Public methodStatic memberAddPreviewKeyboardInputProviderAcquireFocusHandlerAdds a handler for the Keyboard.PreviewKeyboardInputProviderAcquireFocus attached event.
Public methodStatic memberAddPreviewKeyDownHandlerAdds a handler for the Keyboard.PreviewKeyDown attached event.
Public methodStatic memberAddPreviewKeyUpHandlerAdds a handler for the Keyboard.PreviewKeyUp attached event.
Public methodStatic memberAddPreviewLostKeyboardFocusHandlerAdds a handler for the Keyboard.PreviewLostKeyboardFocus attached event.
Public methodStatic memberClearFocusClears focus.
Public methodStatic memberFocusSets keyboard focus on the specified element.
Public methodStatic memberGetKeyStatesGets the set of key states for the specified key.
Public methodStatic memberIsKeyDownDetermines whether the specified key is pressed.
Public methodStatic memberIsKeyToggledDetermines whether the specified key is toggled.
Public methodStatic memberIsKeyUpDetermines whether the specified key is released.
Public methodStatic memberRemoveGotKeyboardFocusHandlerRemoves a handler for the Keyboard.GotKeyboardFocus attached event.
Public methodStatic memberRemoveKeyboardInputProviderAcquireFocusHandlerRemoves a handler for the Keyboard.KeyboardInputProviderAcquireFocus attached event.
Public methodStatic memberRemoveKeyDownHandlerRemoves a handler for the Keyboard.KeyDown attached event.
Public methodStatic memberRemoveKeyUpHandlerRemoves a handler for the Keyboard.KeyUp attached event.
Public methodStatic memberRemoveLostKeyboardFocusHandlerRemoves a handler for the Keyboard.LostKeyboardFocus attached event.
Public methodStatic memberRemovePreviewGotKeyboardFocusHandlerRemoves a handler for the Keyboard.PreviewGotKeyboardFocus attached event.
Public methodStatic memberRemovePreviewKeyboardInputProviderAcquireFocusHandlerRemoves a handler for the Keyboard.PreviewKeyboardInputProviderAcquireFocus attached event.
Public methodStatic memberRemovePreviewKeyDownHandlerRemoves a handler for the Keyboard.PreviewKeyDown attached event.
Public methodStatic memberRemovePreviewKeyUpHandlerRemoves a handler for the Keyboard.PreviewKeyUp attached event.
Public methodStatic memberRemovePreviewLostKeyboardFocusHandlerRemoves a handler for the Keyboard.PreviewLostKeyboardFocus attached event.
Top

  NameDescription
Public attached eventGotKeyboardFocusOccurs when an element receives keyboard focus.
Public attached eventKeyboardInputProviderAcquireFocusOccurs when the keyboard input provider acquires focus.
Public attached eventKeyDownOccurs when a key on the keyboard is pressed.
Public attached eventKeyUpOccurs when a key on the keyboard is released.
Public attached eventLostKeyboardFocusOccurs when an element loses keyboard focus.
Public attached eventPreviewGotKeyboardFocusOccurs when an element is in the process of acquiring keyboard focus.
Public attached eventPreviewKeyboardInputProviderAcquireFocusOccurs when the keyboard input provider is in the process of acquiring focus.
Public attached eventPreviewKeyDownOccurs when a key on the keyboard is pressed.
Public attached eventPreviewKeyUpOccurs when a key on the keyboard is released.
Public attached eventPreviewLostKeyboardFocusOccurs when an element is in the process of losing keyboard focus.
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.5.3, 4, 3.5, 3.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

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.
Show:
© 2014 Microsoft