Keyboard Keyboard Keyboard Keyboard Class

定義

表示鍵盤裝置。Represents the keyboard device.

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

範例

下列範例示範如何使用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 , 請將基底元素true上的屬性設定為。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類別 (例如StackPanelCanvas) 會將的Focusable預設值設定為falsePanel classes, such as StackPanel and Canvas, set the default value of Focusable to false. 因此, 若要讓這些物件取得鍵盤焦點Focusable , 必須將設true為。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. 具有鍵盤焦點的元素已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 GotKeyboardFocusEvent GotKeyboardFocusEvent GotKeyboardFocusEvent

識別 GotKeyboardFocus 附加事件。Identifies the GotKeyboardFocus attached event.

KeyboardInputProviderAcquireFocusEvent KeyboardInputProviderAcquireFocusEvent KeyboardInputProviderAcquireFocusEvent KeyboardInputProviderAcquireFocusEvent

識別 KeyboardInputProviderAcquireFocus 附加事件。Identifies the KeyboardInputProviderAcquireFocus attached event.

KeyDownEvent KeyDownEvent KeyDownEvent KeyDownEvent

識別 KeyDown 附加事件。Identifies the KeyDown attached event.

KeyUpEvent KeyUpEvent KeyUpEvent KeyUpEvent

識別 KeyUp 附加事件。Identifies the KeyUp attached event.

LostKeyboardFocusEvent LostKeyboardFocusEvent LostKeyboardFocusEvent LostKeyboardFocusEvent

識別 LostKeyboardFocus 附加事件。Identifies the LostKeyboardFocus attached event.

PreviewGotKeyboardFocusEvent PreviewGotKeyboardFocusEvent PreviewGotKeyboardFocusEvent PreviewGotKeyboardFocusEvent

識別 PreviewGotKeyboardFocus 附加事件。Identifies the PreviewGotKeyboardFocus attached event.

PreviewKeyboardInputProviderAcquireFocusEvent PreviewKeyboardInputProviderAcquireFocusEvent PreviewKeyboardInputProviderAcquireFocusEvent PreviewKeyboardInputProviderAcquireFocusEvent

識別 PreviewKeyboardInputProviderAcquireFocus 附加事件。Identifies the PreviewKeyboardInputProviderAcquireFocus attached event.

PreviewKeyDownEvent PreviewKeyDownEvent PreviewKeyDownEvent PreviewKeyDownEvent

識別 PreviewKeyDown 附加事件。Identifies the PreviewKeyDown attached event.

PreviewKeyUpEvent PreviewKeyUpEvent PreviewKeyUpEvent PreviewKeyUpEvent

識別 PreviewKeyUp 附加事件。Identifies the PreviewKeyUp attached event.

PreviewLostKeyboardFocusEvent PreviewLostKeyboardFocusEvent PreviewLostKeyboardFocusEvent PreviewLostKeyboardFocusEvent

識別 PreviewLostKeyboardFocus 附加事件。Identifies the PreviewLostKeyboardFocus attached event.

屬性

DefaultRestoreFocusMode DefaultRestoreFocusMode DefaultRestoreFocusMode 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 FocusedElement FocusedElement FocusedElement

取得擁有鍵盤焦點的項目。Gets the element that has keyboard focus.

Modifiers Modifiers Modifiers Modifiers

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

PrimaryDevice PrimaryDevice PrimaryDevice PrimaryDevice

取得主要鍵盤輸入裝置。Gets the primary keyboard input device.

方法

AddGotKeyboardFocusHandler(DependencyObject, KeyboardFocusChangedEventHandler) AddGotKeyboardFocusHandler(DependencyObject, KeyboardFocusChangedEventHandler) AddGotKeyboardFocusHandler(DependencyObject, KeyboardFocusChangedEventHandler) AddGotKeyboardFocusHandler(DependencyObject, KeyboardFocusChangedEventHandler)

加入 GotKeyboardFocus 附加事件的處理常式。Adds a handler for the GotKeyboardFocus attached event.

AddKeyboardInputProviderAcquireFocusHandler(DependencyObject, KeyboardInputProviderAcquireFocusEventHandler) AddKeyboardInputProviderAcquireFocusHandler(DependencyObject, KeyboardInputProviderAcquireFocusEventHandler) AddKeyboardInputProviderAcquireFocusHandler(DependencyObject, KeyboardInputProviderAcquireFocusEventHandler) AddKeyboardInputProviderAcquireFocusHandler(DependencyObject, KeyboardInputProviderAcquireFocusEventHandler)

