Skip to main content

KeymapSystem Library Reference

Overview

The KeymapSystem static library (src/WhiskerToolbox/KeymapSystem/) provides the core infrastructure for configurable, context-aware keyboard shortcuts. It implements action registration, scope-based resolution, user override management, and conflict detection.

Files

KeyAction.hpp

Defines the core action types:

  • KeyActionScopeKind — Enum: Global, EditorFocused, AlwaysRouted
  • KeyActionScope — Pairs a scope kind with an optional EditorTypeId. Factory methods: global(), editorFocused(type_id), alwaysRouted(type_id)
  • KeyActionDescriptor — Pure metadata for a bindable action: action_id, display_name, category, scope, and default_binding

Keymap.hpp

Defines binding types:

  • KeymapEntry — Runtime resolved binding (action → key sequence, enabled flag, override flag)
  • KeymapOverrideEntry — Serializable user override using std::string for reflect-cpp compatibility. Stored in AppPreferencesData for persistence.

KeyActionAdapter.hpp / KeyActionAdapter.cpp

Composition-based component for receiving keymap dispatches. Widgets create a KeyActionAdapter as a child QObject and install a handler lambda. No inheritance required.

Key API:

  • setHandler(Handler) — Install callback std::function<bool(QString const &)>
  • handleKeyAction(action_id) — Called by KeymapManager to dispatch
  • setInstanceId() / setTypeId() — Associate with an editor instance/type

KeymapManager.hpp / KeymapManager.cpp

Central manager class (owned by MainWindow). Inherits QObject.

Registration API:

  • registerAction(descriptor) — Register action metadata (returns false on duplicate)
  • unregisterAction(action_id) — Remove action and its override

Query API:

  • allActions() — All registered descriptors
  • action(action_id) — Lookup by ID
  • bindingFor(action_id) — Effective key (override > default)
  • isActionEnabled(action_id) — Check enabled state

Override API:

  • setUserOverride(action_id, key) — Override key binding
  • clearUserOverride(action_id) — Revert to default
  • clearAllOverrides() — Reset all customizations
  • setActionEnabled(action_id, enabled) — Enable/disable

Resolution API:

  • resolveKeyPress(key, focused_type_id) — Scope-priority resolution: EditorFocused > AlwaysRouted > Global
  • detectConflicts() — Find same-key/same-scope collisions

Adapter routing:

  • registerAdapter(adapter) — Register for dispatch (auto-deregisters on destruction)
  • dispatchAction(action_id, target_type, target_instance) — Route to matching adapter

Serialization:

  • exportOverrides() / importOverrides(overrides) — Round-trip persistence

Signal: bindingsChanged() — emitted on any binding modification.

Event filter (installed on QApplication by MainWindow):

  • eventFilter(obj, event) — Intercepts QEvent::KeyPress. Bypasses text-input widgets (QLineEdit, QTextEdit, QPlainTextEdit, QComboBox, QAbstractSpinBox). Resolves and dispatches through scope priority. Returns false (pass-through) when no action matches.
  • setEditorRegistry(registry) — Set the EditorRegistry used for focus tracking via SelectionContext::activeEditorId().

Registered Action Catalogue

Action ID Display Name Category Scope Default Key
media.assign_group_1–9 Assign to Group 1–9 Media Viewer EditorFocused (MediaWidget) Alt+1–9
media.tool.select Select Tool Media Viewer EditorFocused (MediaWidget) S
media.tool.pen Pen Tool Media Viewer EditorFocused (MediaWidget) P
media.tool.eraser Eraser Tool Media Viewer EditorFocused (MediaWidget) E
media.tool.smooth Smooth Tool Media Viewer EditorFocused (MediaWidget) Shift+S
media.tool.none Deselect Tool Media Viewer EditorFocused (MediaWidget) Escape
media.pen.cycle_append_endpoint Cycle Pen Append Endpoint Media Viewer EditorFocused (MediaWidget) X
timeline.play_pause Play / Pause Timeline AlwaysRouted (TimeScrollBar) Space
timeline.next_frame Next Frame Timeline AlwaysRouted (TimeScrollBar) Right
timeline.prev_frame Previous Frame Timeline AlwaysRouted (TimeScrollBar) Left
timeline.jump_forward Jump Forward Timeline AlwaysRouted (TimeScrollBar) Ctrl+Right
timeline.jump_backward Jump Backward Timeline AlwaysRouted (TimeScrollBar) Ctrl+Left
scatter_plot.polygon_complete Complete Polygon Scatter Plot EditorFocused (ScatterPlotWidget) Return
scatter_plot.polygon_cancel Cancel Polygon Scatter Plot EditorFocused (ScatterPlotWidget) Escape
scatter_plot.polygon_undo_vertex Undo Last Vertex Scatter Plot EditorFocused (ScatterPlotWidget) Backspace
digital_interval_series.mark_contact_start Mark Contact Start Digital Interval Series AlwaysRouted (DigitalIntervalSeriesInspector) (none)
digital_interval_series.mark_contact_end Mark Contact End Digital Interval Series AlwaysRouted (DigitalIntervalSeriesInspector) (none)
digital_interval_series.flip_current_frame Flip Current Frame Digital Interval Series AlwaysRouted (DigitalIntervalSeriesInspector) (none)
whisker_widget.trace Trace Whisker Tracking AlwaysRouted (WhiskerWidget) T
deep_learning.predict_current Predict Current Frame Deep Learning AlwaysRouted (DeepLearningWidget) Ctrl+R

Dependencies

  • Qt6::Core, Qt6::Gui, Qt6::Widgets — QKeySequence, QObject, signals/slots, widget type checks for text-input bypass
  • EditorState — EditorTypeId, EditorInstanceId strong types, EditorRegistry, SelectionContext

Test Coverage

Tests in tests/WhiskerToolbox/KeymapSystem/test_keymap_manager.cpp cover:

  • Action registration and duplicate rejection
  • Unregistration with override cleanup
  • Default and override binding resolution
  • Override clear (single and all)
  • Enable/disable state
  • Conflict detection (same scope, different scope, override-created)
  • Scope resolution priority (EditorFocused > AlwaysRouted > Global)
  • Disabled action exclusion from resolution
  • Override binding in resolution
  • Adapter registration and dispatch routing
  • Adapter auto-deregistration on destruction
  • Instance-targeted dispatch
  • Export/import override round-trip