@draht/tui 2026.3.2-2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (102) hide show
  1. package/README.md +761 -0
  2. package/dist/autocomplete.d.ts +50 -0
  3. package/dist/autocomplete.d.ts.map +1 -0
  4. package/dist/autocomplete.js +596 -0
  5. package/dist/autocomplete.js.map +1 -0
  6. package/dist/components/box.d.ts +22 -0
  7. package/dist/components/box.d.ts.map +1 -0
  8. package/dist/components/box.js +104 -0
  9. package/dist/components/box.js.map +1 -0
  10. package/dist/components/cancellable-loader.d.ts +22 -0
  11. package/dist/components/cancellable-loader.d.ts.map +1 -0
  12. package/dist/components/cancellable-loader.js +35 -0
  13. package/dist/components/cancellable-loader.js.map +1 -0
  14. package/dist/components/editor.d.ts +205 -0
  15. package/dist/components/editor.d.ts.map +1 -0
  16. package/dist/components/editor.js +1679 -0
  17. package/dist/components/editor.js.map +1 -0
  18. package/dist/components/image.d.ts +28 -0
  19. package/dist/components/image.d.ts.map +1 -0
  20. package/dist/components/image.js +69 -0
  21. package/dist/components/image.js.map +1 -0
  22. package/dist/components/input.d.ts +37 -0
  23. package/dist/components/input.d.ts.map +1 -0
  24. package/dist/components/input.js +433 -0
  25. package/dist/components/input.js.map +1 -0
  26. package/dist/components/loader.d.ts +21 -0
  27. package/dist/components/loader.d.ts.map +1 -0
  28. package/dist/components/loader.js +49 -0
  29. package/dist/components/loader.js.map +1 -0
  30. package/dist/components/markdown.d.ts +95 -0
  31. package/dist/components/markdown.d.ts.map +1 -0
  32. package/dist/components/markdown.js +629 -0
  33. package/dist/components/markdown.js.map +1 -0
  34. package/dist/components/select-list.d.ts +32 -0
  35. package/dist/components/select-list.d.ts.map +1 -0
  36. package/dist/components/select-list.js +152 -0
  37. package/dist/components/select-list.js.map +1 -0
  38. package/dist/components/settings-list.d.ts +50 -0
  39. package/dist/components/settings-list.d.ts.map +1 -0
  40. package/dist/components/settings-list.js +185 -0
  41. package/dist/components/settings-list.js.map +1 -0
  42. package/dist/components/spacer.d.ts +12 -0
  43. package/dist/components/spacer.d.ts.map +1 -0
  44. package/dist/components/spacer.js +23 -0
  45. package/dist/components/spacer.js.map +1 -0
  46. package/dist/components/text.d.ts +19 -0
  47. package/dist/components/text.d.ts.map +1 -0
  48. package/dist/components/text.js +89 -0
  49. package/dist/components/text.js.map +1 -0
  50. package/dist/components/truncated-text.d.ts +13 -0
  51. package/dist/components/truncated-text.d.ts.map +1 -0
  52. package/dist/components/truncated-text.js +51 -0
  53. package/dist/components/truncated-text.js.map +1 -0
  54. package/dist/editor-component.d.ts +39 -0
  55. package/dist/editor-component.d.ts.map +1 -0
  56. package/dist/editor-component.js +2 -0
  57. package/dist/editor-component.js.map +1 -0
  58. package/dist/fuzzy.d.ts +16 -0
  59. package/dist/fuzzy.d.ts.map +1 -0
  60. package/dist/fuzzy.js +107 -0
  61. package/dist/fuzzy.js.map +1 -0
  62. package/dist/index.d.ts +23 -0
  63. package/dist/index.d.ts.map +1 -0
  64. package/dist/index.js +32 -0
  65. package/dist/index.js.map +1 -0
  66. package/dist/keybindings.d.ts +39 -0
  67. package/dist/keybindings.d.ts.map +1 -0
  68. package/dist/keybindings.js +114 -0
  69. package/dist/keybindings.js.map +1 -0
  70. package/dist/keys.d.ts +160 -0
  71. package/dist/keys.d.ts.map +1 -0
  72. package/dist/keys.js +959 -0
  73. package/dist/keys.js.map +1 -0
  74. package/dist/kill-ring.d.ts +28 -0
  75. package/dist/kill-ring.d.ts.map +1 -0
  76. package/dist/kill-ring.js +44 -0
  77. package/dist/kill-ring.js.map +1 -0
  78. package/dist/stdin-buffer.d.ts +48 -0
  79. package/dist/stdin-buffer.d.ts.map +1 -0
  80. package/dist/stdin-buffer.js +317 -0
  81. package/dist/stdin-buffer.js.map +1 -0
  82. package/dist/terminal-image.d.ts +68 -0
  83. package/dist/terminal-image.d.ts.map +1 -0
  84. package/dist/terminal-image.js +288 -0
  85. package/dist/terminal-image.js.map +1 -0
  86. package/dist/terminal.d.ts +78 -0
  87. package/dist/terminal.d.ts.map +1 -0
  88. package/dist/terminal.js +249 -0
  89. package/dist/terminal.js.map +1 -0
  90. package/dist/tui.d.ts +210 -0
  91. package/dist/tui.d.ts.map +1 -0
  92. package/dist/tui.js +955 -0
  93. package/dist/tui.js.map +1 -0
  94. package/dist/undo-stack.d.ts +17 -0
  95. package/dist/undo-stack.d.ts.map +1 -0
  96. package/dist/undo-stack.js +25 -0
  97. package/dist/undo-stack.js.map +1 -0
  98. package/dist/utils.d.ts +78 -0
  99. package/dist/utils.d.ts.map +1 -0
  100. package/dist/utils.js +800 -0
  101. package/dist/utils.js.map +1 -0
  102. package/package.json +53 -0
