Keyboard 类

定义

表示键盘设备。Represents the keyboard device.

public ref class Keyboard abstract sealed
public static class Keyboard
type Keyboard = class
Public Class Keyboard
继承
Keyboard

示例

下面的示例演示如何使用 IsKeyToggled 方法来确定某个键是否处于切换状态。The following example shows how to use the IsKeyToggled method to determine if a key is in the toggled state. 如果传递到 IsKeyToggledKey 切换,则会更改按钮的背景。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

注解

Keyboard 类提供与键盘相关的事件、方法和属性,这些事件、方法和属性提供有关键盘状态的信息。The Keyboard class provides keyboard-related events, methods, and properties that provide information regarding the state of the keyboard.

Keyboard 定义为附加事件的每个事件也会由基元素类重新公开 UIElementContentElement 作为新的路由事件。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. 通常,可以更方便地处理 UIElementContentElement上的应用程序的键盘事件,而不是使用 Keyboard 事件。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. 默认情况下,大多数 UIElement 派生对象均可获得焦点。Most UIElement derived objects are focusable by default. 否则,若要使元素可设定焦点,请将基元素上的 Focusable 属性设置为 trueOtherwise, 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 类,如 StackPanelCanvas,则将 Focusable 的默认值设置为 falsePanel classes, such as StackPanel and Canvas, set the default value of Focusable to false. 因此,要使这些对象获取键盘焦点,Focusable 必须设置为 trueTherefore, for these objects to obtain keyboard focus, Focusable must be set to true.

键盘焦点指的是接收键盘输入的对象。Keyboard focus refers to the object that is receiving keyboard input. 具有键盘焦点的元素已将 IsKeyboardFocused 设置为 trueThe 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.

Keyboard 类的静态成员委托给调用线程的主 KeyboardDevice,因此它们不一定是线程安全的。The static members of the Keyboard class delegate to the primary KeyboardDevice of the calling thread, so they are not necessarily thread-safe.

字段

GotKeyboardFocusEvent

标识 GotKeyboardFocus 附加事件。Identifies the GotKeyboardFocus attached event.

KeyboardInputProviderAcquireFocusEvent

标识 KeyboardInputProviderAcquireFocus 附加事件。Identifies the KeyboardInputProviderAcquireFocus attached event.

KeyDownEvent

标识 KeyDown 附加事件。Identifies the KeyDown attached event.

KeyUpEvent

标识 KeyUp 附加事件。Identifies the KeyUp attached event.

LostKeyboardFocusEvent

标识 LostKeyboardFocus 附加事件。Identifies the LostKeyboardFocus attached event.

PreviewGotKeyboardFocusEvent

标识 PreviewGotKeyboardFocus 附加事件。Identifies the PreviewGotKeyboardFocus attached event.

PreviewKeyboardInputProviderAcquireFocusEvent

标识 PreviewKeyboardInputProviderAcquireFocus 附加事件。Identifies the PreviewKeyboardInputProviderAcquireFocus attached event.

PreviewKeyDownEvent

标识 PreviewKeyDown 附加事件。Identifies the PreviewKeyDown attached event.

PreviewKeyUpEvent

标识 PreviewKeyUp 附加事件。Identifies the PreviewKeyUp attached event.

PreviewLostKeyboardFocusEvent

标识 PreviewLostKeyboardFocus 附加事件。Identifies the PreviewLostKeyboardFocus attached event.

属性

DefaultRestoreFocusMode

获取或设置还原焦点时 Windows Presentation Foundation (WPF)Windows Presentation Foundation (WPF) 的行为。Gets or sets the behavior of Windows Presentation Foundation (WPF)Windows Presentation Foundation (WPF) when restoring focus.

FocusedElement

获取具有键盘焦点的元素。Gets the element that has keyboard focus.

Modifiers

获取当前按下的 ModifierKeys 集。Gets the set of ModifierKeys that are currently pressed.

PrimaryDevice

获取主键盘输入设备。Gets the primary keyboard input device.

方法

AddGotKeyboardFocusHandler(DependencyObject, KeyboardFocusChangedEventHandler)

GotKeyboardFocus 附加事件添加处理程序。Adds a handler for the GotKeyboardFocus attached event.

AddKeyboardInputProviderAcquireFocusHandler(DependencyObject, KeyboardInputProviderAcquireFocusEventHandler)

KeyboardInputProviderAcquireFocus 附加事件添加处理程序。Adds a handler for the KeyboardInputProviderAcquireFocus attached event.

AddKeyDownHandler(DependencyObject, KeyEventHandler)

KeyDown 附加事件添加处理程序。Adds a handler for the KeyDown attached event.

AddKeyUpHandler(DependencyObject, KeyEventHandler)

KeyUp 附加事件添加处理程序。Adds a handler for the KeyUp attached event.

AddLostKeyboardFocusHandler(DependencyObject, KeyboardFocusChangedEventHandler)

LostKeyboardFocus 附加事件添加处理程序。Adds a handler for the LostKeyboardFocus attached event.

AddPreviewGotKeyboardFocusHandler(DependencyObject, KeyboardFocusChangedEventHandler)

