Skip to content
Updated April 2026Edit this page ↗

Input parser

The InputParser sits between raw stdin and your app. It reads bytes off the stream, decodes escape sequences, and fires typed KeyEvent and MouseEvent callbacks.

The App creates one for you, but you have direct access if you need lower-level control.

Usage

TYPESCRIPT
import { InputParser } from '@termuijs/core'
 
const parser = new InputParser(process.stdin)
 
parser.onKey((event) => {
    console.log(event.key, event.ctrl, event.shift, event.alt)
})
 
parser.onMouse((event) => {
    console.log(event.x, event.y, event.button)
})
 
// Start reading from stdin
parser.start()
 
// Later, stop reading
parser.stop()

KeyEvent

TYPESCRIPT
interface KeyEvent {
    key: string        // 'a', 'enter', 'up', 'escape', etc.
    ctrl: boolean      // Ctrl was held
    shift: boolean     // Shift was held
    alt: boolean       // Alt/Option was held
    raw: Buffer        // The original bytes from stdin
}

Supported keys

CategoryKeys
PrintableAll ASCII characters
Navigationup, down, left, right, home, end, pageup, pagedown
Controlenter, tab, escape, backspace, delete, space
Functionf1 through f12
Modifiersctrl+*, shift+*, alt+*

API

MethodWhat it does
onKey(handler)Register a key event callback. Returns unsubscribe function.
onMouse(handler)Register a mouse event callback. Returns unsubscribe function.
start()Begin reading from stdin.
stop()Stop reading. Can be restarted later.