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 新的路由事件重新公開。 一般而言,在和ContentElementUIElement處理應用程式的鍵盤事件會比較方便,而不是使用 Keyboard 事件。 如需詳細資訊,請參閱 輸入概觀

為了讓專案接收鍵盤輸入,元素必須是可設定焦點。 根據預設,大部分 UIElement 衍生的物件都是可聚焦的。 否則,若要讓項目成為可設定焦點,請將 Focusable 基底專案上的 屬性設定為 true。 如需基底專案的詳細資訊,請參閱 基底元素概觀

Panel 類別,例如 StackPanelCanvas,將的 Focusable 預設值設定為 false。 因此,若要讓這些物件取得鍵盤焦點, Focusable 必須設定為 true

鍵盤焦點是指接收鍵盤輸入的物件。 具有鍵盤焦點的項目已 IsKeyboardFocused 設定為 true。 整個桌面上只能有一個鍵盤焦點的專案。 邏輯焦點是指焦點範圍內具有焦點的物件。 如需焦點、鍵盤焦點和邏輯焦點的詳細資訊,請參閱 輸入 概觀和 焦點概觀

類別的 Keyboard 靜態成員會委派給呼叫線程的主要 KeyboardDevice 複本,因此它們不一定是安全線程。

欄位

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

項目正在失去鍵盤焦點時發生。

適用於

另請參閱