PreviewGotKeyboardFocus 附加事件添加处理程序。Adds a handler for the PreviewGotKeyboardFocus attached event.

AddPreviewKeyboardInputProviderAcquireFocusHandler(DependencyObject, KeyboardInputProviderAcquireFocusEventHandler)

PreviewKeyboardInputProviderAcquireFocus 附加事件添加处理程序。Adds a handler for the PreviewKeyboardInputProviderAcquireFocus attached event.

AddPreviewKeyDownHandler(DependencyObject, KeyEventHandler)

PreviewKeyDown 附加事件添加处理程序。Adds a handler for the PreviewKeyDown attached event.

AddPreviewKeyUpHandler(DependencyObject, KeyEventHandler)

PreviewKeyUp 附加事件添加处理程序。Adds a handler for the PreviewKeyUp attached event.

AddPreviewLostKeyboardFocusHandler(DependencyObject, KeyboardFocusChangedEventHandler)

PreviewLostKeyboardFocus 附加事件添加处理程序。Adds a handler for the PreviewLostKeyboardFocus attached event.

ClearFocus()

清除焦点。Clears focus.

Focus(IInputElement)

对指定元素设置键盘焦点。Sets keyboard focus on the specified element.

GetKeyStates(Key)

获取指定键的键状态集。Gets the set of key states for the specified key.

IsKeyDown(Key)

确定指定的键是否处于按下状态。Determines whether the specified key is pressed.

IsKeyToggled(Key)

确定指定的键是否处于切换状态。Determines whether the specified key is toggled.

IsKeyUp(Key)

确定指定的键是否处于松开状态。Determines whether the specified key is released.

RemoveGotKeyboardFocusHandler(DependencyObject, KeyboardFocusChangedEventHandler)

移除 GotKeyboardFocus 附加事件的处理程序。Removes a handler for the GotKeyboardFocus attached event.

RemoveKeyboardInputProviderAcquireFocusHandler(DependencyObject, KeyboardInputProviderAcquireFocusEventHandler)

移除 KeyboardInputProviderAcquireFocus 附加事件的处理程序。Removes a handler for the KeyboardInputProviderAcquireFocus attached event.

RemoveKeyDownHandler(DependencyObject, KeyEventHandler)

移除 KeyDown 附加事件的处理程序。Removes a handler for the KeyDown attached event.

RemoveKeyUpHandler(DependencyObject, KeyEventHandler)

移除 KeyUp 附加事件的处理程序。Removes a handler for the KeyUp attached event.

RemoveLostKeyboardFocusHandler(DependencyObject, KeyboardFocusChangedEventHandler)

移除 LostKeyboardFocus 附加事件的处理程序。Removes a handler for the LostKeyboardFocus attached event.

RemovePreviewGotKeyboardFocusHandler(DependencyObject, KeyboardFocusChangedEventHandler)

移除 PreviewGotKeyboardFocus 附加事件的处理程序。Removes a handler for the PreviewGotKeyboardFocus attached event.

RemovePreviewKeyboardInputProviderAcquireFocusHandler(DependencyObject, KeyboardInputProviderAcquireFocusEventHandler)

移除 PreviewKeyboardInputProviderAcquireFocus 附加事件的处理程序。Removes a handler for the PreviewKeyboardInputProviderAcquireFocus attached event.

RemovePreviewKeyDownHandler(DependencyObject, KeyEventHandler)

移除 PreviewKeyDown 附加事件的处理程序。Removes a handler for the PreviewKeyDown attached event.

RemovePreviewKeyUpHandler(DependencyObject, KeyEventHandler)

移除 PreviewKeyUp 附加事件的处理程序。Removes a handler for the PreviewKeyUp attached event.

RemovePreviewLostKeyboardFocusHandler(DependencyObject, KeyboardFocusChangedEventHandler)

移除 PreviewLostKeyboardFocus 附加事件的处理程序。Removes a handler for the PreviewLostKeyboardFocus attached event.

附加事件

GotKeyboardFocus

在元素收到键盘焦点时发生。Occurs when an element receives keyboard focus.

KeyboardInputProviderAcquireFocus

在键盘输入提供程序获取焦点时发生。Occurs when the keyboard input provider acquires focus.

KeyDown

当按下键盘上的某个键时发生。Occurs when a key on the keyboard is pressed.

KeyUp

当松开键盘上的某个键时发生。Occurs when a key on the keyboard is released.

LostKeyboardFocus

在元素丢失键盘焦点时发生。Occurs when an element loses keyboard focus.

PreviewGotKeyboardFocus

当元素正在获取键盘焦点时发生。Occurs when an element is in the process of acquiring keyboard focus.

PreviewKeyboardInputProviderAcquireFocus

在键盘输入提供程序获取焦点过程中发生。Occurs when the keyboard input provider is in the process of acquiring focus.

PreviewKeyDown

当按下键盘上的某个键时发生。Occurs when a key on the keyboard is pressed.

PreviewKeyUp

当松开键盘上的某个键时发生。Occurs when a key on the keyboard is released.

PreviewLostKeyboardFocus

当元素正在失去键盘焦点时发生。Occurs when an element is in the process of losing keyboard focus.

适用于

另请参阅