blob: 70b9c1e31e1c0f80f886db530a1fc11dfeda2700 [file] [log] [blame]
rgindafeaf3142012-01-31 15:14:20 -08001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
rgindacbbd7482012-06-13 15:06:16 -07005'use strict';
6
7lib.rtdep('hterm.Keyboard.KeyMap');
8
rgindafeaf3142012-01-31 15:14:20 -08009/**
10 * Keyboard handler.
11 *
12 * Consumes onKey* events and invokes onVTKeystroke on the associated
13 * hterm.Terminal object.
14 *
15 * See also: [XTERM] as referenced in vt.js.
16 *
17 * @param {hterm.Terminal} The Terminal object associated with this keyboard.
18 */
19hterm.Keyboard = function(terminal) {
20 // The parent vt interpreter.
21 this.terminal = terminal;
22
23 // The element we're currently capturing keyboard events for.
24 this.keyboardElement_ = null;
25
26 // The event handlers we are interested in, and their bound callbacks, saved
27 // so they can be uninstalled with removeEventListener, when required.
28 this.handlers_ = [
Mike Frysingerc5271612017-04-12 02:24:30 -040029 ['focusout', this.onFocusOut_.bind(this)],
Robert Ginda11390c52012-09-13 14:53:34 -070030 ['keydown', this.onKeyDown_.bind(this)],
Andrew de los Reyes574e10e2013-04-04 09:31:57 -070031 ['keypress', this.onKeyPress_.bind(this)],
32 ['keyup', this.onKeyUp_.bind(this)],
Robert Ginda11390c52012-09-13 14:53:34 -070033 ['textInput', this.onTextInput_.bind(this)]
rgindafeaf3142012-01-31 15:14:20 -080034 ];
35
36 /**
37 * The current key map.
38 */
rgindacbbd7482012-06-13 15:06:16 -070039 this.keyMap = new hterm.Keyboard.KeyMap(this);
rgindafeaf3142012-01-31 15:14:20 -080040
Robert Gindaf82267d2015-06-09 15:32:04 -070041 this.bindings = new hterm.Keyboard.Bindings(this);
42
rgindafeaf3142012-01-31 15:14:20 -080043 /**
Robert Ginda034ffa72015-02-26 14:02:37 -080044 * none: Disable any AltGr related munging.
45 * ctrl-alt: Assume Ctrl+Alt means AltGr.
46 * left-alt: Assume left Alt means AltGr.
47 * right-alt: Assume right Alt means AltGr.
48 */
49 this.altGrMode = 'none';
50
51 /**
rginda4bba5e12012-06-20 16:15:30 -070052 * If true, Shift-Insert will fall through to the browser as a paste.
53 * If false, the keystroke will be sent to the host.
54 */
Robert Ginda57f03b42012-09-13 11:02:48 -070055 this.shiftInsertPaste = true;
rginda4bba5e12012-06-20 16:15:30 -070056
57 /**
rgindafeaf3142012-01-31 15:14:20 -080058 * If true, home/end will control the terminal scrollbar and shift home/end
59 * will send the VT keycodes. If false then home/end sends VT codes and
60 * shift home/end scrolls.
61 */
Robert Ginda57f03b42012-09-13 11:02:48 -070062 this.homeKeysScroll = false;
rgindafeaf3142012-01-31 15:14:20 -080063
64 /**
65 * Same as above, except for page up/page down.
66 */
Robert Ginda57f03b42012-09-13 11:02:48 -070067 this.pageKeysScroll = false;
rgindafeaf3142012-01-31 15:14:20 -080068
69 /**
Robert Ginda7e5e9522014-03-14 12:23:58 -070070 * If true, Ctrl-Plus/Minus/Zero controls zoom.
71 * If false, Ctrl-Shift-Plus/Minus/Zero controls zoom, Ctrl-Minus sends ^_,
72 * Ctrl-Plus/Zero do nothing.
73 */
74 this.ctrlPlusMinusZeroZoom = true;
75
76 /**
Robert Gindafb5a3f92014-05-13 14:12:00 -070077 * Ctrl+C copies if true, sends ^C to host if false.
78 * Ctrl+Shift+C sends ^C to host if true, copies if false.
79 */
80 this.ctrlCCopy = false;
81
82 /**
83 * Ctrl+V pastes if true, sends ^V to host if false.
84 * Ctrl+Shift+V sends ^V to host if true, pastes if false.
Leonardo Mesquita61e7c312014-01-04 12:53:12 +010085 */
86 this.ctrlVPaste = false;
87
88 /**
rgindafeaf3142012-01-31 15:14:20 -080089 * Enable/disable application keypad.
90 *
91 * This changes the way numeric keys are sent from the keyboard.
92 */
93 this.applicationKeypad = false;
94
95 /**
96 * Enable/disable the application cursor mode.
97 *
98 * This changes the way cursor keys are sent from the keyboard.
99 */
100 this.applicationCursor = false;
101
102 /**
103 * If true, the backspace should send BS ('\x08', aka ^H). Otherwise
104 * the backspace key should send '\x7f'.
105 */
Robert Ginda57f03b42012-09-13 11:02:48 -0700106 this.backspaceSendsBackspace = false;
rgindafeaf3142012-01-31 15:14:20 -0800107
108 /**
109 * Set whether the meta key sends a leading escape or not.
110 */
Robert Ginda57f03b42012-09-13 11:02:48 -0700111 this.metaSendsEscape = true;
rginda30f20f62012-04-05 16:36:19 -0700112
113 /**
Marius Schilder77857b32014-05-14 16:21:26 -0700114 * Set whether meta-V gets passed to host.
115 */
116 this.passMetaV = true;
117
118 /**
rginda39bdf6f2012-04-10 16:50:55 -0700119 * Controls how the alt key is handled.
120 *
121 * escape....... Send an ESC prefix.
122 * 8-bit........ Add 128 to the unshifted character as in xterm.
123 * browser-key.. Wait for the keypress event and see what the browser says.
124 * (This won't work well on platforms where the browser
125 * performs a default action for some alt sequences.)
rginda30f20f62012-04-05 16:36:19 -0700126 *
127 * This setting only matters when alt is distinct from meta (altIsMeta is
128 * false.)
129 */
Robert Ginda57f03b42012-09-13 11:02:48 -0700130 this.altSendsWhat = 'escape';
rginda42ad71d2012-02-16 14:06:28 -0800131
132 /**
133 * Set whether the alt key acts as a meta key, instead of producing 8-bit
134 * characters.
rginda30f20f62012-04-05 16:36:19 -0700135 *
136 * True to enable, false to disable, null to autodetect based on platform.
rginda42ad71d2012-02-16 14:06:28 -0800137 */
Robert Ginda57f03b42012-09-13 11:02:48 -0700138 this.altIsMeta = false;
Andrew de los Reyes574e10e2013-04-04 09:31:57 -0700139
140 /**
141 * If true, tries to detect DEL key events that are from alt-backspace on
142 * Chrome OS vs from a true DEL key press.
143 *
144 * Background: At the time of writing, on Chrome OS, alt-backspace is mapped
145 * to DEL. Some users may be happy with this, but others may be frustrated
146 * that it's impossible to do meta-backspace. If the user enables this pref,
147 * we use a trick to tell a true DEL keypress from alt-backspace: on
148 * alt-backspace, we will see the alt key go down, then get a DEL keystroke
Mike Frysingere4533182017-04-19 00:18:29 -0400149 * that indicates that alt is not pressed. See https://crbug.com/174410 .
Andrew de los Reyes574e10e2013-04-04 09:31:57 -0700150 */
151 this.altBackspaceIsMetaBackspace = false;
152
153 /**
154 * Used to keep track of the current alt-key state, which is necessary for
Robert Ginda034ffa72015-02-26 14:02:37 -0800155 * the altBackspaceIsMetaBackspace preference above and for the altGrMode
156 * preference. This is a bitmap with where bit positions correspond to the
157 * "location" property of the key event.
Andrew de los Reyes574e10e2013-04-04 09:31:57 -0700158 */
Robert Ginda034ffa72015-02-26 14:02:37 -0800159 this.altKeyPressed = 0;
Andrew de los Reyes6af23ae2013-04-04 14:17:50 -0700160
161 /**
162 * If true, Chrome OS media keys will be mapped to their F-key equivalent.
163 * E.g. "Back" will be mapped to F1. If false, Chrome will handle the keys.
164 */
165 this.mediaKeysAreFKeys = false;
Brad Town5f375a42015-03-12 01:30:58 -0700166
167 /**
168 * Holds the previous setting of altSendsWhat when DECSET 1039 is used. When
169 * DECRST 1039 is used, altSendsWhat is changed back to this and this is
170 * nulled out.
171 */
172 this.previousAltSendsWhat_ = null;
rgindafeaf3142012-01-31 15:14:20 -0800173};
174
175/**
rgindafeaf3142012-01-31 15:14:20 -0800176 * Special handling for keyCodes in a keyboard layout.
177 */
178hterm.Keyboard.KeyActions = {
179 /**
180 * Call preventDefault and stopPropagation for this key event and nothing
181 * else.
182 */
Mike Frysingerc0d77ea2017-05-17 22:33:30 -0400183 CANCEL: lib.f.createEnum('CANCEL'),
rgindafeaf3142012-01-31 15:14:20 -0800184
185 /**
186 * This performs the default terminal action for the key. If used in the
187 * 'normal' action and the the keystroke represents a printable key, the
188 * character will be sent to the host. If used in one of the modifier
189 * actions, the terminal will perform the normal action after (possibly)
190 * altering it.
191 *
192 * - If the normal sequence starts with CSI, the sequence will be adjusted
193 * to include the modifier parameter as described in [XTERM] in the final
194 * table of the "PC-Style Function Keys" section.
195 *
196 * - If the control key is down and the key represents a printable character,
197 * and the uppercase version of the unshifted keycap is between
198 * 64 (ASCII '@') and 95 (ASCII '_'), then the uppercase version of the
199 * unshifted keycap minus 64 is sent. This makes '^@' send '\x00' and
200 * '^_' send '\x1f'. (Note that one higher that 0x1f is 0x20, which is
201 * the first printable ASCII value.)
202 *
203 * - If the alt key is down and the key represents a printable character then
204 * the value of the character is shifted up by 128.
205 *
206 * - If meta is down and configured to send an escape, '\x1b' will be sent
207 * before the normal action is performed.
208 */
Mike Frysingerc0d77ea2017-05-17 22:33:30 -0400209 DEFAULT: lib.f.createEnum('DEFAULT'),
rgindafeaf3142012-01-31 15:14:20 -0800210
211 /**
212 * Causes the terminal to opt out of handling the key event, instead letting
213 * the browser deal with it.
214 */
Mike Frysingerc0d77ea2017-05-17 22:33:30 -0400215 PASS: lib.f.createEnum('PASS'),
rgindafeaf3142012-01-31 15:14:20 -0800216
217 /**
218 * Insert the first or second character of the keyCap, based on e.shiftKey.
219 * The key will be handled in onKeyDown, and e.preventDefault() will be
220 * called.
221 *
222 * It is useful for a modified key action, where it essentially strips the
223 * modifier while preventing the browser from reacting to the key.
224 */
Mike Frysingerc0d77ea2017-05-17 22:33:30 -0400225 STRIP: lib.f.createEnum('STRIP')
rgindafeaf3142012-01-31 15:14:20 -0800226};
227
228/**
229 * Capture keyboard events sent to the associated element.
230 *
231 * This enables the keyboard. Captured events are consumed by this class
232 * and will not perform their default action or bubble to other elements.
233 *
234 * Passing a null element will uninstall the keyboard handlers.
235 *
236 * @param {HTMLElement} element The element whose events should be captured, or
237 * null to disable the keyboard.
238 */
239hterm.Keyboard.prototype.installKeyboard = function(element) {
240 if (element == this.keyboardElement_)
241 return;
242
243 if (element && this.keyboardElement_)
244 this.installKeyboard(null);
245
246 for (var i = 0; i < this.handlers_.length; i++) {
247 var handler = this.handlers_[i];
248 if (element) {
249 element.addEventListener(handler[0], handler[1]);
250 } else {
251 this.keyboardElement_.removeEventListener(handler[0], handler[1]);
252 }
253 }
254
255 this.keyboardElement_ = element;
256};
257
258/**
259 * Disable keyboard event capture.
260 *
261 * This will allow the browser to process key events normally.
262 */
263hterm.Keyboard.prototype.uninstallKeyboard = function() {
264 this.installKeyboard(null);
265};
266
267/**
Robert Ginda11390c52012-09-13 14:53:34 -0700268 * Handle onTextInput events.
269 *
Mike Frysingere5a61b02017-09-13 21:58:29 -0400270 * These are generated when using IMEs, Virtual Keyboards (VKs), compose keys,
271 * Unicode input, etc...
Robert Ginda11390c52012-09-13 14:53:34 -0700272 */
273hterm.Keyboard.prototype.onTextInput_ = function(e) {
274 if (!e.data)
275 return;
276
Mike Frysingere5a61b02017-09-13 21:58:29 -0400277 // Just pass the generated buffer straight down. No need for us to split it
278 // up or otherwise parse it ahead of times.
279 this.terminal.onVTKeystroke(e.data);
Robert Ginda11390c52012-09-13 14:53:34 -0700280};
281
282/**
rgindafeaf3142012-01-31 15:14:20 -0800283 * Handle onKeyPress events.
Mike Frysingerd30bd512019-01-04 02:00:22 -0500284 *
285 * TODO(vapier): Drop this event entirely and only use keydown.
rgindafeaf3142012-01-31 15:14:20 -0800286 */
287hterm.Keyboard.prototype.onKeyPress_ = function(e) {
Mike Frysingerd30bd512019-01-04 02:00:22 -0500288 // FF doesn't set keyCode reliably in keypress events. Stick to the which
289 // field here until we can move to keydown entirely.
290 const key = String.fromCharCode(e.which).toLowerCase();
291 if ((e.ctrlKey || e.metaKey) && (key == 'c' || key == 'v')) {
Rob Spies0bec09b2014-06-06 15:58:09 -0700292 // On FF the key press (not key down) event gets fired for copy/paste.
Zhu Qunying30d40712017-03-14 16:27:00 -0700293 // Let it fall through for the default browser behavior.
Rob Spies0bec09b2014-06-06 15:58:09 -0700294 return;
295 }
296
Adrián Pérez-Orozcod76f2f02018-08-13 10:18:12 -0700297 if (e.keyCode == 9 /* Tab */) {
298 // On FF, a key press event will be fired in addition of key down for the
299 // Tab key if key down isn't handled. This would only happen if a custom
300 // PASS binding has been created and therefore this should be handled by the
301 // browser.
302 return;
303 }
304
rginda39bdf6f2012-04-10 16:50:55 -0700305 if (e.altKey && this.altSendsWhat == 'browser-key' && e.charCode == 0) {
306 // If we got here because we were expecting the browser to handle an
307 // alt sequence but it didn't do it, then we might be on an OS without
308 // an enabled IME system. In that case we fall back to xterm-like
309 // behavior.
310 //
311 // This happens here only as a fallback. Typically these platforms should
312 // set altSendsWhat to either 'escape' or '8-bit'.
313 var ch = String.fromCharCode(e.keyCode);
314 if (!e.shiftKey)
315 ch = ch.toLowerCase();
rginda39bdf6f2012-04-10 16:50:55 -0700316
317 } else if (e.charCode >= 32) {
318 ch = e.charCode;
319 }
320
Robert Ginda8cb7d902013-06-20 14:37:18 -0700321 if (ch)
322 this.terminal.onVTKeystroke(String.fromCharCode(ch));
rgindafeaf3142012-01-31 15:14:20 -0800323
324 e.preventDefault();
325 e.stopPropagation();
326};
327
Marius Schilder45703172014-12-22 17:10:01 -0800328/**
Marius Schilder9dc111b2015-08-13 14:09:39 -0700329 * Prevent default handling for non-ctrl-shifted event.
Marius Schilder45703172014-12-22 17:10:01 -0800330 *
331 * When combined with Chrome permission 'app.window.fullscreen.overrideEsc',
332 * and called for both key down and key up events,
333 * the ESC key remains usable within fullscreen Chrome app windows.
334 */
Marius Schilder9dc111b2015-08-13 14:09:39 -0700335hterm.Keyboard.prototype.preventChromeAppNonCtrlShiftDefault_ = function(e) {
Marius Schilder45703172014-12-22 17:10:01 -0800336 if (!window.chrome || !window.chrome.app || !window.chrome.app.window)
337 return;
Marius Schilder9dc111b2015-08-13 14:09:39 -0700338 if (!e.ctrlKey || !e.shiftKey)
Marius Schilder45703172014-12-22 17:10:01 -0800339 e.preventDefault();
340};
341
Mike Frysingerc5271612017-04-12 02:24:30 -0400342hterm.Keyboard.prototype.onFocusOut_ = function(e) {
Robert Ginda034ffa72015-02-26 14:02:37 -0800343 this.altKeyPressed = 0;
Andrew de los Reyes574e10e2013-04-04 09:31:57 -0700344};
345
346hterm.Keyboard.prototype.onKeyUp_ = function(e) {
347 if (e.keyCode == 18)
Robert Ginda034ffa72015-02-26 14:02:37 -0800348 this.altKeyPressed = this.altKeyPressed & ~(1 << (e.location - 1));
349
Marius Schilder45703172014-12-22 17:10:01 -0800350 if (e.keyCode == 27)
Marius Schilder9dc111b2015-08-13 14:09:39 -0700351 this.preventChromeAppNonCtrlShiftDefault_(e);
Andrew de los Reyes574e10e2013-04-04 09:31:57 -0700352};
353
rgindafeaf3142012-01-31 15:14:20 -0800354/**
355 * Handle onKeyDown events.
356 */
357hterm.Keyboard.prototype.onKeyDown_ = function(e) {
Andrew de los Reyes574e10e2013-04-04 09:31:57 -0700358 if (e.keyCode == 18)
Robert Ginda034ffa72015-02-26 14:02:37 -0800359 this.altKeyPressed = this.altKeyPressed | (1 << (e.location - 1));
360
Marius Schilder45703172014-12-22 17:10:01 -0800361 if (e.keyCode == 27)
Marius Schilder9dc111b2015-08-13 14:09:39 -0700362 this.preventChromeAppNonCtrlShiftDefault_(e);
Andrew de los Reyes574e10e2013-04-04 09:31:57 -0700363
rgindafeaf3142012-01-31 15:14:20 -0800364 var keyDef = this.keyMap.keyDefs[e.keyCode];
365 if (!keyDef) {
Mike Frysinger53e29992017-12-12 09:36:27 -0500366 // If this key hasn't been explicitly registered, fall back to the unknown
367 // key mapping (keyCode == 0), and then automatically register it to avoid
368 // any further warnings here.
369 console.warn(`No definition for key ${e.key} (keyCode ${e.keyCode})`);
370 keyDef = this.keyMap.keyDefs[0];
371 this.keyMap.addKeyDef(e.keyCode, keyDef);
rgindafeaf3142012-01-31 15:14:20 -0800372 }
373
Robert Ginda21de3762012-09-20 16:38:18 -0700374 // The type of action we're going to use.
375 var resolvedActionType = null;
376
rgindafeaf3142012-01-31 15:14:20 -0800377 var self = this;
378 function getAction(name) {
379 // Get the key action for the given action name. If the action is a
380 // function, dispatch it. If the action defers to the normal action,
381 // resolve that instead.
382
Robert Ginda21de3762012-09-20 16:38:18 -0700383 resolvedActionType = name;
384
rgindafeaf3142012-01-31 15:14:20 -0800385 var action = keyDef[name];
386 if (typeof action == 'function')
387 action = action.apply(self.keyMap, [e, keyDef]);
388
389 if (action === DEFAULT && name != 'normal')
390 action = getAction('normal');
391
392 return action;
393 }
394
395 // Note that we use the triple-equals ('===') operator to test equality for
Zhu Qunying30d40712017-03-14 16:27:00 -0700396 // these constants, in order to distinguish usage of the constant from usage
rgindafeaf3142012-01-31 15:14:20 -0800397 // of a literal string that happens to contain the same bytes.
398 var CANCEL = hterm.Keyboard.KeyActions.CANCEL;
399 var DEFAULT = hterm.Keyboard.KeyActions.DEFAULT;
400 var PASS = hterm.Keyboard.KeyActions.PASS;
401 var STRIP = hterm.Keyboard.KeyActions.STRIP;
402
rgindafeaf3142012-01-31 15:14:20 -0800403 var control = e.ctrlKey;
rginda42ad71d2012-02-16 14:06:28 -0800404 var alt = this.altIsMeta ? false : e.altKey;
405 var meta = this.altIsMeta ? (e.altKey || e.metaKey) : e.metaKey;
rgindafeaf3142012-01-31 15:14:20 -0800406
Robert Ginda4c66c9a2015-02-18 13:16:21 -0800407 // In the key-map, we surround the keyCap for non-printables in "[...]"
408 var isPrintable = !(/^\[\w+\]$/.test(keyDef.keyCap));
409
Robert Ginda034ffa72015-02-26 14:02:37 -0800410 switch (this.altGrMode) {
411 case 'ctrl-alt':
412 if (isPrintable && control && alt) {
413 // ctrl-alt-printable means altGr. We clear out the control and
414 // alt modifiers and wait to see the charCode in the keydown event.
415 control = false;
416 alt = false;
417 }
418 break;
419
420 case 'right-alt':
421 if (isPrintable && (this.terminal.keyboard.altKeyPressed & 2)) {
422 control = false;
423 alt = false;
424 }
425 break;
426
427 case 'left-alt':
428 if (isPrintable && (this.terminal.keyboard.altKeyPressed & 1)) {
429 control = false;
430 alt = false;
431 }
432 break;
Robert Ginda4c66c9a2015-02-18 13:16:21 -0800433 }
434
rgindafeaf3142012-01-31 15:14:20 -0800435 var action;
436
437 if (control) {
438 action = getAction('control');
439 } else if (alt) {
440 action = getAction('alt');
rginda42ad71d2012-02-16 14:06:28 -0800441 } else if (meta) {
442 action = getAction('meta');
rgindafeaf3142012-01-31 15:14:20 -0800443 } else {
444 action = getAction('normal');
445 }
446
Robert Gindaf82267d2015-06-09 15:32:04 -0700447 // If e.maskShiftKey was set (during getAction) it means the shift key is
448 // already accounted for in the action, and we should not act on it any
449 // further. This is currently only used for Ctrl-Shift-Tab, which should send
450 // "CSI Z", not "CSI 1 ; 2 Z".
451 var shift = !e.maskShiftKey && e.shiftKey;
452
453 var keyDown = {
454 keyCode: e.keyCode,
455 shift: e.shiftKey, // not `var shift` from above.
456 ctrl: control,
457 alt: alt,
458 meta: meta
459 };
460
461 var binding = this.bindings.getBinding(keyDown);
462
463 if (binding) {
464 // Clear out the modifier bits so we don't try to munge the sequence
465 // further.
466 shift = control = alt = meta = false;
467 resolvedActionType = 'normal';
468 action = binding.action;
469
470 if (typeof action == 'function')
471 action = action.call(this, this.terminal, keyDown);
472 }
473
rginda39bdf6f2012-04-10 16:50:55 -0700474 if (alt && this.altSendsWhat == 'browser-key' && action == DEFAULT) {
475 // When altSendsWhat is 'browser-key', we wait for the keypress event.
476 // In keypress, the browser should have set the event.charCode to the
477 // appropriate character.
478 // TODO(rginda): Character compositions will need some black magic.
479 action = PASS;
480 }
481
rgindafeaf3142012-01-31 15:14:20 -0800482 if (action === PASS || (action === DEFAULT && !(control || alt || meta))) {
483 // If this key is supposed to be handled by the browser, or it is an
484 // unmodified key with the default action, then exit this event handler.
485 // If it's an unmodified key, it'll be handled in onKeyPress where we
486 // can tell for sure which ASCII code to insert.
487 //
488 // This block needs to come before the STRIP test, otherwise we'll strip
489 // the modifier and think it's ok to let the browser handle the keypress.
490 // The browser won't know we're trying to ignore the modifiers and might
491 // perform some default action.
492 return;
493 }
494
495 if (action === STRIP) {
496 alt = control = false;
497 action = keyDef.normal;
498 if (typeof action == 'function')
499 action = action.apply(this.keyMap, [e, keyDef]);
500
501 if (action == DEFAULT && keyDef.keyCap.length == 2)
Robert Gindaf82267d2015-06-09 15:32:04 -0700502 action = keyDef.keyCap.substr((shift ? 1 : 0), 1);
rgindafeaf3142012-01-31 15:14:20 -0800503 }
504
505 e.preventDefault();
506 e.stopPropagation();
507
508 if (action === CANCEL)
509 return;
510
rginda42ad71d2012-02-16 14:06:28 -0800511 if (action !== DEFAULT && typeof action != 'string') {
512 console.warn('Invalid action: ' + JSON.stringify(action));
513 return;
514 }
515
Robert Gindacc6d3a72012-09-24 14:06:08 -0700516 // Strip the modifier that is associated with the action, since we assume that
517 // modifier has already been accounted for in the action.
518 if (resolvedActionType == 'control') {
519 control = false;
520 } else if (resolvedActionType == 'alt') {
521 alt = false;
522 } else if (resolvedActionType == 'meta') {
523 meta = false;
524 }
525
Mike Frysinger86780fb2017-09-15 23:17:05 -0400526 if (action.substr(0, 2) == '\x1b[' && (alt || control || shift || meta)) {
Robert Gindacc6d3a72012-09-24 14:06:08 -0700527 // The action is an escape sequence that and it was triggered in the
528 // presence of a keyboard modifier, we may need to alter the action to
529 // include the modifier before sending it.
rginda42ad71d2012-02-16 14:06:28 -0800530
Mike Frysinger86780fb2017-09-15 23:17:05 -0400531 // The math is funky but aligns w/xterm.
532 let imod = 1;
533 if (shift)
534 imod += 1;
535 if (alt)
536 imod += 2;
537 if (control)
538 imod += 4;
539 if (meta)
540 imod += 8;
541 let mod = ';' + imod;
rgindafeaf3142012-01-31 15:14:20 -0800542
543 if (action.length == 3) {
544 // Some of the CSI sequences have zero parameters unless modified.
545 action = '\x1b[1' + mod + action.substr(2, 1);
546 } else {
547 // Others always have at least one parameter.
Robert Ginda21de3762012-09-20 16:38:18 -0700548 action = action.substr(0, action.length - 1) + mod +
rgindafeaf3142012-01-31 15:14:20 -0800549 action.substr(action.length - 1);
550 }
Robert Ginda21de3762012-09-20 16:38:18 -0700551
552 } else {
Robert Ginda21de3762012-09-20 16:38:18 -0700553 if (action === DEFAULT) {
Robert Gindaf82267d2015-06-09 15:32:04 -0700554 action = keyDef.keyCap.substr((shift ? 1 : 0), 1);
Robert Ginda8cb7d902013-06-20 14:37:18 -0700555
Robert Ginda21de3762012-09-20 16:38:18 -0700556 if (control) {
557 var unshifted = keyDef.keyCap.substr(0, 1);
558 var code = unshifted.charCodeAt(0);
559 if (code >= 64 && code <= 95) {
Robert Ginda8cb7d902013-06-20 14:37:18 -0700560 action = String.fromCharCode(code - 64);
Robert Ginda21de3762012-09-20 16:38:18 -0700561 }
Robert Ginda21de3762012-09-20 16:38:18 -0700562 }
563 }
564
Robert Ginda8cb7d902013-06-20 14:37:18 -0700565 if (alt && this.altSendsWhat == '8-bit' && action.length == 1) {
566 var code = action.charCodeAt(0) + 128;
567 action = String.fromCharCode(code);
568 }
569
Robert Ginda21de3762012-09-20 16:38:18 -0700570 // We respect alt/metaSendsEscape even if the keymap action was a literal
571 // string. Otherwise, every overridden alt/meta action would have to
572 // check alt/metaSendsEscape.
Robert Gindacc6d3a72012-09-24 14:06:08 -0700573 if ((alt && this.altSendsWhat == 'escape') ||
574 (meta && this.metaSendsEscape)) {
Robert Ginda21de3762012-09-20 16:38:18 -0700575 action = '\x1b' + action;
576 }
rgindafeaf3142012-01-31 15:14:20 -0800577 }
578
579 this.terminal.onVTKeystroke(action);
580};