package/dist/keys.js ADDED
@@ -0,0 +1,959 @@
1
+ /**
2
+ * Keyboard input handling for terminal applications.
3
+ *
4
+ * Supports both legacy terminal sequences and Kitty keyboard protocol.
5
+ * See: https://sw.kovidgoyal.net/kitty/keyboard-protocol/
6
+ * Reference: https://github.com/sst/opentui/blob/7da92b4088aebfe27b9f691c04163a48821e49fd/packages/core/src/lib/parse.keypress.ts
7
+ *
8
+ * Symbol keys are also supported, however some ctrl+symbol combos
9
+ * overlap with ASCII codes, e.g. ctrl+[ = ESC.
10
+ * See: https://sw.kovidgoyal.net/kitty/keyboard-protocol/#legacy-ctrl-mapping-of-ascii-keys
11
+ * Those can still be * used for ctrl+shift combos
12
+ *
13
+ * API:
14
+ * - matchesKey(data, keyId) - Check if input matches a key identifier
15
+ * - parseKey(data) - Parse input and return the key identifier
16
+ * - Key - Helper object for creating typed key identifiers
17
+ * - setKittyProtocolActive(active) - Set global Kitty protocol state
18
+ * - isKittyProtocolActive() - Query global Kitty protocol state
19
+ */
20
+ // =============================================================================
21
+ // Global Kitty Protocol State
22
+ // =============================================================================
23
+ let _kittyProtocolActive = false;
24
+ /**
25
+ * Set the global Kitty keyboard protocol state.
26
+ * Called by ProcessTerminal after detecting protocol support.
27
+ */
28
+ export function setKittyProtocolActive(active) {
29
+ _kittyProtocolActive = active;
30
+ }
31
+ /**
32
+ * Query whether Kitty keyboard protocol is currently active.
33
+ */
34
+ export function isKittyProtocolActive() {
35
+ return _kittyProtocolActive;
36
+ }
37
+ /**
38
+ * Helper object for creating typed key identifiers with autocomplete.
39
+ *
40
+ * Usage:
41
+ * - Key.escape, Key.enter, Key.tab, etc. for special keys
42
+ * - Key.backtick, Key.comma, Key.period, etc. for symbol keys
43
+ * - Key.ctrl("c"), Key.alt("x") for single modifier
44
+ * - Key.ctrlShift("p"), Key.ctrlAlt("x") for combined modifiers
45
+ */
46
+ export const Key = {
47
+ // Special keys
48
+ escape: "escape",
49
+ esc: "esc",
50
+ enter: "enter",
51
+ return: "return",
52
+ tab: "tab",
53
+ space: "space",
54
+ backspace: "backspace",
55
+ delete: "delete",
56
+ insert: "insert",
57
+ clear: "clear",
58
+ home: "home",
59
+ end: "end",
60
+ pageUp: "pageUp",
61
+ pageDown: "pageDown",
62
+ up: "up",
63
+ down: "down",
64
+ left: "left",
65
+ right: "right",
66
+ f1: "f1",
67
+ f2: "f2",
68
+ f3: "f3",
69
+ f4: "f4",
70
+ f5: "f5",
71
+ f6: "f6",
72
+ f7: "f7",
73
+ f8: "f8",
74
+ f9: "f9",
75
+ f10: "f10",
76
+ f11: "f11",
77
+ f12: "f12",
78
+ // Symbol keys
79
+ backtick: "`",
80
+ hyphen: "-",
81
+ equals: "=",
82
+ leftbracket: "[",
83
+ rightbracket: "]",
84
+ backslash: "\\",
85
+ semicolon: ";",
86
+ quote: "'",
87
+ comma: ",",
88
+ period: ".",
89
+ slash: "/",
90
+ exclamation: "!",
91
+ at: "@",
92
+ hash: "#",
93
+ dollar: "$",
94
+ percent: "%",
95
+ caret: "^",
96
+ ampersand: "&",
97
+ asterisk: "*",
98
+ leftparen: "(",
99
+ rightparen: ")",
100
+ underscore: "_",
101
+ plus: "+",
102
+ pipe: "|",
103
+ tilde: "~",
104
+ leftbrace: "{",
105
+ rightbrace: "}",
106
+ colon: ":",
107
+ lessthan: "<",
108
+ greaterthan: ">",
109
+ question: "?",
110
+ // Single modifiers
111
+ ctrl: (key) => `ctrl+${key}`,
112
+ shift: (key) => `shift+${key}`,
113
+ alt: (key) => `alt+${key}`,
114
+ // Combined modifiers
115
+ ctrlShift: (key) => `ctrl+shift+${key}`,
116
+ shiftCtrl: (key) => `shift+ctrl+${key}`,
117
+ ctrlAlt: (key) => `ctrl+alt+${key}`,
118
+ altCtrl: (key) => `alt+ctrl+${key}`,
119
+ shiftAlt: (key) => `shift+alt+${key}`,
120
+ altShift: (key) => `alt+shift+${key}`,
121
+ // Triple modifiers
122
+ ctrlShiftAlt: (key) => `ctrl+shift+alt+${key}`,
123
+ };
124
+ // =============================================================================
125
+ // Constants
126
+ // =============================================================================
127
+ const SYMBOL_KEYS = new Set([
128
+ "`",
129
+ "-",
130
+ "=",
131
+ "[",
132
+ "]",
133
+ "\\",
134
+ ";",
135
+ "'",
136
+ ",",
137
+ ".",
138
+ "/",
139
+ "!",
140
+ "@",
141
+ "#",
142
+ "$",
143
+ "%",
144
+ "^",
145
+ "&",
146
+ "*",
147
+ "(",
148
+ ")",
149
+ "_",
150
+ "+",
151
+ "|",
152
+ "~",
153
+ "{",
154
+ "}",
155
+ ":",
156
+ "<",
157
+ ">",
158
+ "?",
159
+ ]);
160
+ const MODIFIERS = {
161
+ shift: 1,
162
+ alt: 2,
163
+ ctrl: 4,
164
+ };
165
+ const LOCK_MASK = 64 + 128; // Caps Lock + Num Lock
166
+ const CODEPOINTS = {
167
+ escape: 27,
168
+ tab: 9,
169
+ enter: 13,
170
+ space: 32,
171
+ backspace: 127,
172
+ kpEnter: 57414, // Numpad Enter (Kitty protocol)
173
+ };
174
+ const ARROW_CODEPOINTS = {
175
+ up: -1,
176
+ down: -2,
177
+ right: -3,
178
+ left: -4,
179
+ };
180
+ const FUNCTIONAL_CODEPOINTS = {
181
+ delete: -10,
182
+ insert: -11,
183
+ pageUp: -12,
184
+ pageDown: -13,
185
+ home: -14,
186
+ end: -15,
187
+ };
188
+ const LEGACY_KEY_SEQUENCES = {
189
+ up: ["\x1b[A", "\x1bOA"],
190
+ down: ["\x1b[B", "\x1bOB"],
191
+ right: ["\x1b[C", "\x1bOC"],
192
+ left: ["\x1b[D", "\x1bOD"],
193
+ home: ["\x1b[H", "\x1bOH", "\x1b[1~", "\x1b[7~"],
194
+ end: ["\x1b[F", "\x1bOF", "\x1b[4~", "\x1b[8~"],
195
+ insert: ["\x1b[2~"],
196
+ delete: ["\x1b[3~"],
197
+ pageUp: ["\x1b[5~", "\x1b[[5~"],
198
+ pageDown: ["\x1b[6~", "\x1b[[6~"],
199
+ clear: ["\x1b[E", "\x1bOE"],
200
+ f1: ["\x1bOP", "\x1b[11~", "\x1b[[A"],
201
+ f2: ["\x1bOQ", "\x1b[12~", "\x1b[[B"],
202
+ f3: ["\x1bOR", "\x1b[13~", "\x1b[[C"],
203
+ f4: ["\x1bOS", "\x1b[14~", "\x1b[[D"],
204
+ f5: ["\x1b[15~", "\x1b[[E"],
205
+ f6: ["\x1b[17~"],
206
+ f7: ["\x1b[18~"],
207
+ f8: ["\x1b[19~"],
208
+ f9: ["\x1b[20~"],
209
+ f10: ["\x1b[21~"],
210
+ f11: ["\x1b[23~"],
211
+ f12: ["\x1b[24~"],
212
+ };
213
+ const LEGACY_SHIFT_SEQUENCES = {
214
+ up: ["\x1b[a"],
215
+ down: ["\x1b[b"],
216
+ right: ["\x1b[c"],
217
+ left: ["\x1b[d"],
218
+ clear: ["\x1b[e"],
219
+ insert: ["\x1b[2$"],
220
+ delete: ["\x1b[3$"],
221
+ pageUp: ["\x1b[5$"],
222
+ pageDown: ["\x1b[6$"],
223
+ home: ["\x1b[7$"],
224
+ end: ["\x1b[8$"],
225
+ };
226
+ const LEGACY_CTRL_SEQUENCES = {
227
+ up: ["\x1bOa"],
228
+ down: ["\x1bOb"],
229
+ right: ["\x1bOc"],
230
+ left: ["\x1bOd"],
231
+ clear: ["\x1bOe"],
232
+ insert: ["\x1b[2^"],
233
+ delete: ["\x1b[3^"],
234
+ pageUp: ["\x1b[5^"],
235
+ pageDown: ["\x1b[6^"],
236
+ home: ["\x1b[7^"],
237
+ end: ["\x1b[8^"],
238
+ };
239
+ const LEGACY_SEQUENCE_KEY_IDS = {
240
+ "\x1bOA": "up",
241
+ "\x1bOB": "down",
242
+ "\x1bOC": "right",
243
+ "\x1bOD": "left",
244
+ "\x1bOH": "home",
245
+ "\x1bOF": "end",
246
+ "\x1b[E": "clear",
247
+ "\x1bOE": "clear",
248
+ "\x1bOe": "ctrl+clear",
249
+ "\x1b[e": "shift+clear",
250
+ "\x1b[2~": "insert",
251
+ "\x1b[2$": "shift+insert",
252
+ "\x1b[2^": "ctrl+insert",
253
+ "\x1b[3$": "shift+delete",
254
+ "\x1b[3^": "ctrl+delete",
255
+ "\x1b[[5~": "pageUp",
256
+ "\x1b[[6~": "pageDown",
257
+ "\x1b[a": "shift+up",
258
+ "\x1b[b": "shift+down",
259
+ "\x1b[c": "shift+right",
260
+ "\x1b[d": "shift+left",
261
+ "\x1bOa": "ctrl+up",
262
+ "\x1bOb": "ctrl+down",
263
+ "\x1bOc": "ctrl+right",
264
+ "\x1bOd": "ctrl+left",
265
+ "\x1b[5$": "shift+pageUp",
266
+ "\x1b[6$": "shift+pageDown",
267
+ "\x1b[7$": "shift+home",
268
+ "\x1b[8$": "shift+end",
269
+ "\x1b[5^": "ctrl+pageUp",
270
+ "\x1b[6^": "ctrl+pageDown",
271
+ "\x1b[7^": "ctrl+home",
272
+ "\x1b[8^": "ctrl+end",
273
+ "\x1bOP": "f1",
274
+ "\x1bOQ": "f2",
275
+ "\x1bOR": "f3",
276
+ "\x1bOS": "f4",
277
+ "\x1b[11~": "f1",
278
+ "\x1b[12~": "f2",
279
+ "\x1b[13~": "f3",
280
+ "\x1b[14~": "f4",
281
+ "\x1b[[A": "f1",
282
+ "\x1b[[B": "f2",
283
+ "\x1b[[C": "f3",
284
+ "\x1b[[D": "f4",
285
+ "\x1b[[E": "f5",
286
+ "\x1b[15~": "f5",
287
+ "\x1b[17~": "f6",
288
+ "\x1b[18~": "f7",
289
+ "\x1b[19~": "f8",
290
+ "\x1b[20~": "f9",
291
+ "\x1b[21~": "f10",
292
+ "\x1b[23~": "f11",
293
+ "\x1b[24~": "f12",
294
+ "\x1bb": "alt+left",
295
+ "\x1bf": "alt+right",
296
+ "\x1bp": "alt+up",
297
+ "\x1bn": "alt+down",
298
+ };
299
+ const matchesLegacySequence = (data, sequences) => sequences.includes(data);
300
+ const matchesLegacyModifierSequence = (data, key, modifier) => {
301
+ if (modifier === MODIFIERS.shift) {
302
+ return matchesLegacySequence(data, LEGACY_SHIFT_SEQUENCES[key]);
303
+ }
304
+ if (modifier === MODIFIERS.ctrl) {
305
+ return matchesLegacySequence(data, LEGACY_CTRL_SEQUENCES[key]);
306
+ }
307
+ return false;
308
+ };
309
+ // Store the last parsed event type for isKeyRelease() to query
310
+ let _lastEventType = "press";
311
+ /**
312
+ * Check if the last parsed key event was a key release.
313
+ * Only meaningful when Kitty keyboard protocol with flag 2 is active.
314
+ */
315
+ export function isKeyRelease(data) {
316
+ // Don't treat bracketed paste content as key release, even if it contains
317
+ // patterns like ":3F" (e.g., bluetooth MAC addresses like "90:62:3F:A5").
318
+ // Terminal.ts re-wraps paste content with bracketed paste markers before
319
+ // passing to TUI, so pasted data will always contain \x1b[200~.
320
+ if (data.includes("\x1b[200~")) {
321
+ return false;
322
+ }
323
+ // Quick check: release events with flag 2 contain ":3"
324
+ // Format: \x1b[<codepoint>;<modifier>:3u
325
+ if (data.includes(":3u") ||
326
+ data.includes(":3~") ||
327
+ data.includes(":3A") ||
328
+ data.includes(":3B") ||
329
+ data.includes(":3C") ||
330
+ data.includes(":3D") ||
331
+ data.includes(":3H") ||
332
+ data.includes(":3F")) {
333
+ return true;
334
+ }
335
+ return false;
336
+ }
337
+ /**
338
+ * Check if the last parsed key event was a key repeat.
339
+ * Only meaningful when Kitty keyboard protocol with flag 2 is active.
340
+ */
341
+ export function isKeyRepeat(data) {
342
+ // Don't treat bracketed paste content as key repeat, even if it contains
343
+ // patterns like ":2F". See isKeyRelease() for details.
344
+ if (data.includes("\x1b[200~")) {
345
+ return false;
346
+ }
347
+ if (data.includes(":2u") ||
348
+ data.includes(":2~") ||
349
+ data.includes(":2A") ||
350
+ data.includes(":2B") ||
351
+ data.includes(":2C") ||
352
+ data.includes(":2D") ||
353
+ data.includes(":2H") ||
354
+ data.includes(":2F")) {
355
+ return true;
356
+ }
357
+ return false;
358
+ }
359
+ function parseEventType(eventTypeStr) {
360
+ if (!eventTypeStr)
361
+ return "press";
362
+ const eventType = parseInt(eventTypeStr, 10);
363
+ if (eventType === 2)
364
+ return "repeat";
365
+ if (eventType === 3)
366
+ return "release";
367
+ return "press";
368
+ }
369
+ function parseKittySequence(data) {
370
+ // CSI u format with alternate keys (flag 4):
371
+ // \x1b[<codepoint>u
372
+ // \x1b[<codepoint>;<mod>u
373
+ // \x1b[<codepoint>;<mod>:<event>u
374
+ // \x1b[<codepoint>:<shifted>;<mod>u
375
+ // \x1b[<codepoint>:<shifted>:<base>;<mod>u
376
+ // \x1b[<codepoint>::<base>;<mod>u (no shifted key, only base)
377
+ //
378
+ // With flag 2, event type is appended after modifier colon: 1=press, 2=repeat, 3=release
379
+ // With flag 4, alternate keys are appended after codepoint with colons
380
+ const csiUMatch = data.match(/^\x1b\[(\d+)(?::(\d*))?(?::(\d+))?(?:;(\d+))?(?::(\d+))?u$/);
381
+ if (csiUMatch) {
382
+ const codepoint = parseInt(csiUMatch[1], 10);
383
+ const shiftedKey = csiUMatch[2] && csiUMatch[2].length > 0 ? parseInt(csiUMatch[2], 10) : undefined;
384
+ const baseLayoutKey = csiUMatch[3] ? parseInt(csiUMatch[3], 10) : undefined;
385
+ const modValue = csiUMatch[4] ? parseInt(csiUMatch[4], 10) : 1;
386
+ const eventType = parseEventType(csiUMatch[5]);
387
+ _lastEventType = eventType;
388
+ return { codepoint, shiftedKey, baseLayoutKey, modifier: modValue - 1, eventType };
389
+ }
390
+ // Arrow keys with modifier: \x1b[1;<mod>A/B/C/D or \x1b[1;<mod>:<event>A/B/C/D
391
+ const arrowMatch = data.match(/^\x1b\[1;(\d+)(?::(\d+))?([ABCD])$/);
392
+ if (arrowMatch) {
393
+ const modValue = parseInt(arrowMatch[1], 10);
394
+ const eventType = parseEventType(arrowMatch[2]);
395
+ const arrowCodes = { A: -1, B: -2, C: -3, D: -4 };
396
+ _lastEventType = eventType;
397
+ return { codepoint: arrowCodes[arrowMatch[3]], modifier: modValue - 1, eventType };
398
+ }
399
+ // Functional keys: \x1b[<num>~ or \x1b[<num>;<mod>~ or \x1b[<num>;<mod>:<event>~
400
+ const funcMatch = data.match(/^\x1b\[(\d+)(?:;(\d+))?(?::(\d+))?~$/);
401
+ if (funcMatch) {
402
+ const keyNum = parseInt(funcMatch[1], 10);
403
+ const modValue = funcMatch[2] ? parseInt(funcMatch[2], 10) : 1;
404
+ const eventType = parseEventType(funcMatch[3]);
405
+ const funcCodes = {
406
+ 2: FUNCTIONAL_CODEPOINTS.insert,
407
+ 3: FUNCTIONAL_CODEPOINTS.delete,
408
+ 5: FUNCTIONAL_CODEPOINTS.pageUp,
409
+ 6: FUNCTIONAL_CODEPOINTS.pageDown,
410
+ 7: FUNCTIONAL_CODEPOINTS.home,
411
+ 8: FUNCTIONAL_CODEPOINTS.end,
412
+ };
413
+ const codepoint = funcCodes[keyNum];
414
+ if (codepoint !== undefined) {
415
+ _lastEventType = eventType;
416
+ return { codepoint, modifier: modValue - 1, eventType };
417
+ }
418
+ }
419
+ // Home/End with modifier: \x1b[1;<mod>H/F or \x1b[1;<mod>:<event>H/F
420
+ const homeEndMatch = data.match(/^\x1b\[1;(\d+)(?::(\d+))?([HF])$/);
421
+ if (homeEndMatch) {
422
+ const modValue = parseInt(homeEndMatch[1], 10);
423
+ const eventType = parseEventType(homeEndMatch[2]);
424
+ const codepoint = homeEndMatch[3] === "H" ? FUNCTIONAL_CODEPOINTS.home : FUNCTIONAL_CODEPOINTS.end;
425
+ _lastEventType = eventType;
426
+ return { codepoint, modifier: modValue - 1, eventType };
427
+ }
428
+ return null;
429
+ }
430
+ function matchesKittySequence(data, expectedCodepoint, expectedModifier) {
431
+ const parsed = parseKittySequence(data);
432
+ if (!parsed)
433
+ return false;
434
+ const actualMod = parsed.modifier & ~LOCK_MASK;
435
+ const expectedMod = expectedModifier & ~LOCK_MASK;
436
+ // Check if modifiers match
437
+ if (actualMod !== expectedMod)
438
+ return false;
439
+ // Primary match: codepoint matches directly
440
+ if (parsed.codepoint === expectedCodepoint)
441
+ return true;
442
+ // Alternate match: use base layout key for non-Latin keyboard layouts.
443
+ // This allows Ctrl+С (Cyrillic) to match Ctrl+c (Latin) when terminal reports
444
+ // the base layout key (the key in standard PC-101 layout).
445
+ //
446
+ // Only fall back to base layout key when the codepoint is NOT already a
447
+ // recognized Latin letter (a-z) or symbol (e.g., /, -, [, ;, etc.).
448
+ // When the codepoint is a recognized key, it is authoritative regardless
449
+ // of physical key position. This prevents remapped layouts (Dvorak, Colemak,
450
+ // xremap, etc.) from causing false matches: both letters and symbols move
451
+ // to different physical positions, so Ctrl+K could falsely match Ctrl+V
452
+ // (letter remapping) and Ctrl+/ could falsely match Ctrl+[ (symbol remapping)
453
+ // if the base layout key were always considered.
454
+ if (parsed.baseLayoutKey !== undefined && parsed.baseLayoutKey === expectedCodepoint) {
455
+ const cp = parsed.codepoint;
456
+ const isLatinLetter = cp >= 97 && cp <= 122; // a-z
457
+ const isKnownSymbol = SYMBOL_KEYS.has(String.fromCharCode(cp));
458
+ if (!isLatinLetter && !isKnownSymbol)
459
+ return true;
460
+ }
461
+ return false;
462
+ }
463
+ /**
464
+ * Match xterm modifyOtherKeys format: CSI 27 ; modifiers ; keycode ~
465
+ * This is used by terminals when Kitty protocol is not enabled.
466
+ * Modifier values are 1-indexed: 2=shift, 3=alt, 5=ctrl, etc.
467
+ */
468
+ function matchesModifyOtherKeys(data, expectedKeycode, expectedModifier) {
469
+ const match = data.match(/^\x1b\[27;(\d+);(\d+)~$/);
470
+ if (!match)
471
+ return false;
472
+ const modValue = parseInt(match[1], 10);
473
+ const keycode = parseInt(match[2], 10);
474
+ // Convert from 1-indexed xterm format to our 0-indexed format
475
+ const actualMod = modValue - 1;
476
+ return keycode === expectedKeycode && actualMod === expectedModifier;
477
+ }
478
+ // =============================================================================
479
+ // Generic Key Matching
480
+ // =============================================================================
481
+ /**
482
+ * Get the control character for a key.
483
+ * Uses the universal formula: code & 0x1f (mask to lower 5 bits)
484
+ *
485
+ * Works for:
486
+ * - Letters a-z → 1-26
487
+ * - Symbols [\]_ → 27, 28, 29, 31
488
+ * - Also maps - to same as _ (same physical key on US keyboards)
489
+ */
490
+ function rawCtrlChar(key) {
491
+ const char = key.toLowerCase();
492
+ const code = char.charCodeAt(0);
493
+ if ((code >= 97 && code <= 122) || char === "[" || char === "\\" || char === "]" || char === "_") {
494
+ return String.fromCharCode(code & 0x1f);
495
+ }
496
+ // Handle - as _ (same physical key on US keyboards)
497
+ if (char === "-") {
498
+ return String.fromCharCode(31); // Same as Ctrl+_
499
+ }
500
+ return null;
501
+ }
502
+ function parseKeyId(keyId) {
503
+ const parts = keyId.toLowerCase().split("+");
504
+ const key = parts[parts.length - 1];
505
+ if (!key)
506
+ return null;
507
+ return {
508
+ key,
509
+ ctrl: parts.includes("ctrl"),
510
+ shift: parts.includes("shift"),
511
+ alt: parts.includes("alt"),
512
+ };
513
+ }
514
+ /**
515
+ * Match input data against a key identifier string.
516
+ *
517
+ * Supported key identifiers:
518
+ * - Single keys: "escape", "tab", "enter", "backspace", "delete", "home", "end", "space"
519
+ * - Arrow keys: "up", "down", "left", "right"
520
+ * - Ctrl combinations: "ctrl+c", "ctrl+z", etc.
521
+ * - Shift combinations: "shift+tab", "shift+enter"
522
+ * - Alt combinations: "alt+enter", "alt+backspace"
523
+ * - Combined modifiers: "shift+ctrl+p", "ctrl+alt+x"
524
+ *
525
+ * Use the Key helper for autocomplete: Key.ctrl("c"), Key.escape, Key.ctrlShift("p")
526
+ *
527
+ * @param data - Raw input data from terminal
528
+ * @param keyId - Key identifier (e.g., "ctrl+c", "escape", Key.ctrl("c"))
529
+ */
530
+ export function matchesKey(data, keyId) {
531
+ const parsed = parseKeyId(keyId);
532
+ if (!parsed)
533
+ return false;
534
+ const { key, ctrl, shift, alt } = parsed;
535
+ let modifier = 0;
536
+ if (shift)
537
+ modifier |= MODIFIERS.shift;
538
+ if (alt)
539
+ modifier |= MODIFIERS.alt;
540
+ if (ctrl)
541
+ modifier |= MODIFIERS.ctrl;
542
+ switch (key) {
543
+ case "escape":
544
+ case "esc":
545
+ if (modifier !== 0)
546
+ return false;
547
+ return data === "\x1b" || matchesKittySequence(data, CODEPOINTS.escape, 0);
548
+ case "space":
549
+ if (!_kittyProtocolActive) {
550
+ if (ctrl && !alt && !shift && data === "\x00") {
551
+ return true;
552
+ }
553
+ if (alt && !ctrl && !shift && data === "\x1b ") {
554
+ return true;
555
+ }
556
+ }
557
+ if (modifier === 0) {
558
+ return data === " " || matchesKittySequence(data, CODEPOINTS.space, 0);
559
+ }
560
+ return matchesKittySequence(data, CODEPOINTS.space, modifier);
561
+ case "tab":
562
+ if (shift && !ctrl && !alt) {
563
+ return data === "\x1b[Z" || matchesKittySequence(data, CODEPOINTS.tab, MODIFIERS.shift);
564
+ }
565
+ if (modifier === 0) {
566
+ return data === "\t" || matchesKittySequence(data, CODEPOINTS.tab, 0);
567
+ }
568
+ return matchesKittySequence(data, CODEPOINTS.tab, modifier);
569
+ case "enter":
570
+ case "return":
571
+ if (shift && !ctrl && !alt) {
572
+ // CSI u sequences (standard Kitty protocol)
573
+ if (matchesKittySequence(data, CODEPOINTS.enter, MODIFIERS.shift) ||
574
+ matchesKittySequence(data, CODEPOINTS.kpEnter, MODIFIERS.shift)) {
575
+ return true;
576
+ }
577
+ // xterm modifyOtherKeys format (fallback when Kitty protocol not enabled)
578
+ if (matchesModifyOtherKeys(data, CODEPOINTS.enter, MODIFIERS.shift)) {
579
+ return true;
580
+ }
581
+ // When Kitty protocol is active, legacy sequences are custom terminal mappings
582
+ // \x1b\r = Kitty's "map shift+enter send_text all \e\r"
583
+ // \n = Ghostty's "keybind = shift+enter=text:\n"
584
+ if (_kittyProtocolActive) {
585
+ return data === "\x1b\r" || data === "\n";
586
+ }
587
+ return false;
588
+ }
589
+ if (alt && !ctrl && !shift) {
590
+ // CSI u sequences (standard Kitty protocol)
591
+ if (matchesKittySequence(data, CODEPOINTS.enter, MODIFIERS.alt) ||
592
+ matchesKittySequence(data, CODEPOINTS.kpEnter, MODIFIERS.alt)) {
593
+ return true;
594
+ }
595
+ // xterm modifyOtherKeys format (fallback when Kitty protocol not enabled)
596
+ if (matchesModifyOtherKeys(data, CODEPOINTS.enter, MODIFIERS.alt)) {
597
+ return true;
598
+ }
599
+ // \x1b\r is alt+enter only in legacy mode (no Kitty protocol)
600
+ // When Kitty protocol is active, alt+enter comes as CSI u sequence
601
+ if (!_kittyProtocolActive) {
602
+ return data === "\x1b\r";
603
+ }
604
+ return false;
605
+ }
606
+ if (modifier === 0) {
607
+ return (data === "\r" ||
608
+ (!_kittyProtocolActive && data === "\n") ||
609
+ data === "\x1bOM" || // SS3 M (numpad enter in some terminals)
610
+ matchesKittySequence(data, CODEPOINTS.enter, 0) ||
611
+ matchesKittySequence(data, CODEPOINTS.kpEnter, 0));
612
+ }
613
+ return (matchesKittySequence(data, CODEPOINTS.enter, modifier) ||
614
+ matchesKittySequence(data, CODEPOINTS.kpEnter, modifier));
615
+ case "backspace":
616
+ if (alt && !ctrl && !shift) {
617
+ if (data === "\x1b\x7f" || data === "\x1b\b") {
618
+ return true;
619
+ }
620
+ return matchesKittySequence(data, CODEPOINTS.backspace, MODIFIERS.alt);
621
+ }
622
+ if (modifier === 0) {
623
+ return data === "\x7f" || data === "\x08" || matchesKittySequence(data, CODEPOINTS.backspace, 0);
624
+ }
625
+ return matchesKittySequence(data, CODEPOINTS.backspace, modifier);
626
+ case "insert":
627
+ if (modifier === 0) {
628
+ return (matchesLegacySequence(data, LEGACY_KEY_SEQUENCES.insert) ||
629
+ matchesKittySequence(data, FUNCTIONAL_CODEPOINTS.insert, 0));
630
+ }
631
+ if (matchesLegacyModifierSequence(data, "insert", modifier)) {
632
+ return true;
633
+ }
634
+ return matchesKittySequence(data, FUNCTIONAL_CODEPOINTS.insert, modifier);
635
+ case "delete":
636
+ if (modifier === 0) {
637
+ return (matchesLegacySequence(data, LEGACY_KEY_SEQUENCES.delete) ||
638
+ matchesKittySequence(data, FUNCTIONAL_CODEPOINTS.delete, 0));
639
+ }
640
+ if (matchesLegacyModifierSequence(data, "delete", modifier)) {
641
+ return true;
642
+ }
643
+ return matchesKittySequence(data, FUNCTIONAL_CODEPOINTS.delete, modifier);
644
+ case "clear":
645
+ if (modifier === 0) {
646
+ return matchesLegacySequence(data, LEGACY_KEY_SEQUENCES.clear);
647
+ }
648
+ return matchesLegacyModifierSequence(data, "clear", modifier);
649
+ case "home":
650
+ if (modifier === 0) {
651
+ return (matchesLegacySequence(data, LEGACY_KEY_SEQUENCES.home) ||
652
+ matchesKittySequence(data, FUNCTIONAL_CODEPOINTS.home, 0));
653
+ }
654
+ if (matchesLegacyModifierSequence(data, "home", modifier)) {
655
+ return true;
656
+ }
657
+ return matchesKittySequence(data, FUNCTIONAL_CODEPOINTS.home, modifier);
658
+ case "end":
659
+ if (modifier === 0) {
660
+ return (matchesLegacySequence(data, LEGACY_KEY_SEQUENCES.end) ||
661
+ matchesKittySequence(data, FUNCTIONAL_CODEPOINTS.end, 0));
662
+ }
663
+ if (matchesLegacyModifierSequence(data, "end", modifier)) {
664
+ return true;
665
+ }
666
+ return matchesKittySequence(data, FUNCTIONAL_CODEPOINTS.end, modifier);
667
+ case "pageup":
668
+ if (modifier === 0) {
669
+ return (matchesLegacySequence(data, LEGACY_KEY_SEQUENCES.pageUp) ||
670
+ matchesKittySequence(data, FUNCTIONAL_CODEPOINTS.pageUp, 0));
671
+ }
672
+ if (matchesLegacyModifierSequence(data, "pageUp", modifier)) {
673
+ return true;
674
+ }
675
+ return matchesKittySequence(data, FUNCTIONAL_CODEPOINTS.pageUp, modifier);
676
+ case "pagedown":
677
+ if (modifier === 0) {
678
+ return (matchesLegacySequence(data, LEGACY_KEY_SEQUENCES.pageDown) ||
679
+ matchesKittySequence(data, FUNCTIONAL_CODEPOINTS.pageDown, 0));
680
+ }
681
+ if (matchesLegacyModifierSequence(data, "pageDown", modifier)) {
682
+ return true;
683
+ }
684
+ return matchesKittySequence(data, FUNCTIONAL_CODEPOINTS.pageDown, modifier);
685
+ case "up":
686
+ if (alt && !ctrl && !shift) {
687
+ return data === "\x1bp" || matchesKittySequence(data, ARROW_CODEPOINTS.up, MODIFIERS.alt);
688
+ }
689
+ if (modifier === 0) {
690
+ return (matchesLegacySequence(data, LEGACY_KEY_SEQUENCES.up) ||
691
+ matchesKittySequence(data, ARROW_CODEPOINTS.up, 0));
692
+ }
693
+ if (matchesLegacyModifierSequence(data, "up", modifier)) {
694
+ return true;
695
+ }
696
+ return matchesKittySequence(data, ARROW_CODEPOINTS.up, modifier);
697
+ case "down":
698
+ if (alt && !ctrl && !shift) {
699
+ return data === "\x1bn" || matchesKittySequence(data, ARROW_CODEPOINTS.down, MODIFIERS.alt);
700
+ }
701
+ if (modifier === 0) {
702
+ return (matchesLegacySequence(data, LEGACY_KEY_SEQUENCES.down) ||
703
+ matchesKittySequence(data, ARROW_CODEPOINTS.down, 0));
704
+ }
705
+ if (matchesLegacyModifierSequence(data, "down", modifier)) {
706
+ return true;
707
+ }
708
+ return matchesKittySequence(data, ARROW_CODEPOINTS.down, modifier);
709
+ case "left":
710
+ if (alt && !ctrl && !shift) {
711
+ return (data === "\x1b[1;3D" ||
712
+ (!_kittyProtocolActive && data === "\x1bB") ||
713
+ data === "\x1bb" ||
714
+ matchesKittySequence(data, ARROW_CODEPOINTS.left, MODIFIERS.alt));
715
+ }
716
+ if (ctrl && !alt && !shift) {
717
+ return (data === "\x1b[1;5D" ||
718
+ matchesLegacyModifierSequence(data, "left", MODIFIERS.ctrl) ||
719
+ matchesKittySequence(data, ARROW_CODEPOINTS.left, MODIFIERS.ctrl));
720
+ }
721
+ if (modifier === 0) {
722
+ return (matchesLegacySequence(data, LEGACY_KEY_SEQUENCES.left) ||
723
+ matchesKittySequence(data, ARROW_CODEPOINTS.left, 0));
724
+ }
725
+ if (matchesLegacyModifierSequence(data, "left", modifier)) {
726
+ return true;
727
+ }
728
+ return matchesKittySequence(data, ARROW_CODEPOINTS.left, modifier);
729
+ case "right":
730
+ if (alt && !ctrl && !shift) {
731
+ return (data === "\x1b[1;3C" ||
732
+ (!_kittyProtocolActive && data === "\x1bF") ||
733
+ data === "\x1bf" ||
734
+ matchesKittySequence(data, ARROW_CODEPOINTS.right, MODIFIERS.alt));
735
+ }
736
+ if (ctrl && !alt && !shift) {
737
+ return (data === "\x1b[1;5C" ||
738
+ matchesLegacyModifierSequence(data, "right", MODIFIERS.ctrl) ||
739
+ matchesKittySequence(data, ARROW_CODEPOINTS.right, MODIFIERS.ctrl));
740
+ }
741
+ if (modifier === 0) {
742
+ return (matchesLegacySequence(data, LEGACY_KEY_SEQUENCES.right) ||
743
+ matchesKittySequence(data, ARROW_CODEPOINTS.right, 0));
744
+ }
745
+ if (matchesLegacyModifierSequence(data, "right", modifier)) {
746
+ return true;
747
+ }
748
+ return matchesKittySequence(data, ARROW_CODEPOINTS.right, modifier);
749
+ case "f1":
750
+ case "f2":
751
+ case "f3":
752
+ case "f4":
753
+ case "f5":
754
+ case "f6":
755
+ case "f7":
756
+ case "f8":
757
+ case "f9":
758
+ case "f10":
759
+ case "f11":
760
+ case "f12": {
761
+ if (modifier !== 0) {
762
+ return false;
763
+ }
764
+ const functionKey = key;
765
+ return matchesLegacySequence(data, LEGACY_KEY_SEQUENCES[functionKey]);
766
+ }
767
+ }
768
+ // Handle single letter keys (a-z) and some symbols
769
+ if (key.length === 1 && ((key >= "a" && key <= "z") || SYMBOL_KEYS.has(key))) {
770
+ const codepoint = key.charCodeAt(0);
771
+ const rawCtrl = rawCtrlChar(key);
772
+ if (ctrl && alt && !shift && !_kittyProtocolActive && rawCtrl) {
773
+ // Legacy: ctrl+alt+key is ESC followed by the control character
774
+ return data === `\x1b${rawCtrl}`;
775
+ }
776
+ if (alt && !ctrl && !shift && !_kittyProtocolActive && key >= "a" && key <= "z") {
777
+ // Legacy: alt+letter is ESC followed by the letter
778
+ if (data === `\x1b${key}`)
779
+ return true;
780
+ }
781
+ if (ctrl && !shift && !alt) {
782
+ // Legacy: ctrl+key sends the control character
783
+ if (rawCtrl && data === rawCtrl)
784
+ return true;
785
+ return matchesKittySequence(data, codepoint, MODIFIERS.ctrl);
786
+ }
787
+ if (ctrl && shift && !alt) {
788
+ return matchesKittySequence(data, codepoint, MODIFIERS.shift + MODIFIERS.ctrl);
789
+ }
790
+ if (shift && !ctrl && !alt) {
791
+ // Legacy: shift+letter produces uppercase
792
+ if (data === key.toUpperCase())
793
+ return true;
794
+ return matchesKittySequence(data, codepoint, MODIFIERS.shift);
795
+ }
796
+ if (modifier !== 0) {
797
+ return matchesKittySequence(data, codepoint, modifier);
798
+ }
799
+ // Check both raw char and Kitty sequence (needed for release events)
800
+ return data === key || matchesKittySequence(data, codepoint, 0);
801
+ }
802
+ return false;
803
+ }
804
+ /**
805
+ * Parse input data and return the key identifier if recognized.
806
+ *
807
+ * @param data - Raw input data from terminal
808
+ * @returns Key identifier string (e.g., "ctrl+c") or undefined
809
+ */
810
+ export function parseKey(data) {
811
+ const kitty = parseKittySequence(data);
812
+ if (kitty) {
813
+ const { codepoint, baseLayoutKey, modifier } = kitty;
814
+ const mods = [];
815
+ const effectiveMod = modifier & ~LOCK_MASK;
816
+ if (effectiveMod & MODIFIERS.shift)
817
+ mods.push("shift");
818
+ if (effectiveMod & MODIFIERS.ctrl)
819
+ mods.push("ctrl");
820
+ if (effectiveMod & MODIFIERS.alt)
821
+ mods.push("alt");
822
+ // Use base layout key only when codepoint is not a recognized Latin
823
+ // letter (a-z) or symbol (/, -, [, ;, etc.). For those, the codepoint
824
+ // is authoritative regardless of physical key position. This prevents
825
+ // remapped layouts (Dvorak, Colemak, xremap, etc.) from reporting the
826
+ // wrong key name based on the QWERTY physical position.
827
+ const isLatinLetter = codepoint >= 97 && codepoint <= 122; // a-z
828
+ const isKnownSymbol = SYMBOL_KEYS.has(String.fromCharCode(codepoint));
829
+ const effectiveCodepoint = isLatinLetter || isKnownSymbol ? codepoint : (baseLayoutKey ?? codepoint);
830
+ let keyName;
831
+ if (effectiveCodepoint === CODEPOINTS.escape)
832
+ keyName = "escape";
833
+ else if (effectiveCodepoint === CODEPOINTS.tab)
834
+ keyName = "tab";
835
+ else if (effectiveCodepoint === CODEPOINTS.enter || effectiveCodepoint === CODEPOINTS.kpEnter)
836
+ keyName = "enter";
837
+ else if (effectiveCodepoint === CODEPOINTS.space)
838
+ keyName = "space";
839
+ else if (effectiveCodepoint === CODEPOINTS.backspace)
840
+ keyName = "backspace";
841
+ else if (effectiveCodepoint === FUNCTIONAL_CODEPOINTS.delete)
842
+ keyName = "delete";
843
+ else if (effectiveCodepoint === FUNCTIONAL_CODEPOINTS.insert)
844
+ keyName = "insert";
845
+ else if (effectiveCodepoint === FUNCTIONAL_CODEPOINTS.home)
846
+ keyName = "home";
847
+ else if (effectiveCodepoint === FUNCTIONAL_CODEPOINTS.end)
848
+ keyName = "end";
849
+ else if (effectiveCodepoint === FUNCTIONAL_CODEPOINTS.pageUp)
850
+ keyName = "pageUp";
851
+ else if (effectiveCodepoint === FUNCTIONAL_CODEPOINTS.pageDown)
852
+ keyName = "pageDown";
853
+ else if (effectiveCodepoint === ARROW_CODEPOINTS.up)
854
+ keyName = "up";
855
+ else if (effectiveCodepoint === ARROW_CODEPOINTS.down)
856
+ keyName = "down";
857
+ else if (effectiveCodepoint === ARROW_CODEPOINTS.left)
858
+ keyName = "left";
859
+ else if (effectiveCodepoint === ARROW_CODEPOINTS.right)
860
+ keyName = "right";
861
+ else if (effectiveCodepoint >= 97 && effectiveCodepoint <= 122)
862
+ keyName = String.fromCharCode(effectiveCodepoint);
863
+ else if (SYMBOL_KEYS.has(String.fromCharCode(effectiveCodepoint)))
864
+ keyName = String.fromCharCode(effectiveCodepoint);
865
+ if (keyName) {
866
+ return mods.length > 0 ? `${mods.join("+")}+${keyName}` : keyName;
867
+ }
868
+ }
869
+ // Mode-aware legacy sequences
870
+ // When Kitty protocol is active, ambiguous sequences are interpreted as custom terminal mappings:
871
+ // - \x1b\r = shift+enter (Kitty mapping), not alt+enter
872
+ // - \n = shift+enter (Ghostty mapping)
873
+ if (_kittyProtocolActive) {
874
+ if (data === "\x1b\r" || data === "\n")
875
+ return "shift+enter";
876
+ }
877
+ const legacySequenceKeyId = LEGACY_SEQUENCE_KEY_IDS[data];
878
+ if (legacySequenceKeyId)
879
+ return legacySequenceKeyId;
880
+ // Legacy sequences (used when Kitty protocol is not active, or for unambiguous sequences)
881
+ if (data === "\x1b")
882
+ return "escape";
883
+ if (data === "\x1c")
884
+ return "ctrl+\\";
885
+ if (data === "\x1d")
886
+ return "ctrl+]";
887
+ if (data === "\x1f")
888
+ return "ctrl+-";
889
+ if (data === "\x1b\x1b")
890
+ return "ctrl+alt+[";
891
+ if (data === "\x1b\x1c")
892
+ return "ctrl+alt+\\";
893
+ if (data === "\x1b\x1d")
894
+ return "ctrl+alt+]";
895
+ if (data === "\x1b\x1f")
896
+ return "ctrl+alt+-";
897
+ if (data === "\t")
898
+ return "tab";
899
+ if (data === "\r" || (!_kittyProtocolActive && data === "\n") || data === "\x1bOM")
900
+ return "enter";
901
+ if (data === "\x00")
902
+ return "ctrl+space";
903
+ if (data === " ")
904
+ return "space";
905
+ if (data === "\x7f" || data === "\x08")
906
+ return "backspace";
907
+ if (data === "\x1b[Z")
908
+ return "shift+tab";
909
+ if (!_kittyProtocolActive && data === "\x1b\r")
910
+ return "alt+enter";
911
+ if (!_kittyProtocolActive && data === "\x1b ")
912
+ return "alt+space";
913
+ if (data === "\x1b\x7f" || data === "\x1b\b")
914
+ return "alt+backspace";
915
+ if (!_kittyProtocolActive && data === "\x1bB")
916
+ return "alt+left";
917
+ if (!_kittyProtocolActive && data === "\x1bF")
918
+ return "alt+right";
919
+ if (!_kittyProtocolActive && data.length === 2 && data[0] === "\x1b") {
920
+ const code = data.charCodeAt(1);
921
+ if (code >= 1 && code <= 26) {
922
+ return `ctrl+alt+${String.fromCharCode(code + 96)}`;
923
+ }
924
+ // Legacy alt+letter (ESC followed by letter a-z)
925
+ if (code >= 97 && code <= 122) {
926
+ return `alt+${String.fromCharCode(code)}`;
927
+ }
928
+ }
929
+ if (data === "\x1b[A")
930
+ return "up";
931
+ if (data === "\x1b[B")
932
+ return "down";
933
+ if (data === "\x1b[C")
934
+ return "right";
935
+ if (data === "\x1b[D")
936
+ return "left";
937
+ if (data === "\x1b[H" || data === "\x1bOH")
938
+ return "home";
939
+ if (data === "\x1b[F" || data === "\x1bOF")
940
+ return "end";
941
+ if (data === "\x1b[3~")
942
+ return "delete";
943
+ if (data === "\x1b[5~")
944
+ return "pageUp";
945
+ if (data === "\x1b[6~")
946
+ return "pageDown";
947
+ // Raw Ctrl+letter
948
+ if (data.length === 1) {
949
+ const code = data.charCodeAt(0);
950
+ if (code >= 1 && code <= 26) {
951
+ return `ctrl+${String.fromCharCode(code + 96)}`;
952
+ }
953
+ if (code >= 32 && code <= 126) {
954
+ return data;
955
+ }
956
+ }
957
+ return undefined;
958
+ }
959
+ //# sourceMappingURL=keys.js.map