Skip to main content

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

The KeyHandler 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

event
Emitted when a key is pressed down.Callback arguments:
  • event (KeyEvent) - The key event object
event
Emitted when a key is released.Callback arguments:
  • event (KeyEvent) - The key event object
event
Emitted when text is pasted into the terminal.Callback arguments:

Usage

KeyEvent

The KeyEvent interface represents a keyboard event with detailed information about the key pressed and any modifiers.

Properties

string
The name of the key (e.g., “a”, “return”, “escape”, “up”, “f1”)
boolean
Whether the Ctrl/Control modifier key was pressed
boolean
Whether the Meta/Alt/Option modifier key was pressed
boolean
Whether the Shift modifier key was pressed
boolean
Whether the Option/Alt modifier key was pressed (macOS)
boolean
Whether the Super/Command modifier key was pressed (requires Kitty protocol)
boolean
Whether the Hyper modifier key was pressed (requires Kitty protocol)
string
The raw escape sequence that was received
boolean
Whether the key is a numeric digit (0-9)
string
The original raw input string
KeyEventType
The type of keyboard event: "press", "repeat", or "release"
'raw' | 'kitty'
The source parser that decoded this event
string
The terminal escape code (if applicable)
boolean
Whether Caps Lock was active (requires Kitty protocol)
boolean
Whether Num Lock was active (requires Kitty protocol)
number
The base key code before modifiers (Kitty protocol)
boolean
Whether this is a repeated key event (key held down)
boolean
Whether preventDefault() was called on this event
boolean
Whether stopPropagation() was called on this event

Methods

() => void
Prevents the default action for this key event from executing
() => void
Stops the event from propagating to other handlers

Example

PasteEvent

The PasteEvent interface represents a paste operation in the terminal (bracketed paste mode).

Properties

string
The pasted text content (ANSI escape codes are stripped)
boolean
Whether preventDefault() was called on this event
boolean
Whether stopPropagation() was called on this event

Methods

() => void
Prevents the default paste handling
() => void
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

boolean
default:"true"
Disambiguate escape codes to fix ESC timing issues and alt+key ambiguity
boolean
default:"true"
Report alternate keys (numpad, shifted, base layout) for cross-keyboard shortcuts
boolean
default:"false"
Report event types (press, repeat, release) to enable keyrelease events
boolean
default:"false"
Report all keys as escape codes
boolean
default:"false"
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
  • "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

Call event.stopPropagation() when you handle an event to prevent it from bubbling to other handlers:
Before performing an action, check if another handler already prevented the default behavior:
Follow common keyboard shortcut conventions:
  • Ctrl+C: Copy (or exit if enabled in config)
  • Ctrl+V: Paste
  • Ctrl+Z: Undo
  • Ctrl+S: Save