加入 KeyboardInputProviderAcquireFocus 附加事件的處理常式。Adds a handler for the KeyboardInputProviderAcquireFocus attached event.

AddKeyDownHandler(DependencyObject, KeyEventHandler) AddKeyDownHandler(DependencyObject, KeyEventHandler) AddKeyDownHandler(DependencyObject, KeyEventHandler) AddKeyDownHandler(DependencyObject, KeyEventHandler)

加入 KeyDown 附加事件的處理常式。Adds a handler for the KeyDown attached event.

AddKeyUpHandler(DependencyObject, KeyEventHandler) AddKeyUpHandler(DependencyObject, KeyEventHandler) AddKeyUpHandler(DependencyObject, KeyEventHandler) AddKeyUpHandler(DependencyObject, KeyEventHandler)

加入 KeyUp 附加事件的處理常式。Adds a handler for the KeyUp attached event.

AddLostKeyboardFocusHandler(DependencyObject, KeyboardFocusChangedEventHandler) AddLostKeyboardFocusHandler(DependencyObject, KeyboardFocusChangedEventHandler) AddLostKeyboardFocusHandler(DependencyObject, KeyboardFocusChangedEventHandler) AddLostKeyboardFocusHandler(DependencyObject, KeyboardFocusChangedEventHandler)

加入 LostKeyboardFocus 附加事件的處理常式。Adds a handler for the LostKeyboardFocus attached event.

AddPreviewGotKeyboardFocusHandler(DependencyObject, KeyboardFocusChangedEventHandler) AddPreviewGotKeyboardFocusHandler(DependencyObject, KeyboardFocusChangedEventHandler) AddPreviewGotKeyboardFocusHandler(DependencyObject, KeyboardFocusChangedEventHandler) AddPreviewGotKeyboardFocusHandler(DependencyObject, KeyboardFocusChangedEventHandler)

加入 PreviewGotKeyboardFocus 附加事件的處理常式。Adds a handler for the PreviewGotKeyboardFocus attached event.

AddPreviewKeyboardInputProviderAcquireFocusHandler(DependencyObject, KeyboardInputProviderAcquireFocusEventHandler) AddPreviewKeyboardInputProviderAcquireFocusHandler(DependencyObject, KeyboardInputProviderAcquireFocusEventHandler) AddPreviewKeyboardInputProviderAcquireFocusHandler(DependencyObject, KeyboardInputProviderAcquireFocusEventHandler) AddPreviewKeyboardInputProviderAcquireFocusHandler(DependencyObject, KeyboardInputProviderAcquireFocusEventHandler)

加入 PreviewKeyboardInputProviderAcquireFocus 附加事件的處理常式。Adds a handler for the PreviewKeyboardInputProviderAcquireFocus attached event.

AddPreviewKeyDownHandler(DependencyObject, KeyEventHandler) AddPreviewKeyDownHandler(DependencyObject, KeyEventHandler) AddPreviewKeyDownHandler(DependencyObject, KeyEventHandler) AddPreviewKeyDownHandler(DependencyObject, KeyEventHandler)

加入 PreviewKeyDown 附加事件的處理常式。Adds a handler for the PreviewKeyDown attached event.

AddPreviewKeyUpHandler(DependencyObject, KeyEventHandler) AddPreviewKeyUpHandler(DependencyObject, KeyEventHandler) AddPreviewKeyUpHandler(DependencyObject, KeyEventHandler) AddPreviewKeyUpHandler(DependencyObject, KeyEventHandler)

加入 PreviewKeyUp 附加事件的處理常式。Adds a handler for the PreviewKeyUp attached event.

AddPreviewLostKeyboardFocusHandler(DependencyObject, KeyboardFocusChangedEventHandler) AddPreviewLostKeyboardFocusHandler(DependencyObject, KeyboardFocusChangedEventHandler) AddPreviewLostKeyboardFocusHandler(DependencyObject, KeyboardFocusChangedEventHandler) AddPreviewLostKeyboardFocusHandler(DependencyObject, KeyboardFocusChangedEventHandler)

加入 PreviewLostKeyboardFocus 附加事件的處理常式。Adds a handler for the PreviewLostKeyboardFocus attached event.

