MCPcopy Create free account
hub / github.com/Noumena-Network/code / parseKey

Function parseKey

src/ink/events/input-event.ts:27–192  ·  view source on GitHub ↗
(keypress: ParsedKey)

Source from the content-addressed store, hash-verified

25}
26
27function parseKey(keypress: ParsedKey): [Key, string] {
28 const key: Key = {
29 upArrow: keypress.name === 'up',
30 downArrow: keypress.name === 'down',
31 leftArrow: keypress.name === 'left',
32 rightArrow: keypress.name === 'right',
33 pageDown: keypress.name === 'pagedown',
34 pageUp: keypress.name === 'pageup',
35 wheelUp: keypress.name === 'wheelup',
36 wheelDown: keypress.name === 'wheeldown',
37 home: keypress.name === 'home',
38 end: keypress.name === 'end',
39 // Some terminals surface Enter as `enter`, others as `return`.
40 // Normalize both so the rest of the app sees one submit signal.
41 return: keypress.name === 'return' || keypress.name === 'enter',
42 escape: keypress.name === 'escape',
43 fn: keypress.fn,
44 ctrl: keypress.ctrl,
45 shift: keypress.shift,
46 tab: keypress.name === 'tab',
47 backspace: keypress.name === 'backspace',
48 delete: keypress.name === 'delete',
49 // `parseKeypress` parses \u001B\u001B[A (meta + up arrow) as meta = false
50 // but with option = true, so we need to take this into account here
51 // to avoid breaking changes in Ink.
52 // TODO(vadimdemedes): consider removing this in the next major version.
53 meta: keypress.meta || keypress.name === 'escape' || keypress.option,
54 // Super (Cmd on macOS / Win key) — only arrives via kitty keyboard
55 // protocol CSI u sequences. Distinct from meta (Alt/Option) so
56 // bindings like cmd+c can be expressed separately from opt+c.
57 super: keypress.super,
58 }
59
60 let input = keypress.ctrl ? keypress.name : keypress.sequence
61
62 // Handle undefined input case
63 if (input === undefined) {
64 input = ''
65 }
66
67 // When ctrl is set, keypress.name for space is the literal word "space".
68 // Convert to actual space character for consistency with the CSI u branch
69 // (which maps 'space' → ' '). Without this, ctrl+space leaks the literal
70 // word "space" into text input.
71 if (keypress.ctrl && input === 'space') {
72 input = ' '
73 }
74
75 // Suppress unrecognized escape sequences that were parsed as function keys
76 // (matched by FN_KEY_RE) but have no name in the keyName map.
77 // Examples: ESC[25~ (F13/Right Alt on Windows), ESC[26~ (F14), etc.
78 // Without this, the ESC prefix is stripped below and the remainder (e.g.,
79 // "[25~") leaks into the input as literal text.
80 if (keypress.code && !keypress.name) {
81 input = ''
82 }
83
84 // Suppress ESC-less SGR mouse fragments. When a heavy React commit blocks

Callers 1

constructorMethod · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected