通过


Keyboard 类

定义

表示键盘设备。

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

示例

以下示例演示如何使用 IsKeyToggled 该方法来确定键是否处于切换状态。 Key如果传递的按钮IsKeyToggled已切换,按钮的背景将更改。

// 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 类提供与键盘相关的事件、方法和属性,这些事件和属性提供有关键盘状态的信息。

Keyboard基元素类UIElementContentElement新的路由事件也会重新公开定义为附加事件的每个事件。 通常,处理应用程序的 UIElement 键盘事件更为 ContentElement方便,而不是使用 Keyboard 事件。 有关详细信息,请参阅 输入概述

为了使元素接收键盘输入,该元素必须可聚焦。 默认情况下,大多数 UIElement 派生对象可聚焦。 否则,若要使元素可聚焦,请将 Focusable 基元素上的属性设置为 true。 有关基元素的详细信息,请参阅 基本元素概述

Panel 类,例如 StackPanel ,并将 Canvas默认值 Focusable 设置为 false。 因此,要使这些对象获得键盘焦点, Focusable 必须设置为 true

键盘焦点是指接收键盘输入的对象。 具有键盘焦点的元素已 IsKeyboardFocused 设置为 true。 整个桌面上只能有一个具有键盘焦点的元素。 逻辑焦点是指具有焦点的焦点范围内的对象。 有关焦点、键盘焦点和逻辑焦点的详细信息,请参阅 输入概述焦点概述

类委托给调用线程的主KeyboardDevice节点的静态成员Keyboard,因此它们不一定是线程安全的。

字段

名称 说明
GotKeyboardFocusEvent

标识 GotKeyboardFocus 附加事件。

KeyboardInputProviderAcquireFocusEvent

标识 KeyboardInputProviderAcquireFocus 附加事件。

KeyDownEvent

标识 KeyDown 附加事件。

KeyUpEvent

标识 KeyUp 附加事件。

LostKeyboardFocusEvent

标识 LostKeyboardFocus 附加事件。

PreviewGotKeyboardFocusEvent

标识 PreviewGotKeyboardFocus 附加事件。

PreviewKeyboardInputProviderAcquireFocusEvent

标识 PreviewKeyboardInputProviderAcquireFocus 附加事件。

PreviewKeyDownEvent

标识 PreviewKeyDown 附加事件。

PreviewKeyUpEvent

标识 PreviewKeyUp 附加事件。

PreviewLostKeyboardFocusEvent

标识 PreviewLostKeyboardFocus 附加事件。

属性

名称 说明
DefaultRestoreFocusMode

获取或设置还原焦点时 Windows Presentation Foundation (WPF)的行为。

FocusedElement

获取具有键盘焦点的元素。

Modifiers

获取当前按下的 ModifierKeys 集合。

PrimaryDevice

获取主键盘输入设备。

方法

名称 说明
AddGotKeyboardFocusHandler(DependencyObject, KeyboardFocusChangedEventHandler)

为附加事件添加处理程序 GotKeyboardFocus

AddKeyboardInputProviderAcquireFocusHandler(DependencyObject, KeyboardInputProviderAcquireFocusEventHandler)

为附加事件添加处理程序 KeyboardInputProviderAcquireFocus

AddKeyDownHandler(DependencyObject, KeyEventHandler)

为附加事件添加处理程序 KeyDown

AddKeyUpHandler(DependencyObject, KeyEventHandler)

为附加事件添加处理程序 KeyUp

AddLostKeyboardFocusHandler(DependencyObject, KeyboardFocusChangedEventHandler)

为附加事件添加处理程序 LostKeyboardFocus

AddPreviewGotKeyboardFocusHandler(DependencyObject, KeyboardFocusChangedEventHandler)

为附加事件添加处理程序 PreviewGotKeyboardFocus

AddPreviewKeyboardInputProviderAcquireFocusHandler(DependencyObject, KeyboardInputProviderAcquireFocusEventHandler)

为附加事件添加处理程序 PreviewKeyboardInputProviderAcquireFocus

AddPreviewKeyDownHandler(DependencyObject, KeyEventHandler)

为附加事件添加处理程序 PreviewKeyDown

AddPreviewKeyUpHandler(DependencyObject, KeyEventHandler)

为附加事件添加处理程序 PreviewKeyUp

AddPreviewLostKeyboardFocusHandler(DependencyObject, KeyboardFocusChangedEventHandler)

为附加事件添加处理程序 PreviewLostKeyboardFocus

ClearFocus()

清除焦点。

Focus(IInputElement)

设置指定元素上的键盘焦点。

GetKeyStates(Key)

获取指定键的键状态集。

IsKeyDown(Key)

确定是否按下指定的键。

IsKeyToggled(Key)

确定是否切换指定的键。

IsKeyUp(Key)

确定是否释放指定的键。

RemoveGotKeyboardFocusHandler(DependencyObject, KeyboardFocusChangedEventHandler)

删除附加事件的处理程序 GotKeyboardFocus

RemoveKeyboardInputProviderAcquireFocusHandler(DependencyObject, KeyboardInputProviderAcquireFocusEventHandler)

删除附加事件的处理程序 KeyboardInputProviderAcquireFocus

RemoveKeyDownHandler(DependencyObject, KeyEventHandler)

删除附加事件的处理程序 KeyDown

RemoveKeyUpHandler(DependencyObject, KeyEventHandler)

删除附加事件的处理程序 KeyUp

RemoveLostKeyboardFocusHandler(DependencyObject, KeyboardFocusChangedEventHandler)

删除附加事件的处理程序 LostKeyboardFocus

RemovePreviewGotKeyboardFocusHandler(DependencyObject, KeyboardFocusChangedEventHandler)

删除附加事件的处理程序 PreviewGotKeyboardFocus

RemovePreviewKeyboardInputProviderAcquireFocusHandler(DependencyObject, KeyboardInputProviderAcquireFocusEventHandler)

删除附加事件的处理程序 PreviewKeyboardInputProviderAcquireFocus

RemovePreviewKeyDownHandler(DependencyObject, KeyEventHandler)

删除附加事件的处理程序 PreviewKeyDown

RemovePreviewKeyUpHandler(DependencyObject, KeyEventHandler)

删除附加事件的处理程序 PreviewKeyUp

RemovePreviewLostKeyboardFocusHandler(DependencyObject, KeyboardFocusChangedEventHandler)

删除附加事件的处理程序 PreviewLostKeyboardFocus

附加活动

名称 说明
GotKeyboardFocus

当元素接收键盘焦点时发生。

KeyboardInputProviderAcquireFocus

当键盘输入提供程序获取焦点时发生。

KeyDown

按下键盘上的键时发生。

KeyUp

释放键盘上的键时发生。

LostKeyboardFocus

当元素失去键盘焦点时发生。

PreviewGotKeyboardFocus

当元素正在获取键盘焦点的过程中发生。

PreviewKeyboardInputProviderAcquireFocus

当键盘输入提供程序正在获取焦点的过程中发生。

PreviewKeyDown

按下键盘上的键时发生。

PreviewKeyUp

释放键盘上的键时发生。

PreviewLostKeyboardFocus

当元素在失去键盘焦点的过程中发生。

适用于

另请参阅