ClearFocus() ClearFocus() ClearFocus() ClearFocus()

清除焦點。Clears focus.

Focus(IInputElement) Focus(IInputElement) Focus(IInputElement) Focus(IInputElement)

設定鍵盤焦點於指定的項目上。Sets keyboard focus on the specified element.

GetKeyStates(Key) GetKeyStates(Key) GetKeyStates(Key) GetKeyStates(Key)

取得指定按鍵的按鍵狀態集。Gets the set of key states for the specified key.

IsKeyDown(Key) IsKeyDown(Key) IsKeyDown(Key) IsKeyDown(Key)

判斷指定的按鍵是否為已按下。Determines whether the specified key is pressed.

IsKeyToggled(Key) IsKeyToggled(Key) IsKeyToggled(Key) IsKeyToggled(Key)

判斷指定的按鍵是否為已切換。Determines whether the specified key is toggled.

IsKeyUp(Key) IsKeyUp(Key) IsKeyUp(Key) IsKeyUp(Key)

判斷指定的按鍵是否為已放開。Determines whether the specified key is released.

RemoveGotKeyboardFocusHandler(DependencyObject, KeyboardFocusChangedEventHandler) RemoveGotKeyboardFocusHandler(DependencyObject, KeyboardFocusChangedEventHandler) RemoveGotKeyboardFocusHandler(DependencyObject, KeyboardFocusChangedEventHandler) RemoveGotKeyboardFocusHandler(DependencyObject, KeyboardFocusChangedEventHandler)

移除 GotKeyboardFocus 附加事件的處理常式。Removes a handler for the GotKeyboardFocus attached event.

RemoveKeyboardInputProviderAcquireFocusHandler(DependencyObject, KeyboardInputProviderAcquireFocusEventHandler) RemoveKeyboardInputProviderAcquireFocusHandler(DependencyObject, KeyboardInputProviderAcquireFocusEventHandler) RemoveKeyboardInputProviderAcquireFocusHandler(DependencyObject, KeyboardInputProviderAcquireFocusEventHandler) RemoveKeyboardInputProviderAcquireFocusHandler(DependencyObject, KeyboardInputProviderAcquireFocusEventHandler)

移除 KeyboardInputProviderAcquireFocus 附加事件的處理常式。Removes a handler for the KeyboardInputProviderAcquireFocus attached event.

RemoveKeyDownHandler(DependencyObject, KeyEventHandler) RemoveKeyDownHandler(DependencyObject, KeyEventHandler) RemoveKeyDownHandler(DependencyObject, KeyEventHandler) RemoveKeyDownHandler(DependencyObject, KeyEventHandler)

移除 KeyDown 附加事件的處理常式。Removes a handler for the KeyDown attached event.

RemoveKeyUpHandler(DependencyObject, KeyEventHandler) RemoveKeyUpHandler(DependencyObject, KeyEventHandler) RemoveKeyUpHandler(DependencyObject, KeyEventHandler) RemoveKeyUpHandler(DependencyObject, KeyEventHandler)

移除 KeyUp 附加事件的處理常式。Removes a handler for the KeyUp attached event.

RemoveLostKeyboardFocusHandler(DependencyObject, KeyboardFocusChangedEventHandler) RemoveLostKeyboardFocusHandler(DependencyObject, KeyboardFocusChangedEventHandler) RemoveLostKeyboardFocusHandler(DependencyObject, KeyboardFocusChangedEventHandler) RemoveLostKeyboardFocusHandler(DependencyObject, KeyboardFocusChangedEventHandler)

移除 LostKeyboardFocus 附加事件的處理常式。Removes a handler for the LostKeyboardFocus attached event.

RemovePreviewGotKeyboardFocusHandler(DependencyObject, KeyboardFocusChangedEventHandler) RemovePreviewGotKeyboardFocusHandler(DependencyObject, KeyboardFocusChangedEventHandler) RemovePreviewGotKeyboardFocusHandler(DependencyObject, KeyboardFocusChangedEventHandler) RemovePreviewGotKeyboardFocusHandler(DependencyObject, KeyboardFocusChangedEventHandler)

移除 PreviewGotKeyboardFocus 附加事件的處理常式。Removes a handler for the PreviewGotKeyboardFocus attached event.

