Hot Key Service
Overview
IHotKeyService is the central component responsible for global keyboard shortcuts. It listens for key presses on the application window, matches them against the set of registered hot key actions, and executes the matching action against the currently focused view model.
Every shortcut is described by an IHotKeyAction. The service also persists user overrides: when a user rebinds an action, the new gesture is saved to the application configuration; default gestures are not stored, so resetting a binding simply removes the override.
Key Features
Global Dispatch: Subscribes to
KeyDownon the currentTopLeveland routes each gesture to a matching action.Context Awareness: An action is evaluated against the currently focused view model (
Shell.Navigation.SelectedControl). A typed action (HotKeyAction<T>) runs when that view model — or one of its routable ancestors — is of typeT; otherwise it is skipped.Rebinding & Persistence: Gestures can be changed at runtime through the indexer. Overrides are stored in
HotKeyServiceConfig; assigning the default gesture (ornull) removes the override.CanExecute Tracking:
ObserveCanExecutereports whether an action can currently run — convenient for enabling or disabling UI elements.
Hot Key Actions
A shortcut is defined by implementing IHotKeyAction (which extends IHotKeyInfo). The framework provides the HotKeyAction<T> base class that targets a capability interface T: the action can execute when the focused view model — or any of its ancestors in the routable tree — is of type T, and InternalExecute receives that matched instance already cast.
The framework registers a default set of actions out of the box, including SaveAction, SaveAsAction, UndoAction, RedoAction, SearchAction, RefreshAction, ClosePageAction, OpenFileAction, OpenHomePageAction, CancelAction and ClearAction.
Registration
The service and its default actions are registered by the core services, so in a normal app you do not register them yourself. A module that needs an extra shortcut just adds its own action — the service and the built-in actions are already in place:
If you compose the service registration yourself, pass a configure delegate to RegisterHotKeys and call RegisterDefault() inside it to keep the built-in actions:
Rebinding a Hot Key
Use the indexer to read or change the gesture bound to an action. Assigning null restores the default:
API
IHotKeyService
Dispatches global keyboard shortcuts and stores user overrides.
Property | Type | Description |
|---|---|---|
|
| Emits every recognized gesture before it is dispatched to an action. |
|
| Enables or disables global hot key handling. |
|
| All registered actions. |
|
| Emits when an action's gesture changes. |
Indexer | Type | Description |
|---|---|---|
|
| Gets or sets the gesture bound to an action id. Setting |
Method | Return Type | Description |
|---|---|---|
|
| Reports whether the action can execute for the current context. |
IHotKeyService.this[string hotKeyId]
Parameter | Type | Description |
|---|---|---|
|
| The action identifier. |
IHotKeyService.ObserveCanExecute
Parameter | Type | Description |
|---|---|---|
|
| The action identifier. |
IHotKeyInfo
Provides a read-only description of a hot key action.
Property | Type | Description |
|---|---|---|
|
| Unique identifier of the action. |
|
| Display name. |
|
| Human-readable description. |
|
| Icon associated with the action. |
|
| Default gesture used when not overridden. |
IHotKeyAction
Defines an executable hot key action. Extends IHotKeyInfo.
Method | Return Type | Description |
|---|---|---|
|
| Whether the action can run for the given context. |
|
| Runs the action against the given context. |
IHotKeyAction.CanExecute
Parameter | Type | Description |
|---|---|---|
|
| The view model context. |
IHotKeyAction.Execute
Parameter | Type | Description |
|---|---|---|
|
| The view model context. |
|
| A token that cancels the operation. |
Editing shortcuts in Settings
The default Settings page includes Keyboard shortcuts. Applications that compose their settings manually can opt in with settings.RegisterHotKeysSubPage().
The editor lists registered IHotKeyAction instances, including plugin contributions. Search matches the display name, description, action ID, and effective shortcut. Filters show all commands, overrides, unassigned commands, or conflicts.
Edits remain local until Apply. Cancel changes reloads the service assignments; Reset and Reset all restore defaults in the draft. Removing a shortcut uses new KeyGesture(Key.None) and persists an explicit disabled assignment. Assigning null through the service indexer continues to mean “restore the default”.
Recording a shortcut temporarily suspends service dispatch. If the new gesture is already assigned, the editor asks before removing it from the other commands. Multiple unassigned commands do not conflict. Existing duplicate gestures are shown by the conflict filter and must be resolved before applying. This editor conservatively treats equal gestures as conflicts even when actions target different contexts.
ApplyHotKeys on HotKeyService validates the batch and persists it before publishing change notifications. Consumers observing the first notification already see the complete new batch. The default interface implementation retains compatibility with custom services by applying entries through their indexer; custom implementations can override it for batch persistence.
The capture control handles key input through Avalonia routed events. Subscriptions belong to view models, and service notifications are marshalled to the UI dispatcher before updating bound collections. See Avalonia routed events and R3 subscription lifetime for the underlying framework behavior.