import type { ParsedKey } from '../parse-keypress.js' import { TerminalEvent } from './terminal-event.js' /** Keyboard event dispatched through the rendered tree via capture / bubble. */ export class KeyboardEvent extends TerminalEvent { readonly key: string readonly ctrl: boolean readonly shift: boolean readonly meta: boolean readonly superKey: boolean readonly fn: boolean constructor(parsedKey: ParsedKey) { super('keydown', { bubbles: true, cancelable: true }) this.key = keyFromParsed(parsedKey) this.ctrl = parsedKey.ctrl this.shift = parsedKey.shift // Meta and Option both come over the wire as ESC-prefixed sequences // that parseKeypress can't always distinguish; we collapse them so // user code doesn't have to branch on the source terminal. this.meta = parsedKey.meta || parsedKey.option this.superKey = parsedKey.super this.fn = parsedKey.fn } } /** Pick the right `key` value out of a parsed keypress. */ function keyFromParsed(parsed: ParsedKey): string { const seq = parsed.sequence ?? '' const name = parsed.name ?? '' if (parsed.ctrl) return name if (seq.length === 1) { const code = seq.charCodeAt(0) if (code >= 0x20 && code !== 0x7f) return seq } return name || seq }