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. 如果 Key 傳遞至 IsKeyToggled 已切換,則按鈕的背景會變更。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. 一般來說,在和上處理應用程式的鍵盤事件比較方便, UIElement ContentElement 而不是使用 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 )會將的預設值設定 FocusablefalsePanel 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.

適用於

另請參閱