Debian Codegraph @

node/@xterm/xterm/src/common/input/Keyboard.ts

raw
1 /**
2 * Copyright (c) 2014 The xterm.js authors. All rights reserved.
3 * Copyright (c) 2012-2013, Christopher Jeffrey (MIT License)
4 * @license MIT
5 */
6
7 import { IKeyboardEvent, IKeyboardResult, KeyboardResultType } from 'common/Types';
8 import { C0 } from 'common/data/EscapeSequences';
9
10 // reg + shift key mappings for digits and special chars
11 const KEYCODE_KEY_MAPPINGS: { [key: number]: [string, string]} = {
12 // digits 0-9
13 48: ['0', ')'],
14 49: ['1', '!'],
15 50: ['2', '@'],
16 51: ['3', '#'],
17 52: ['4', '$'],
18 53: ['5', '%'],
19 54: ['6', '^'],
20 55: ['7', '&'],
21 56: ['8', '*'],
22 57: ['9', '('],
23
24 // special chars
25 186: [';', ':'],
26 187: ['=', '+'],
27 188: [',', '<'],
28 189: ['-', '_'],
29 190: ['.', '>'],
30 191: ['/', '?'],
31 192: ['`', '~'],
32 219: ['[', '{'],
33 220: ['\\', '|'],
34 221: [']', '}'],
35 222: ['\'', '"']
36 };
37
38 export function evaluateKeyboardEvent(
39 ev: IKeyboardEvent,
40 applicationCursorMode: boolean,
41 isMac: boolean,
42 macOptionIsMeta: boolean
43 ): IKeyboardResult {
44 const result: IKeyboardResult = {
45 type: KeyboardResultType.SEND_KEY,
46 // Whether to cancel event propagation (NOTE: this may not be needed since the event is
47 // canceled at the end of keyDown
48 cancel: false,
49 // The new key even to emit
50 key: undefined
51 };
52 const modifiers = (ev.shiftKey ? 1 : 0) | (ev.altKey ? 2 : 0) | (ev.ctrlKey ? 4 : 0) | (ev.metaKey ? 8 : 0);
53 switch (ev.keyCode) {
54 case 0:
55 if (ev.key === 'UIKeyInputUpArrow') {
56 if (applicationCursorMode) {
57 result.key = C0.ESC + 'OA';
58 } else {
59 result.key = C0.ESC + '[A';
60 }
61 }
62 else if (ev.key === 'UIKeyInputLeftArrow') {
63 if (applicationCursorMode) {
64 result.key = C0.ESC + 'OD';
65 } else {
66 result.key = C0.ESC + '[D';
67 }
68 }
69 else if (ev.key === 'UIKeyInputRightArrow') {
70 if (applicationCursorMode) {
71 result.key = C0.ESC + 'OC';
72 } else {
73 result.key = C0.ESC + '[C';
74 }
75 }
76 else if (ev.key === 'UIKeyInputDownArrow') {
77 if (applicationCursorMode) {
78 result.key = C0.ESC + 'OB';
79 } else {
80 result.key = C0.ESC + '[B';
81 }
82 }
83 break;
84 case 8:
85 // backspace
86 result.key = ev.ctrlKey ? '\b' : C0.DEL; // ^H or ^?
87 if (ev.altKey) {
88 result.key = C0.ESC + result.key;
89 }
90 break;
91 case 9:
92 // tab
93 if (ev.shiftKey) {
94 result.key = C0.ESC + '[Z';
95 break;
96 }
97 result.key = C0.HT;
98 result.cancel = true;
99 break;
100 case 13:
101 // return/enter
102 result.key = ev.altKey ? C0.ESC + C0.CR : C0.CR;
103 result.cancel = true;
104 break;
105 case 27:
106 // escape
107 result.key = C0.ESC;
108 if (ev.altKey) {
109 result.key = C0.ESC + C0.ESC;
110 }
111 result.cancel = true;
112 break;
113 case 37:
114 // left-arrow
115 if (ev.metaKey) {
116 break;
117 }
118 if (modifiers) {
119 result.key = C0.ESC + '[1;' + (modifiers + 1) + 'D';
120 } else if (applicationCursorMode) {
121 result.key = C0.ESC + 'OD';
122 } else {
123 result.key = C0.ESC + '[D';
124 }
125 break;
126 case 39:
127 // right-arrow
128 if (ev.metaKey) {
129 break;
130 }
131 if (modifiers) {
132 result.key = C0.ESC + '[1;' + (modifiers + 1) + 'C';
133 } else if (applicationCursorMode) {
134 result.key = C0.ESC + 'OC';
135 } else {
136 result.key = C0.ESC + '[C';
137 }
138 break;
139 case 38:
140 // up-arrow
141 if (ev.metaKey) {
142 break;
143 }
144 if (modifiers) {
145 result.key = C0.ESC + '[1;' + (modifiers + 1) + 'A';
146 } else if (applicationCursorMode) {
147 result.key = C0.ESC + 'OA';
148 } else {
149 result.key = C0.ESC + '[A';
150 }
151 break;
152 case 40:
153 // down-arrow
154 if (ev.metaKey) {
155 break;
156 }
157 if (modifiers) {
158 result.key = C0.ESC + '[1;' + (modifiers + 1) + 'B';
159 } else if (applicationCursorMode) {
160 result.key = C0.ESC + 'OB';
161 } else {
162 result.key = C0.ESC + '[B';
163 }
164 break;
165 case 45:
166 // insert
167 if (!ev.shiftKey && !ev.ctrlKey) {
168 // <Ctrl> or <Shift> + <Insert> are used to
169 // copy-paste on some systems.
170 result.key = C0.ESC + '[2~';
171 }
172 break;
173 case 46:
174 // delete
175 if (modifiers) {
176 result.key = C0.ESC + '[3;' + (modifiers + 1) + '~';
177 } else {
178 result.key = C0.ESC + '[3~';
179 }
180 break;
181 case 36:
182 // home
183 if (modifiers) {
184 result.key = C0.ESC + '[1;' + (modifiers + 1) + 'H';
185 } else if (applicationCursorMode) {
186 result.key = C0.ESC + 'OH';
187 } else {
188 result.key = C0.ESC + '[H';
189 }
190 break;
191 case 35:
192 // end
193 if (modifiers) {
194 result.key = C0.ESC + '[1;' + (modifiers + 1) + 'F';
195 } else if (applicationCursorMode) {
196 result.key = C0.ESC + 'OF';
197 } else {
198 result.key = C0.ESC + '[F';
199 }
200 break;
201 case 33:
202 // page up
203 if (ev.shiftKey) {
204 result.type = KeyboardResultType.PAGE_UP;
205 } else if (ev.ctrlKey) {
206 result.key = C0.ESC + '[5;' + (modifiers + 1) + '~';
207 } else {
208 result.key = C0.ESC + '[5~';
209 }
210 break;
211 case 34:
212 // page down
213 if (ev.shiftKey) {
214 result.type = KeyboardResultType.PAGE_DOWN;
215 } else if (ev.ctrlKey) {
216 result.key = C0.ESC + '[6;' + (modifiers + 1) + '~';
217 } else {
218 result.key = C0.ESC + '[6~';
219 }
220 break;
221 case 112:
222 // F1-F12
223 if (modifiers) {
224 result.key = C0.ESC + '[1;' + (modifiers + 1) + 'P';
225 } else {
226 result.key = C0.ESC + 'OP';
227 }
228 break;
229 case 113:
230 if (modifiers) {
231 result.key = C0.ESC + '[1;' + (modifiers + 1) + 'Q';
232 } else {
233 result.key = C0.ESC + 'OQ';
234 }
235 break;
236 case 114:
237 if (modifiers) {
238 result.key = C0.ESC + '[1;' + (modifiers + 1) + 'R';
239 } else {
240 result.key = C0.ESC + 'OR';
241 }
242 break;
243 case 115:
244 if (modifiers) {
245 result.key = C0.ESC + '[1;' + (modifiers + 1) + 'S';
246 } else {
247 result.key = C0.ESC + 'OS';
248 }
249 break;
250 case 116:
251 if (modifiers) {
252 result.key = C0.ESC + '[15;' + (modifiers + 1) + '~';
253 } else {
254 result.key = C0.ESC + '[15~';
255 }
256 break;
257 case 117:
258 if (modifiers) {
259 result.key = C0.ESC + '[17;' + (modifiers + 1) + '~';
260 } else {
261 result.key = C0.ESC + '[17~';
262 }
263 break;
264 case 118:
265 if (modifiers) {
266 result.key = C0.ESC + '[18;' + (modifiers + 1) + '~';
267 } else {
268 result.key = C0.ESC + '[18~';
269 }
270 break;
271 case 119:
272 if (modifiers) {
273 result.key = C0.ESC + '[19;' + (modifiers + 1) + '~';
274 } else {
275 result.key = C0.ESC + '[19~';
276 }
277 break;
278 case 120:
279 if (modifiers) {
280 result.key = C0.ESC + '[20;' + (modifiers + 1) + '~';
281 } else {
282 result.key = C0.ESC + '[20~';
283 }
284 break;
285 case 121:
286 if (modifiers) {
287 result.key = C0.ESC + '[21;' + (modifiers + 1) + '~';
288 } else {
289 result.key = C0.ESC + '[21~';
290 }
291 break;
292 case 122:
293 if (modifiers) {
294 result.key = C0.ESC + '[23;' + (modifiers + 1) + '~';
295 } else {
296 result.key = C0.ESC + '[23~';
297 }
298 break;
299 case 123:
300 if (modifiers) {
301 result.key = C0.ESC + '[24;' + (modifiers + 1) + '~';
302 } else {
303 result.key = C0.ESC + '[24~';
304 }
305 break;
306 default:
307 // a-z and space
308 if (ev.ctrlKey && !ev.shiftKey && !ev.altKey && !ev.metaKey) {
309 if (ev.keyCode >= 65 && ev.keyCode <= 90) {
310 result.key = String.fromCharCode(ev.keyCode - 64);
311 } else if (ev.keyCode === 32) {
312 result.key = C0.NUL;
313 } else if (ev.keyCode >= 51 && ev.keyCode <= 55) {
314 // escape, file sep, group sep, record sep, unit sep
315 result.key = String.fromCharCode(ev.keyCode - 51 + 27);
316 } else if (ev.keyCode === 56) {
317 result.key = C0.DEL;
318 } else if (ev.keyCode === 219) {
319 result.key = C0.ESC;
320 } else if (ev.keyCode === 220) {
321 result.key = C0.FS;
322 } else if (ev.keyCode === 221) {
323 result.key = C0.GS;
324 }
325 } else if ((!isMac || macOptionIsMeta) && ev.altKey && !ev.metaKey) {
326 // On macOS this is a third level shift when !macOptionIsMeta. Use <Esc> instead.
327 const keyMapping = KEYCODE_KEY_MAPPINGS[ev.keyCode];
328 const key = keyMapping?.[!ev.shiftKey ? 0 : 1];
329 if (key) {
330 result.key = C0.ESC + key;
331 } else if (ev.keyCode >= 65 && ev.keyCode <= 90) {
332 const keyCode = ev.ctrlKey ? ev.keyCode - 64 : ev.keyCode + 32;
333 let keyString = String.fromCharCode(keyCode);
334 if (ev.shiftKey) {
335 keyString = keyString.toUpperCase();
336 }
337 result.key = C0.ESC + keyString;
338 } else if (ev.keyCode === 32) {
339 result.key = C0.ESC + (ev.ctrlKey ? C0.NUL : ' ');
340 } else if (ev.key === 'Dead' && ev.code.startsWith('Key')) {
341 // Reference: https://github.com/xtermjs/xterm.js/issues/3725
342 // Alt will produce a "dead key" (initate composition) with some
343 // of the letters in US layout (e.g. N/E/U).
344 // It's safe to match against Key* since no other `code` values begin with "Key".
345 // https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/code/code_values#code_values_on_mac
346 let keyString = ev.code.slice(3, 4);
347 if (!ev.shiftKey) {
348 keyString = keyString.toLowerCase();
349 }
350 result.key = C0.ESC + keyString;
351 result.cancel = true;
352 }
353 } else if (isMac && !ev.altKey && !ev.ctrlKey && !ev.shiftKey && ev.metaKey) {
354 if (ev.keyCode === 65) { // cmd + a
355 result.type = KeyboardResultType.SELECT_ALL;
356 }
357 } else if (ev.key && !ev.ctrlKey && !ev.altKey && !ev.metaKey && ev.keyCode >= 48 && ev.key.length === 1) {
358 // Include only keys that that result in a _single_ character; don't include num lock,
359 // volume up, etc.
360 result.key = ev.key;
361 } else if (ev.key && ev.ctrlKey) {
362 if (ev.key === '_') { // ^_
363 result.key = C0.US;
364 }
365 if (ev.key === '@') { // ^ + shift + 2 = ^ + @
366 result.key = C0.NUL;
367 }
368 }
369 break;
370 }
371
372 return result;
373 }