keys.test.ts 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614
  1. /**
  2. * Tests for keyboard input handling
  3. */
  4. import assert from "node:assert";
  5. import { describe, it } from "node:test";
  6. import {
  7. decodeKittyPrintable,
  8. decodePrintableKey,
  9. Key,
  10. matchesKey,
  11. parseKey,
  12. setKittyProtocolActive,
  13. } from "../src/keys.ts";
  14. function withEnv(name: string, value: string | undefined, fn: () => void): void {
  15. const previous = process.env[name];
  16. if (value === undefined) delete process.env[name];
  17. else process.env[name] = value;
  18. try {
  19. fn();
  20. } finally {
  21. if (previous === undefined) delete process.env[name];
  22. else process.env[name] = previous;
  23. }
  24. }
  25. function withEnvVars(vars: Record<string, string | undefined>, fn: () => void): void {
  26. const entries = Object.entries(vars);
  27. const run = (index: number): void => {
  28. if (index >= entries.length) {
  29. fn();
  30. return;
  31. }
  32. const [name, value] = entries[index]!;
  33. withEnv(name, value, () => run(index + 1));
  34. };
  35. run(0);
  36. }
  37. describe("matchesKey", () => {
  38. describe("Kitty protocol with alternate keys (non-Latin layouts)", () => {
  39. // Kitty protocol flag 4 (Report alternate keys) sends:
  40. // CSI codepoint:shifted:base ; modifier:event u
  41. // Where base is the key in standard PC-101 layout
  42. it("should match Ctrl+c when pressing Ctrl+С (Cyrillic) with base layout key", () => {
  43. setKittyProtocolActive(true);
  44. // Cyrillic 'с' = codepoint 1089, Latin 'c' = codepoint 99
  45. // Format: CSI 1089::99;5u (codepoint::base;modifier with ctrl=4, +1=5)
  46. const cyrillicCtrlC = "\x1b[1089::99;5u";
  47. assert.strictEqual(matchesKey(cyrillicCtrlC, "ctrl+c"), true);
  48. setKittyProtocolActive(false);
  49. });
  50. it("should match Ctrl+d when pressing Ctrl+В (Cyrillic) with base layout key", () => {
  51. setKittyProtocolActive(true);
  52. // Cyrillic 'в' = codepoint 1074, Latin 'd' = codepoint 100
  53. const cyrillicCtrlD = "\x1b[1074::100;5u";
  54. assert.strictEqual(matchesKey(cyrillicCtrlD, "ctrl+d"), true);
  55. setKittyProtocolActive(false);
  56. });
  57. it("should match Ctrl+z when pressing Ctrl+Я (Cyrillic) with base layout key", () => {
  58. setKittyProtocolActive(true);
  59. // Cyrillic 'я' = codepoint 1103, Latin 'z' = codepoint 122
  60. const cyrillicCtrlZ = "\x1b[1103::122;5u";
  61. assert.strictEqual(matchesKey(cyrillicCtrlZ, "ctrl+z"), true);
  62. setKittyProtocolActive(false);
  63. });
  64. it("should match Ctrl+Shift+p with base layout key", () => {
  65. setKittyProtocolActive(true);
  66. // Cyrillic 'з' = codepoint 1079, Latin 'p' = codepoint 112
  67. // ctrl=4, shift=1, +1 = 6
  68. const cyrillicCtrlShiftP = "\x1b[1079::112;6u";
  69. assert.strictEqual(matchesKey(cyrillicCtrlShiftP, "ctrl+shift+p"), true);
  70. setKittyProtocolActive(false);
  71. });
  72. it("should still match direct codepoint when no base layout key", () => {
  73. setKittyProtocolActive(true);
  74. // Latin ctrl+c without base layout key (terminal doesn't support flag 4)
  75. const latinCtrlC = "\x1b[99;5u";
  76. assert.strictEqual(matchesKey(latinCtrlC, "ctrl+c"), true);
  77. setKittyProtocolActive(false);
  78. });
  79. it("should match super-modified Kitty bindings, including combined modifiers", () => {
  80. setKittyProtocolActive(true);
  81. assert.strictEqual(matchesKey("\x1b[107;9u", "super+k"), true);
  82. assert.strictEqual(matchesKey("\x1b[13;9u", "super+enter"), true);
  83. assert.strictEqual(matchesKey("\x1b[107;13u", Key.ctrlSuper("k")), true);
  84. assert.strictEqual(matchesKey("\x1b[107;13u", "ctrl+super+k"), true);
  85. assert.strictEqual(matchesKey("\x1b[107;14u", "ctrl+shift+super+k"), true);
  86. assert.strictEqual(matchesKey("\x1b[107;13u", "super+k"), false);
  87. assert.strictEqual(parseKey("\x1b[107;9u"), "super+k");
  88. assert.strictEqual(parseKey("\x1b[13;9u"), "super+enter");
  89. assert.strictEqual(parseKey("\x1b[107;13u"), "ctrl+super+k");
  90. assert.strictEqual(parseKey("\x1b[107;14u"), "shift+ctrl+super+k");
  91. setKittyProtocolActive(false);
  92. });
  93. it("should match digit bindings via Kitty CSI-u", () => {
  94. setKittyProtocolActive(true);
  95. assert.strictEqual(matchesKey("\x1b[49u", "1"), true);
  96. assert.strictEqual(matchesKey("\x1b[49;5u", "ctrl+1"), true);
  97. assert.strictEqual(matchesKey("\x1b[49;5u", "ctrl+2"), false);
  98. assert.strictEqual(parseKey("\x1b[49u"), "1");
  99. assert.strictEqual(parseKey("\x1b[49;5u"), "ctrl+1");
  100. setKittyProtocolActive(false);
  101. });
  102. it("should normalize Kitty keypad functional keys to logical digits, symbols, and navigation", () => {
  103. setKittyProtocolActive(true);
  104. assert.strictEqual(matchesKey("\x1b[57400u", "1"), true);
  105. assert.strictEqual(matchesKey("\x1b[57410u", "/"), true);
  106. assert.strictEqual(matchesKey("\x1b[57417u", "left"), true);
  107. assert.strictEqual(matchesKey("\x1b[57426u", "delete"), true);
  108. assert.strictEqual(parseKey("\x1b[57399u"), "0");
  109. assert.strictEqual(parseKey("\x1b[57409u"), ".");
  110. assert.strictEqual(parseKey("\x1b[57413u"), "+");
  111. assert.strictEqual(parseKey("\x1b[57416u"), ",");
  112. assert.strictEqual(parseKey("\x1b[57417u"), "left");
  113. assert.strictEqual(parseKey("\x1b[57418u"), "right");
  114. assert.strictEqual(parseKey("\x1b[57419u"), "up");
  115. assert.strictEqual(parseKey("\x1b[57420u"), "down");
  116. assert.strictEqual(parseKey("\x1b[57421u"), "pageUp");
  117. assert.strictEqual(parseKey("\x1b[57422u"), "pageDown");
  118. assert.strictEqual(parseKey("\x1b[57423u"), "home");
  119. assert.strictEqual(parseKey("\x1b[57424u"), "end");
  120. assert.strictEqual(parseKey("\x1b[57425u"), "insert");
  121. assert.strictEqual(parseKey("\x1b[57426u"), "delete");
  122. setKittyProtocolActive(false);
  123. });
  124. it("should handle shifted key in format", () => {
  125. setKittyProtocolActive(true);
  126. // Format with shifted key: CSI codepoint:shifted:base;modifier u
  127. // Latin 'c' with shifted 'C' (67) and base 'c' (99)
  128. const shiftedKey = "\x1b[99:67:99;2u"; // shift modifier = 1, +1 = 2
  129. assert.strictEqual(matchesKey(shiftedKey, "shift+c"), true);
  130. setKittyProtocolActive(false);
  131. });
  132. it("should handle event type in format", () => {
  133. setKittyProtocolActive(true);
  134. // Format with event type: CSI codepoint::base;modifier:event u
  135. // Cyrillic ctrl+c release event (event type 3)
  136. const releaseEvent = "\x1b[1089::99;5:3u";
  137. assert.strictEqual(matchesKey(releaseEvent, "ctrl+c"), true);
  138. setKittyProtocolActive(false);
  139. });
  140. it("should handle full format with shifted key, base key, and event type", () => {
  141. setKittyProtocolActive(true);
  142. // Full format: CSI codepoint:shifted:base;modifier:event u
  143. // Cyrillic 'С' (shifted) with base 'c', Ctrl+Shift pressed, repeat event
  144. // Cyrillic 'с' = 1089, Cyrillic 'С' = 1057, Latin 'c' = 99
  145. // ctrl=4, shift=1, +1 = 6, repeat event = 2
  146. const fullFormat = "\x1b[1089:1057:99;6:2u";
  147. assert.strictEqual(matchesKey(fullFormat, "ctrl+shift+c"), true);
  148. setKittyProtocolActive(false);
  149. });
  150. it("should prefer codepoint for Latin letters even when base layout differs", () => {
  151. setKittyProtocolActive(true);
  152. // Dvorak Ctrl+K reports codepoint 'k' (107) and base layout 'v' (118)
  153. const dvorakCtrlK = "\x1b[107::118;5u";
  154. assert.strictEqual(matchesKey(dvorakCtrlK, "ctrl+k"), true);
  155. assert.strictEqual(matchesKey(dvorakCtrlK, "ctrl+v"), false);
  156. setKittyProtocolActive(false);
  157. });
  158. it("should prefer codepoint for symbol keys even when base layout differs", () => {
  159. setKittyProtocolActive(true);
  160. // Dvorak Ctrl+/ reports codepoint '/' (47) and base layout '[' (91)
  161. const dvorakCtrlSlash = "\x1b[47::91;5u";
  162. assert.strictEqual(matchesKey(dvorakCtrlSlash, "ctrl+/"), true);
  163. assert.strictEqual(matchesKey(dvorakCtrlSlash, "ctrl+["), false);
  164. setKittyProtocolActive(false);
  165. });
  166. it("should not match wrong key even with base layout", () => {
  167. setKittyProtocolActive(true);
  168. // Cyrillic ctrl+с with base 'c' should NOT match ctrl+d
  169. const cyrillicCtrlC = "\x1b[1089::99;5u";
  170. assert.strictEqual(matchesKey(cyrillicCtrlC, "ctrl+d"), false);
  171. setKittyProtocolActive(false);
  172. });
  173. it("should not match wrong modifiers even with base layout", () => {
  174. setKittyProtocolActive(true);
  175. // Cyrillic ctrl+с should NOT match ctrl+shift+c
  176. const cyrillicCtrlC = "\x1b[1089::99;5u";
  177. assert.strictEqual(matchesKey(cyrillicCtrlC, "ctrl+shift+c"), false);
  178. setKittyProtocolActive(false);
  179. });
  180. });
  181. describe("modifyOtherKeys matching", () => {
  182. it("should match xterm modifyOtherKeys Ctrl+c", () => {
  183. setKittyProtocolActive(false);
  184. assert.strictEqual(matchesKey("\x1b[27;5;99~", "ctrl+c"), true);
  185. assert.strictEqual(parseKey("\x1b[27;5;99~"), "ctrl+c");
  186. });
  187. it("should match xterm modifyOtherKeys Ctrl+d", () => {
  188. setKittyProtocolActive(false);
  189. assert.strictEqual(matchesKey("\x1b[27;5;100~", "ctrl+d"), true);
  190. assert.strictEqual(parseKey("\x1b[27;5;100~"), "ctrl+d");
  191. });
  192. it("should match xterm modifyOtherKeys Ctrl+z", () => {
  193. setKittyProtocolActive(false);
  194. assert.strictEqual(matchesKey("\x1b[27;5;122~", "ctrl+z"), true);
  195. assert.strictEqual(parseKey("\x1b[27;5;122~"), "ctrl+z");
  196. });
  197. it("should match xterm modifyOtherKeys Enter variants", () => {
  198. setKittyProtocolActive(false);
  199. assert.strictEqual(matchesKey("\x1b[27;5;13~", "ctrl+enter"), true);
  200. assert.strictEqual(matchesKey("\x1b[27;2;13~", "shift+enter"), true);
  201. assert.strictEqual(matchesKey("\x1b[27;3;13~", "alt+enter"), true);
  202. assert.strictEqual(parseKey("\x1b[27;5;13~"), "ctrl+enter");
  203. assert.strictEqual(parseKey("\x1b[27;2;13~"), "shift+enter");
  204. assert.strictEqual(parseKey("\x1b[27;3;13~"), "alt+enter");
  205. });
  206. it("should match xterm modifyOtherKeys Tab variants", () => {
  207. setKittyProtocolActive(false);
  208. assert.strictEqual(matchesKey("\x1b[27;2;9~", "shift+tab"), true);
  209. assert.strictEqual(matchesKey("\x1b[27;5;9~", "ctrl+tab"), true);
  210. assert.strictEqual(matchesKey("\x1b[27;3;9~", "alt+tab"), true);
  211. assert.strictEqual(parseKey("\x1b[27;2;9~"), "shift+tab");
  212. assert.strictEqual(parseKey("\x1b[27;5;9~"), "ctrl+tab");
  213. assert.strictEqual(parseKey("\x1b[27;3;9~"), "alt+tab");
  214. });
  215. it("should match xterm modifyOtherKeys Backspace variants", () => {
  216. setKittyProtocolActive(false);
  217. assert.strictEqual(matchesKey("\x1b[27;1;127~", "backspace"), true);
  218. assert.strictEqual(matchesKey("\x1b[27;5;127~", "ctrl+backspace"), true);
  219. assert.strictEqual(matchesKey("\x1b[27;3;127~", "alt+backspace"), true);
  220. assert.strictEqual(parseKey("\x1b[27;1;127~"), "backspace");
  221. assert.strictEqual(parseKey("\x1b[27;5;127~"), "ctrl+backspace");
  222. assert.strictEqual(parseKey("\x1b[27;3;127~"), "alt+backspace");
  223. });
  224. it("should match xterm modifyOtherKeys Escape", () => {
  225. setKittyProtocolActive(false);
  226. assert.strictEqual(matchesKey("\x1b[27;1;27~", "escape"), true);
  227. assert.strictEqual(parseKey("\x1b[27;1;27~"), "escape");
  228. });
  229. it("should match xterm modifyOtherKeys Space variants", () => {
  230. setKittyProtocolActive(false);
  231. assert.strictEqual(matchesKey("\x1b[27;1;32~", "space"), true);
  232. assert.strictEqual(matchesKey("\x1b[27;5;32~", "ctrl+space"), true);
  233. assert.strictEqual(parseKey("\x1b[27;1;32~"), "space");
  234. assert.strictEqual(parseKey("\x1b[27;5;32~"), "ctrl+space");
  235. });
  236. it("should match xterm modifyOtherKeys symbol combos", () => {
  237. setKittyProtocolActive(false);
  238. assert.strictEqual(matchesKey("\x1b[27;5;47~", "ctrl+/"), true);
  239. assert.strictEqual(parseKey("\x1b[27;5;47~"), "ctrl+/");
  240. });
  241. it("should match xterm modifyOtherKeys digit combos", () => {
  242. setKittyProtocolActive(false);
  243. assert.strictEqual(matchesKey("\x1b[27;5;49~", "ctrl+1"), true);
  244. assert.strictEqual(matchesKey("\x1b[27;2;49~", "shift+1"), true);
  245. assert.strictEqual(parseKey("\x1b[27;5;49~"), "ctrl+1");
  246. assert.strictEqual(parseKey("\x1b[27;2;49~"), "shift+1");
  247. });
  248. it("should match xterm modifyOtherKeys shifted uppercase letters", () => {
  249. setKittyProtocolActive(false);
  250. assert.strictEqual(matchesKey("\x1b[27;2;69~", "shift+e"), true);
  251. assert.strictEqual(matchesKey("\x1b[27;6;69~", "ctrl+shift+e"), true);
  252. assert.strictEqual(parseKey("\x1b[27;2;69~"), "shift+e");
  253. assert.strictEqual(parseKey("\x1b[27;6;69~"), "shift+ctrl+e");
  254. });
  255. it("should match Ctrl+Alt+letter via CSI-u when kitty inactive", () => {
  256. setKittyProtocolActive(false);
  257. assert.strictEqual(matchesKey("\x1b[104;7u", "ctrl+alt+h"), true);
  258. assert.strictEqual(parseKey("\x1b[104;7u"), "ctrl+alt+h");
  259. });
  260. it("should match Ctrl+Alt+letter via xterm modifyOtherKeys", () => {
  261. setKittyProtocolActive(false);
  262. assert.strictEqual(matchesKey("\x1b[27;7;104~", "ctrl+alt+h"), true);
  263. assert.strictEqual(parseKey("\x1b[27;7;104~"), "ctrl+alt+h");
  264. });
  265. });
  266. describe("Legacy key matching", () => {
  267. it("should match legacy Ctrl+c", () => {
  268. setKittyProtocolActive(false);
  269. // Ctrl+c sends ASCII 3 (ETX)
  270. assert.strictEqual(matchesKey("\x03", "ctrl+c"), true);
  271. });
  272. it("should match legacy Ctrl+d", () => {
  273. setKittyProtocolActive(false);
  274. // Ctrl+d sends ASCII 4 (EOT)
  275. assert.strictEqual(matchesKey("\x04", "ctrl+d"), true);
  276. });
  277. it("should match escape key", () => {
  278. assert.strictEqual(matchesKey("\x1b", "escape"), true);
  279. });
  280. it("should match legacy linefeed as enter", () => {
  281. setKittyProtocolActive(false);
  282. assert.strictEqual(matchesKey("\n", "enter"), true);
  283. assert.strictEqual(parseKey("\n"), "enter");
  284. });
  285. it("should treat linefeed as shift+enter when kitty active", () => {
  286. setKittyProtocolActive(true);
  287. assert.strictEqual(matchesKey("\n", "shift+enter"), true);
  288. assert.strictEqual(matchesKey("\n", "enter"), false);
  289. assert.strictEqual(parseKey("\n"), "shift+enter");
  290. setKittyProtocolActive(false);
  291. });
  292. it("should parse ctrl+space", () => {
  293. setKittyProtocolActive(false);
  294. assert.strictEqual(matchesKey("\x00", "ctrl+space"), true);
  295. assert.strictEqual(parseKey("\x00"), "ctrl+space");
  296. });
  297. it("should match legacy Ctrl+symbol", () => {
  298. setKittyProtocolActive(false);
  299. // Ctrl+\ sends ASCII 28 (File Separator) in legacy terminals
  300. assert.strictEqual(matchesKey("\x1c", "ctrl+\\"), true);
  301. assert.strictEqual(parseKey("\x1c"), "ctrl+\\");
  302. // Ctrl+] sends ASCII 29 (Group Separator) in legacy terminals
  303. assert.strictEqual(matchesKey("\x1d", "ctrl+]"), true);
  304. assert.strictEqual(parseKey("\x1d"), "ctrl+]");
  305. // Ctrl+_ sends ASCII 31 (Unit Separator) in legacy terminals
  306. // Ctrl+- is on the same physical key on US keyboards
  307. assert.strictEqual(matchesKey("\x1f", "ctrl+_"), true);
  308. assert.strictEqual(matchesKey("\x1f", "ctrl+-"), true);
  309. assert.strictEqual(parseKey("\x1f"), "ctrl+-");
  310. });
  311. it("should match legacy Ctrl+Alt+symbol", () => {
  312. setKittyProtocolActive(false);
  313. // Ctrl+Alt+[ sends ESC followed by ESC (Ctrl+[ = ESC)
  314. assert.strictEqual(matchesKey("\x1b\x1b", "ctrl+alt+["), true);
  315. assert.strictEqual(parseKey("\x1b\x1b"), "ctrl+alt+[");
  316. // Ctrl+Alt+\ sends ESC followed by ASCII 28
  317. assert.strictEqual(matchesKey("\x1b\x1c", "ctrl+alt+\\"), true);
  318. assert.strictEqual(parseKey("\x1b\x1c"), "ctrl+alt+\\");
  319. // Ctrl+Alt+] sends ESC followed by ASCII 29
  320. assert.strictEqual(matchesKey("\x1b\x1d", "ctrl+alt+]"), true);
  321. assert.strictEqual(parseKey("\x1b\x1d"), "ctrl+alt+]");
  322. // Ctrl+_ sends ASCII 31 (Unit Separator) in legacy terminals
  323. // Ctrl+- is on the same physical key on US keyboards
  324. assert.strictEqual(matchesKey("\x1b\x1f", "ctrl+alt+_"), true);
  325. assert.strictEqual(matchesKey("\x1b\x1f", "ctrl+alt+-"), true);
  326. assert.strictEqual(parseKey("\x1b\x1f"), "ctrl+alt+-");
  327. });
  328. it("should treat raw 0x08 as plain backspace outside Windows Terminal", () => {
  329. setKittyProtocolActive(false);
  330. withEnv("WT_SESSION", undefined, () => {
  331. assert.strictEqual(matchesKey("\x7f", "backspace"), true);
  332. assert.strictEqual(matchesKey("\x7f", "ctrl+backspace"), false);
  333. assert.strictEqual(parseKey("\x7f"), "backspace");
  334. assert.strictEqual(matchesKey("\x08", "backspace"), true);
  335. assert.strictEqual(matchesKey("\x08", "ctrl+backspace"), false);
  336. assert.strictEqual(parseKey("\x08"), "backspace");
  337. assert.strictEqual(matchesKey("\x08", "ctrl+h"), true);
  338. });
  339. });
  340. it("should treat raw 0x08 as ctrl+backspace in local Windows Terminal", () => {
  341. setKittyProtocolActive(false);
  342. withEnvVars(
  343. {
  344. WT_SESSION: "test-session",
  345. SSH_CONNECTION: undefined,
  346. SSH_CLIENT: undefined,
  347. SSH_TTY: undefined,
  348. },
  349. () => {
  350. assert.strictEqual(matchesKey("\x08", "ctrl+backspace"), true);
  351. assert.strictEqual(matchesKey("\x08", "backspace"), false);
  352. assert.strictEqual(parseKey("\x08"), "ctrl+backspace");
  353. assert.strictEqual(matchesKey("\x08", "ctrl+h"), true);
  354. },
  355. );
  356. });
  357. it("should treat raw 0x08 as plain backspace in Windows Terminal over SSH", () => {
  358. setKittyProtocolActive(false);
  359. withEnvVars(
  360. {
  361. WT_SESSION: "test-session",
  362. SSH_CONNECTION: "1 2 3 4",
  363. SSH_CLIENT: "1 2 3",
  364. SSH_TTY: "/dev/pts/1",
  365. },
  366. () => {
  367. assert.strictEqual(matchesKey("\x08", "ctrl+backspace"), false);
  368. assert.strictEqual(matchesKey("\x08", "backspace"), true);
  369. assert.strictEqual(parseKey("\x08"), "backspace");
  370. assert.strictEqual(matchesKey("\x08", "ctrl+h"), true);
  371. },
  372. );
  373. });
  374. it("should parse legacy alt-prefixed sequences when kitty inactive", () => {
  375. setKittyProtocolActive(false);
  376. assert.strictEqual(matchesKey("\x1b ", "alt+space"), true);
  377. assert.strictEqual(parseKey("\x1b "), "alt+space");
  378. assert.strictEqual(matchesKey("\x1b\b", "alt+backspace"), true);
  379. assert.strictEqual(parseKey("\x1b\b"), "alt+backspace");
  380. assert.strictEqual(matchesKey("\x1b\x03", "ctrl+alt+c"), true);
  381. assert.strictEqual(parseKey("\x1b\x03"), "ctrl+alt+c");
  382. assert.strictEqual(matchesKey("\x1bB", "alt+left"), true);
  383. assert.strictEqual(parseKey("\x1bB"), "alt+left");
  384. assert.strictEqual(matchesKey("\x1bF", "alt+right"), true);
  385. assert.strictEqual(parseKey("\x1bF"), "alt+right");
  386. assert.strictEqual(matchesKey("\x1ba", "alt+a"), true);
  387. assert.strictEqual(parseKey("\x1ba"), "alt+a");
  388. assert.strictEqual(matchesKey("\x1b1", "alt+1"), true);
  389. assert.strictEqual(parseKey("\x1b1"), "alt+1");
  390. assert.strictEqual(matchesKey("\x1by", "alt+y"), true);
  391. assert.strictEqual(parseKey("\x1by"), "alt+y");
  392. assert.strictEqual(matchesKey("\x1bz", "alt+z"), true);
  393. assert.strictEqual(parseKey("\x1bz"), "alt+z");
  394. setKittyProtocolActive(true);
  395. assert.strictEqual(matchesKey("\x1b ", "alt+space"), false);
  396. assert.strictEqual(parseKey("\x1b "), undefined);
  397. assert.strictEqual(matchesKey("\x1b\b", "alt+backspace"), true);
  398. assert.strictEqual(parseKey("\x1b\b"), "alt+backspace");
  399. assert.strictEqual(matchesKey("\x1b\x03", "ctrl+alt+c"), false);
  400. assert.strictEqual(parseKey("\x1b\x03"), undefined);
  401. assert.strictEqual(matchesKey("\x1bB", "alt+left"), false);
  402. assert.strictEqual(parseKey("\x1bB"), undefined);
  403. assert.strictEqual(matchesKey("\x1bF", "alt+right"), false);
  404. assert.strictEqual(parseKey("\x1bF"), undefined);
  405. assert.strictEqual(matchesKey("\x1ba", "alt+a"), false);
  406. assert.strictEqual(parseKey("\x1ba"), undefined);
  407. assert.strictEqual(matchesKey("\x1b1", "alt+1"), false);
  408. assert.strictEqual(parseKey("\x1b1"), undefined);
  409. assert.strictEqual(matchesKey("\x1by", "alt+y"), false);
  410. assert.strictEqual(parseKey("\x1by"), undefined);
  411. setKittyProtocolActive(false);
  412. });
  413. it("should match arrow keys", () => {
  414. assert.strictEqual(matchesKey("\x1b[A", "up"), true);
  415. assert.strictEqual(matchesKey("\x1b[B", "down"), true);
  416. assert.strictEqual(matchesKey("\x1b[C", "right"), true);
  417. assert.strictEqual(matchesKey("\x1b[D", "left"), true);
  418. });
  419. it("should match SS3 arrows and home/end", () => {
  420. assert.strictEqual(matchesKey("\x1bOA", "up"), true);
  421. assert.strictEqual(matchesKey("\x1bOB", "down"), true);
  422. assert.strictEqual(matchesKey("\x1bOC", "right"), true);
  423. assert.strictEqual(matchesKey("\x1bOD", "left"), true);
  424. assert.strictEqual(matchesKey("\x1bOH", "home"), true);
  425. assert.strictEqual(matchesKey("\x1bOF", "end"), true);
  426. });
  427. it("should match legacy function keys and clear", () => {
  428. assert.strictEqual(matchesKey("\x1bOP", "f1"), true);
  429. assert.strictEqual(matchesKey("\x1b[24~", "f12"), true);
  430. assert.strictEqual(matchesKey("\x1b[E", "clear"), true);
  431. });
  432. it("should match alt+arrows", () => {
  433. assert.strictEqual(matchesKey("\x1bp", "alt+up"), true);
  434. assert.strictEqual(matchesKey("\x1bp", "up"), false);
  435. });
  436. it("should match rxvt modifier sequences", () => {
  437. assert.strictEqual(matchesKey("\x1b[a", "shift+up"), true);
  438. assert.strictEqual(matchesKey("\x1bOa", "ctrl+up"), true);
  439. assert.strictEqual(matchesKey("\x1b[2$", "shift+insert"), true);
  440. assert.strictEqual(matchesKey("\x1b[2^", "ctrl+insert"), true);
  441. assert.strictEqual(matchesKey("\x1b[7$", "shift+home"), true);
  442. });
  443. });
  444. });
  445. describe("decodeKittyPrintable", () => {
  446. it("should decode Kitty keypad functional keys to printable characters", () => {
  447. assert.strictEqual(decodeKittyPrintable("\x1b[57399u"), "0");
  448. assert.strictEqual(decodeKittyPrintable("\x1b[57400u"), "1");
  449. assert.strictEqual(decodeKittyPrintable("\x1b[57409u"), ".");
  450. assert.strictEqual(decodeKittyPrintable("\x1b[57410u"), "/");
  451. assert.strictEqual(decodeKittyPrintable("\x1b[57411u"), "*");
  452. assert.strictEqual(decodeKittyPrintable("\x1b[57412u"), "-");
  453. assert.strictEqual(decodeKittyPrintable("\x1b[57413u"), "+");
  454. assert.strictEqual(decodeKittyPrintable("\x1b[57415u"), "=");
  455. assert.strictEqual(decodeKittyPrintable("\x1b[57416u"), ",");
  456. assert.strictEqual(decodeKittyPrintable("\x1b[57417u"), undefined);
  457. });
  458. });
  459. describe("decodePrintableKey", () => {
  460. it("should decode printable xterm modifyOtherKeys sequences", () => {
  461. assert.strictEqual(decodePrintableKey("\x1b[27;2;69~"), "E");
  462. assert.strictEqual(decodePrintableKey("\x1b[27;2;196~"), "Ä");
  463. assert.strictEqual(decodePrintableKey("\x1b[27;2;32~"), " ");
  464. assert.strictEqual(decodePrintableKey("\x1b[27;2;13~"), undefined);
  465. assert.strictEqual(decodePrintableKey("\x1b[27;6;69~"), undefined);
  466. });
  467. });
  468. describe("parseKey", () => {
  469. describe("Kitty protocol with alternate keys", () => {
  470. it("should return Latin key name when base layout key is present", () => {
  471. setKittyProtocolActive(true);
  472. // Cyrillic ctrl+с with base layout 'c'
  473. const cyrillicCtrlC = "\x1b[1089::99;5u";
  474. assert.strictEqual(parseKey(cyrillicCtrlC), "ctrl+c");
  475. setKittyProtocolActive(false);
  476. });
  477. it("should prefer codepoint for Latin letters when base layout differs", () => {
  478. setKittyProtocolActive(true);
  479. // Dvorak Ctrl+K reports codepoint 'k' (107) and base layout 'v' (118)
  480. const dvorakCtrlK = "\x1b[107::118;5u";
  481. assert.strictEqual(parseKey(dvorakCtrlK), "ctrl+k");
  482. setKittyProtocolActive(false);
  483. });
  484. it("should prefer codepoint for symbol keys when base layout differs", () => {
  485. setKittyProtocolActive(true);
  486. // Dvorak Ctrl+/ reports codepoint '/' (47) and base layout '[' (91)
  487. const dvorakCtrlSlash = "\x1b[47::91;5u";
  488. assert.strictEqual(parseKey(dvorakCtrlSlash), "ctrl+/");
  489. setKittyProtocolActive(false);
  490. });
  491. it("should return key name from codepoint when no base layout", () => {
  492. setKittyProtocolActive(true);
  493. const latinCtrlC = "\x1b[99;5u";
  494. assert.strictEqual(parseKey(latinCtrlC), "ctrl+c");
  495. setKittyProtocolActive(false);
  496. });
  497. it("should parse shifted uppercase CSI-u letters as shift+letter", () => {
  498. setKittyProtocolActive(true);
  499. assert.strictEqual(matchesKey("\x1b[69;2u", "shift+e"), true);
  500. assert.strictEqual(parseKey("\x1b[69;2u"), "shift+e");
  501. setKittyProtocolActive(false);
  502. });
  503. it("should ignore Kitty CSI-u with unsupported modifiers", () => {
  504. setKittyProtocolActive(true);
  505. assert.strictEqual(parseKey("\x1b[99;17u"), undefined);
  506. setKittyProtocolActive(false);
  507. });
  508. });
  509. describe("Legacy key parsing", () => {
  510. it("should parse legacy Ctrl+letter", () => {
  511. setKittyProtocolActive(false);
  512. assert.strictEqual(parseKey("\x03"), "ctrl+c");
  513. assert.strictEqual(parseKey("\x04"), "ctrl+d");
  514. });
  515. it("should parse special keys", () => {
  516. assert.strictEqual(parseKey("\x1b"), "escape");
  517. assert.strictEqual(parseKey("\t"), "tab");
  518. assert.strictEqual(parseKey("\r"), "enter");
  519. assert.strictEqual(parseKey("\n"), "enter");
  520. assert.strictEqual(parseKey("\x00"), "ctrl+space");
  521. assert.strictEqual(parseKey(" "), "space");
  522. assert.strictEqual(parseKey("1"), "1");
  523. assert.strictEqual(matchesKey("1", "1"), true);
  524. });
  525. it("should parse arrow keys", () => {
  526. assert.strictEqual(parseKey("\x1b[A"), "up");
  527. assert.strictEqual(parseKey("\x1b[B"), "down");
  528. assert.strictEqual(parseKey("\x1b[C"), "right");
  529. assert.strictEqual(parseKey("\x1b[D"), "left");
  530. });
  531. it("should parse SS3 arrows and home/end", () => {
  532. assert.strictEqual(parseKey("\x1bOA"), "up");
  533. assert.strictEqual(parseKey("\x1bOB"), "down");
  534. assert.strictEqual(parseKey("\x1bOC"), "right");
  535. assert.strictEqual(parseKey("\x1bOD"), "left");
  536. assert.strictEqual(parseKey("\x1bOH"), "home");
  537. assert.strictEqual(parseKey("\x1bOF"), "end");
  538. });
  539. it("should parse legacy function and modifier sequences", () => {
  540. assert.strictEqual(parseKey("\x1bOP"), "f1");
  541. assert.strictEqual(parseKey("\x1b[24~"), "f12");
  542. assert.strictEqual(parseKey("\x1b[E"), "clear");
  543. assert.strictEqual(parseKey("\x1b[2^"), "ctrl+insert");
  544. assert.strictEqual(parseKey("\x1bp"), "alt+up");
  545. });
  546. it("should parse double bracket pageUp", () => {
  547. assert.strictEqual(parseKey("\x1b[[5~"), "pageUp");
  548. });
  549. });
  550. });