Overview
OpenTUI provides a comprehensive keyboard input handling system that supports standard key events, modifier keys, and the Kitty keyboard protocol for enhanced terminal input.KeyHandler
TheKeyHandler class is the main entry point for handling keyboard input. It extends EventEmitter and emits events for key presses, key releases, and paste operations.
Events
Emitted when text is pasted into the terminal.Callback arguments:
event(PasteEvent) - The paste event object
Usage
KeyEvent
TheKeyEvent interface represents a keyboard event with detailed information about the key pressed and any modifiers.
Properties
The name of the key (e.g., “a”, “return”, “escape”, “up”, “f1”)
Whether the Ctrl/Control modifier key was pressed
Whether the Meta/Alt/Option modifier key was pressed
Whether the Shift modifier key was pressed
Whether the Option/Alt modifier key was pressed (macOS)
Whether the Super/Command modifier key was pressed (requires Kitty protocol)
Whether the Hyper modifier key was pressed (requires Kitty protocol)
The raw escape sequence that was received
Whether the key is a numeric digit (0-9)
The original raw input string
The type of keyboard event:
"press", "repeat", or "release"The source parser that decoded this event
The terminal escape code (if applicable)
Whether Caps Lock was active (requires Kitty protocol)
Whether Num Lock was active (requires Kitty protocol)
The base key code before modifiers (Kitty protocol)
Whether this is a repeated key event (key held down)
Whether
preventDefault() was called on this eventWhether
stopPropagation() was called on this eventMethods
Prevents the default action for this key event from executing
Stops the event from propagating to other handlers
Example
PasteEvent
ThePasteEvent interface represents a paste operation in the terminal (bracketed paste mode).
Properties
The pasted text content (ANSI escape codes are stripped)
Whether
preventDefault() was called on this eventWhether
stopPropagation() was called on this eventMethods
Prevents the default paste handling
Stops the event from propagating to other handlers
Example
Kitty Keyboard Protocol
OpenTUI supports the Kitty keyboard protocol for enhanced keyboard input handling. This protocol provides:- Disambiguated escape codes (fixes ESC timing, alt+key ambiguity)
- Event types (press, repeat, release)
- Alternate keys (numpad vs regular, shifted vs base layout)
- Additional modifiers (Super, Hyper, Caps Lock, Num Lock)
Configuration
Enable the Kitty keyboard protocol when creating the renderer:Options
Disambiguate escape codes to fix ESC timing issues and alt+key ambiguity
Report alternate keys (numpad, shifted, base layout) for cross-keyboard shortcuts
Report event types (press, repeat, release) to enable
keyrelease eventsReport all keys as escape codes
Report text associated with key events
Common Key Names
Special Keys
"return"- Enter/Return key"escape"- Escape key"backspace"- Backspace key"tab"- Tab key"space"- Spacebar"delete"- Delete key
Navigation Keys
"up","down","left","right"- Arrow keys"home","end"- Home and End keys"pageup","pagedown"- Page Up and Page Down
Function Keys
"f1"through"f12"- Function keys
Letters and Numbers
"a"through"z"- Letter keys (lowercase)"0"through"9"- Number keys
Best Practices
Handle event propagation
Handle event propagation
Call
event.stopPropagation() when you handle an event to prevent it from bubbling to other handlers:Check event.defaultPrevented
Check event.defaultPrevented
Before performing an action, check if another handler already prevented the default behavior:
Use keyboard shortcuts consistently
Use keyboard shortcuts consistently
Follow common keyboard shortcut conventions:
- Ctrl+C: Copy (or exit if enabled in config)
- Ctrl+V: Paste
- Ctrl+Z: Undo
- Ctrl+S: Save