Keymap System
The Keymap System provides configurable, context-aware keyboard shortcuts for WhiskerToolbox. It is heavily inspired by Blender’s keymap architecture, adapted for a Qt6 docked-widget environment.
Motivation
All keyboard shortcuts in WhiskerToolbox are currently hardcoded:
- Global keys (Space for play/pause, arrows for frame navigation) live in
MainWindow::eventFilter(). - Per-widget keys (1–9 for group assignment in Media_Widget, Enter/Escape/Backspace for polygon editing in scatter plots) are in individual
keyPressEvent()overrides. - Always-routed keys (arrow keys dispatched to
TimeScrollBarregardless of focus) are mixed into the global event filter with manualqobject_castchecks.
There is no way for users to reconfigure bindings, no way for a widget to advertise “I provide action X,” and no systematic conflict detection. The Keymap System replaces this ad-hoc approach with a structured, extensible, and user-configurable dispatch layer.
Use Cases
- Focus switching — Assign a hotkey (e.g., F2) to raise and focus a specific editor, regardless of which widget currently has keyboard focus.
- Per-editor shortcuts — When a particular editor is the active editor, its
EditorFocusedactions take priority (e.g., Media Viewer group assignment on Alt+1–9). - Always-routed commands — Certain actions always dispatch to a specific widget even when it is not focused (e.g., arrow keys always control the timeline scrollbar).
Design Overview
Scope Resolution Order
When a key is pressed, the system resolves bindings in this order (first match wins):
- Text/input bypass — If the focused widget is a
QLineEdit,QTextEdit,QComboBox, or similar input widget, the key event passes through to Qt normally. This preserves standard text editing. - Focused-editor keymap — Actions registered for the editor type that is the active editor in
SelectionContext(e.g.,media.assign_group_1only whenMediaWidgetis active). This is not the same as Qt widget focus: clicking the media canvas or focusing a media dock tab updates the active editor viaEditorCreationControllerandMedia_Widget. - Always-routed keymap — Actions that always dispatch to a specific widget regardless of focus (e.g., Space → play/pause via TimeScrollBar).
- Global keymap — Actions not tied to any editor (e.g., focus-switching hotkeys).
This matches Blender’s model: context-specific bindings shadow global bindings, and input widgets are always exempt.
Core Types
KeyActionDescriptor
A named, bindable action. Pure metadata — no callback stored.
| Field | Type | Example |
|---|---|---|
action_id |
QString |
"media.assign_group_1" |
display_name |
QString |
"Assign to Group 1" |
category |
QString |
"Media Viewer" |
scope |
KeyActionScope |
EditorFocused("MediaWidget") |
default_binding |
QKeySequence |
Alt+1 (media group assign) |
KeyActionScope
Determines when an action is eligible for dispatch.
| Variant | Meaning |
|---|---|
Global |
Always available, no target widget |
EditorFocused(EditorTypeId) |
Only when this editor type is the active editor (SelectionContext::activeEditorId) |
AlwaysRouted(EditorTypeId) |
Always available; dispatched to this editor type regardless of focus |
KeymapEntry
A resolved binding: maps an action to a key sequence. Supports user overrides.
action_id: "timeline.play_pause"
key_sequence: QKeySequence(Qt::Key_Space)
enabled: true
is_override: false // true when user has customized this binding
KeyActionAdapter (composition)
Widgets that want to receive keymap dispatches create a KeyActionAdapter as a child QObject. This uses composition over inheritance — no multiple inheritance or virtual interfaces required.
class KeyActionAdapter : public QObject {
Q_OBJECT
public:
using Handler = std::function<bool(QString const &)>;
explicit KeyActionAdapter(QObject * parent = nullptr);
/// Set the handler called when an action is dispatched to this adapter.
void setHandler(Handler handler);
/// Called by KeymapManager. Delegates to the installed handler.
bool handleKeyAction(QString const & action_id);
/// The EditorInstanceId this adapter is associated with.
void setInstanceId(EditorInstanceId const & id);
[[nodiscard]] EditorInstanceId instanceId() const;
};When a widget is created, it constructs a KeyActionAdapter as a child and registers it with KeymapManager. When the widget is destroyed, Qt’s parent-child ownership automatically destroys the adapter, and KeymapManager detects this via QObject::destroyed to clean up its routing table.
This approach avoids the diamond-inheritance problems that arise when multiple mixins are combined with QObject-derived classes.
KeymapManager
Central singleton-like object (owned by MainWindow). Responsibilities:
- Action registry — Stores all
KeyActionDescriptors. Widgets register descriptors duringregisterTypes(). - Event filter — Installed on
QApplication. InterceptsQEvent::KeyPress, applies scope resolution, and dispatches to the correct handler. - Override management — Merges shipped defaults with user customizations. Detects and reports conflicts (same key bound to multiple actions in the same scope).
- Persistence — Serializes user overrides to
AppPreferencesData(stored in~/.config/WhiskerToolbox/preferences.json). - Auto-generated focus actions — For every registered
EditorTypeId, creates a"focus.<type_id>"action (default: unbound) that raises the dock widget and transfers focus.
Dispatch Flow
QKeyEvent arrives at QApplication event filter
│
├── Is focused widget a text input? → pass through to Qt
│
├── Look up focused editor via SelectionContext::activeEditorId()
│ └── Find EditorFocused actions for that type matching this key
│ └── Found? → look up KeyActionAdapter for that editor instance
│ └── Call adapter->handleKeyAction(action_id) → done
│
├── Find AlwaysRouted actions matching this key
│ └── Found? → look up KeyActionAdapter for the target editor type
│ └── Call adapter->handleKeyAction(action_id) → done
│
├── Find Global actions matching this key
│ └── Found? → execute (focus switch, app command, etc.) → done
│
└── No match → pass through to Qt (event not consumed)
Registration Pattern
Widgets register action descriptors (pure metadata) during their registerTypes() call. No callbacks or widget pointers are stored at registration time.
At runtime, when a widget instance is created, it constructs a KeyActionAdapter, installs a handler, and registers the adapter with KeymapManager. Dispatch happens through the adapter — no virtual interface inheritance required.
// During type registration (pure metadata, no widget instances yet):
keymap_manager->registerAction({
.action_id = "media.assign_group_1",
.display_name = "Assign to Group 1",
.category = "Media Viewer",
.scope = KeyActionScope::EditorFocused(EditorTypeId("MediaWidget")),
.default_binding = QKeySequence(Qt::Key_1)
});
// ... repeat for groups 2–9When a Media_Window is created, it sets up its adapter:
// In Media_Window constructor:
_key_adapter = new KeyActionAdapter(this); // QObject parenting
_key_adapter->setInstanceId(_state->getInstanceId());
_key_adapter->setHandler([this](QString const & action_id) -> bool {
if (action_id == "media.assign_group_1") {
assignToGroup(1);
return true;
}
// ...
return false;
});
keymap_manager->registerAdapter(_key_adapter);
// Deregistration is automatic via QObject::destroyed signalPersistence
User keybinding overrides are stored in AppPreferencesData:
struct KeymapOverrideEntry {
std::string action_id; // "timeline.play_pause"
std::string key_sequence; // "Space"
bool enabled = true;
};
struct AppPreferencesData {
// ... existing fields ...
std::vector<KeymapOverrideEntry> keybinding_overrides;
};This is app-wide (not per-workspace) — the same keybindings apply everywhere, matching Blender’s model. Serialized via reflect-cpp to ~/.config/WhiskerToolbox/preferences.json.
Keybinding Editor Widget
A dedicated editor (registered via EditorRegistry, opened from the View menu) that displays all actions grouped by category:
- Each row shows: action name, scope badge, current key binding, and a “Record” button
- Pressing “Record” then pressing a key captures the new binding
- Conflicting bindings (same key + same scope) are highlighted
- “Reset to Default” per-action and “Reset All” buttons
- Changes are persisted on apply
Relationship to Existing Systems
| System | Relationship |
|---|---|
SelectionContext |
KeymapManager reads activeEditorId() to determine focused editor for scope resolution |
ContextAction |
Orthogonal — ContextActions are mouse-driven actions shown in context menus; KeyActions are keyboard-driven. A future unification could share action IDs. |
Commands (ICommand) |
A KeyActionAdapter handler may internally create and execute an ICommand. The keymap layer handles input binding; the command layer handles data mutation. |
EditorRegistry |
KeymapManager uses the type registry to enumerate editor types (for auto-generated focus actions) and to look up widget instances for dispatch. |
AppPreferencesData |
Stores user keybinding overrides. Loaded on startup, saved on change. |
Contents
- Implementation Roadmap: Step-by-step plan for building the Keymap System.