RemovePreviewKeyboardInputProviderAcquireFocusHandler(DependencyObject, KeyboardInputProviderAcquireFocusEventHandler) RemovePreviewKeyboardInputProviderAcquireFocusHandler(DependencyObject, KeyboardInputProviderAcquireFocusEventHandler) RemovePreviewKeyboardInputProviderAcquireFocusHandler(DependencyObject, KeyboardInputProviderAcquireFocusEventHandler) RemovePreviewKeyboardInputProviderAcquireFocusHandler(DependencyObject, KeyboardInputProviderAcquireFocusEventHandler)

移除 PreviewKeyboardInputProviderAcquireFocus 附加事件的處理常式。Removes a handler for the PreviewKeyboardInputProviderAcquireFocus attached event.

RemovePreviewKeyDownHandler(DependencyObject, KeyEventHandler) RemovePreviewKeyDownHandler(DependencyObject, KeyEventHandler) RemovePreviewKeyDownHandler(DependencyObject, KeyEventHandler) RemovePreviewKeyDownHandler(DependencyObject, KeyEventHandler)

移除 PreviewKeyDown 附加事件的處理常式。Removes a handler for the PreviewKeyDown attached event.

RemovePreviewKeyUpHandler(DependencyObject, KeyEventHandler) RemovePreviewKeyUpHandler(DependencyObject, KeyEventHandler) RemovePreviewKeyUpHandler(DependencyObject, KeyEventHandler) RemovePreviewKeyUpHandler(DependencyObject, KeyEventHandler)

移除 PreviewKeyUp 附加事件的處理常式。Removes a handler for the PreviewKeyUp attached event.

RemovePreviewLostKeyboardFocusHandler(DependencyObject, KeyboardFocusChangedEventHandler) RemovePreviewLostKeyboardFocusHandler(DependencyObject, KeyboardFocusChangedEventHandler) RemovePreviewLostKeyboardFocusHandler(DependencyObject, KeyboardFocusChangedEventHandler) RemovePreviewLostKeyboardFocusHandler(DependencyObject, KeyboardFocusChangedEventHandler)

移除 PreviewLostKeyboardFocus 附加事件的處理常式。Removes a handler for the PreviewLostKeyboardFocus attached event.

附加事件

GotKeyboardFocus GotKeyboardFocus GotKeyboardFocus GotKeyboardFocus

當項目接收到鍵盤焦點時發生。Occurs when an element receives keyboard focus.

KeyboardInputProviderAcquireFocus KeyboardInputProviderAcquireFocus KeyboardInputProviderAcquireFocus KeyboardInputProviderAcquireFocus

發生於鍵盤輸入提供者取得焦點時。Occurs when the keyboard input provider acquires focus.

KeyDown KeyDown KeyDown KeyDown

當按下鍵盤上的按鍵時發生。Occurs when a key on the keyboard is pressed.

KeyUp KeyUp KeyUp KeyUp

當放開鍵盤上的按鍵時發生。Occurs when a key on the keyboard is released.

LostKeyboardFocus LostKeyboardFocus LostKeyboardFocus LostKeyboardFocus

當項目失去鍵盤焦點時發生。Occurs when an element loses keyboard focus.

PreviewGotKeyboardFocus PreviewGotKeyboardFocus PreviewGotKeyboardFocus PreviewGotKeyboardFocus

當項目正在取得鍵盤焦點時發生。Occurs when an element is in the process of acquiring keyboard focus.

PreviewKeyboardInputProviderAcquireFocus PreviewKeyboardInputProviderAcquireFocus PreviewKeyboardInputProviderAcquireFocus PreviewKeyboardInputProviderAcquireFocus

發生於鍵盤輸入提供者正在取得鍵盤焦點時。Occurs when the keyboard input provider is in the process of acquiring focus.

PreviewKeyDown PreviewKeyDown PreviewKeyDown PreviewKeyDown

當按下鍵盤上的按鍵時發生。Occurs when a key on the keyboard is pressed.

PreviewKeyUp PreviewKeyUp PreviewKeyUp PreviewKeyUp

當放開鍵盤上的按鍵時發生。Occurs when a key on the keyboard is released.

PreviewLostKeyboardFocus PreviewLostKeyboardFocus PreviewLostKeyboardFocus PreviewLostKeyboardFocus

當項目正在失去鍵盤焦點時發生。Occurs when an element is in the process of losing keyboard focus.

適用於

另請參閱