Input & Keyboard Event Routing Architecture
Overview
The TUI input system dispatches keyboard events from Mordant to selection screens, tool approval prompts, extension shortcuts, and chat prompt editing commands.
Key Routing & Guarding Rules
Keyboard event handling in InputStateMachine.handle(event, screen) follows a strict priority chain:
-
Custom Overlay Handling:
-
If a
CustomViewSessionoverlay is active, keys emit directly to the overlay session.
-
-
Extension Shortcut Guarding:
-
Custom extension shortcuts registered in
ShortcutRegistryare evaluated only when:-
Current visible screen is
TuiScreen.CHAT -
AND
!callbacks.isApprovalPending()(no pending tool approval prompt)
-
-
If both conditions hold and a matching shortcut is registered, the shortcut action executes and input state machine returns without further dispatch.
-
On selection screens (
MODEL_SELECTION,SESSION_SELECTION,EFFORT_SELECTION,TREE_SELECTION,FORK_SELECTION) or when tool approval is pending, shortcut lookup is bypassed so keys like"j"or"k"navigate screens or respond to tool prompts without triggering extension callbacks.
-
-
Screen Key Dispatch:
-
Events delegate to polymorphic
ScreenKeyHandlerstrategy implementations configured for the activeTuiScreen. -
On
TuiScreen.CHAT, ifisApprovalPending()is true, events delegate tocallbacks.onApprovalKey(event). -
Otherwise, events dispatch through the polymorphic
ChatKeyHandlerpipeline.
-
Polymorphic Key Handler Architecture
Monolithic conditional key handling in handleChatKey is decomposed using Martin Fowler refactoring patterns (Replace Conditional with Polymorphism and Extract Class):
1. ScreenKeyHandler
Polymorphic strategy for per-screen dispatch (TuiScreen.CHAT, MODEL_SELECTION, SESSION_SELECTION, EFFORT_SELECTION, TREE_SELECTION, FORK_SELECTION).
2. ChatKeyHandler Pipeline
Sequential functional handlers implementing single-responsibility strategy logic:
-
SessionLifecycleKeyHandler:Ctrl+C(agent cancellation /ctrl_csession shutdown) andCtrl+D(empty bufferctrl_dsession shutdown). -
ViewportKeyHandler:Ctrl+L(screen clearing) andCtrl+T(thinking visibility toggle). -
EffortKeyHandler:Ctrl+E,Alt+E, andCtrl+Shift+Ereasoning effort cycling. -
ExternalEditorKeyHandler:Ctrl+Gexternal editor launcher. -
InputHistoryKeyHandler:Alt+UpandAlt+Downinput history navigation. -
CompletionAndScrollKeyHandler:ArrowUp,ArrowDown,PageUp, andPageDownscrolling and completion overlay navigation. -
BufferEditorKeyHandler: Text typing,Enterprompt submission / slash-command execution,Tabcompletion trigger,Backspacecharacter deletion, andEscapebuffer clearing.
3. CompletionController
Class extracted from InputStateMachine managing file completion (@/#), slash command completion (/), and completion item selection/application.
Thread-Safe Single-Writer Editor Mutation Queue
Programmatic editor text APIs in MordantExtensionUI (setEditorText, pasteToEditor) write mutations into a lock-free single-writer Channel<EditorMutation> (SetText, Insert, Clear).
InputStateMachine.drainEditorMutations(channel) sequentially applies queued mutations to inputBuffer on frame loop ticks without mutexes or synchronized race conditions.
Conflict Key Definitions
Built-in conflict keys (including ctrl+shift+e) are defined in :jvm:cli at CliShortcutConflicts.BUILT_IN_CONFLICT_KEYS and injected into ShortcutRegistry at construction in TuiModule.