rginda | feaf314 | 2012-01-31 15:14:20 -0800 | [diff] [blame] | 1 | // 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 | |
rginda | cbbd748 | 2012-06-13 15:06:16 -0700 | [diff] [blame] | 5 | 'use strict'; |
| 6 | |
| 7 | lib.rtdep('hterm.Keyboard.KeyMap'); |
| 8 | |
rginda | feaf314 | 2012-01-31 15:14:20 -0800 | [diff] [blame] | 9 | /** |
| 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 | */ |
| 19 | hterm.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 Frysinger | c527161 | 2017-04-12 02:24:30 -0400 | [diff] [blame] | 29 | ['focusout', this.onFocusOut_.bind(this)], |
Robert Ginda | 11390c5 | 2012-09-13 14:53:34 -0700 | [diff] [blame] | 30 | ['keydown', this.onKeyDown_.bind(this)], |
Andrew de los Reyes | 574e10e | 2013-04-04 09:31:57 -0700 | [diff] [blame] | 31 | ['keypress', this.onKeyPress_.bind(this)], |
| 32 | ['keyup', this.onKeyUp_.bind(this)], |
Robert Ginda | 11390c5 | 2012-09-13 14:53:34 -0700 | [diff] [blame] | 33 | ['textInput', this.onTextInput_.bind(this)] |
rginda | feaf314 | 2012-01-31 15:14:20 -0800 | [diff] [blame] | 34 | ]; |
| 35 | |
| 36 | /** |
| 37 | * The current key map. |
| 38 | */ |
rginda | cbbd748 | 2012-06-13 15:06:16 -0700 | [diff] [blame] | 39 | this.keyMap = new hterm.Keyboard.KeyMap(this); |
rginda | feaf314 | 2012-01-31 15:14:20 -0800 | [diff] [blame] | 40 | |
Robert Ginda | f82267d | 2015-06-09 15:32:04 -0700 | [diff] [blame] | 41 | this.bindings = new hterm.Keyboard.Bindings(this); |
| 42 | |
rginda | feaf314 | 2012-01-31 15:14:20 -0800 | [diff] [blame] | 43 | /** |
Robert Ginda | 034ffa7 | 2015-02-26 14:02:37 -0800 | [diff] [blame] | 44 | * 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 | /** |
rginda | 4bba5e1 | 2012-06-20 16:15:30 -0700 | [diff] [blame] | 52 | * 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 Ginda | 57f03b4 | 2012-09-13 11:02:48 -0700 | [diff] [blame] | 55 | this.shiftInsertPaste = true; |
rginda | 4bba5e1 | 2012-06-20 16:15:30 -0700 | [diff] [blame] | 56 | |
| 57 | /** |
rginda | feaf314 | 2012-01-31 15:14:20 -0800 | [diff] [blame] | 58 | * 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 Ginda | 57f03b4 | 2012-09-13 11:02:48 -0700 | [diff] [blame] | 62 | this.homeKeysScroll = false; |
rginda | feaf314 | 2012-01-31 15:14:20 -0800 | [diff] [blame] | 63 | |
| 64 | /** |
| 65 | * Same as above, except for page up/page down. |
| 66 | */ |
Robert Ginda | 57f03b4 | 2012-09-13 11:02:48 -0700 | [diff] [blame] | 67 | this.pageKeysScroll = false; |
rginda | feaf314 | 2012-01-31 15:14:20 -0800 | [diff] [blame] | 68 | |
| 69 | /** |
Robert Ginda | 7e5e952 | 2014-03-14 12:23:58 -0700 | [diff] [blame] | 70 | * 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 Ginda | fb5a3f9 | 2014-05-13 14:12:00 -0700 | [diff] [blame] | 77 | * 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 Mesquita | 61e7c31 | 2014-01-04 12:53:12 +0100 | [diff] [blame] | 85 | */ |
| 86 | this.ctrlVPaste = false; |
| 87 | |
| 88 | /** |
rginda | feaf314 | 2012-01-31 15:14:20 -0800 | [diff] [blame] | 89 | * 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 Ginda | 57f03b4 | 2012-09-13 11:02:48 -0700 | [diff] [blame] | 106 | this.backspaceSendsBackspace = false; |
rginda | feaf314 | 2012-01-31 15:14:20 -0800 | [diff] [blame] | 107 | |
| 108 | /** |
Robert Ginda | 8cb7d90 | 2013-06-20 14:37:18 -0700 | [diff] [blame] | 109 | * The encoding method for data sent to the host. |
| 110 | */ |
| 111 | this.characterEncoding = 'utf-8'; |
| 112 | |
| 113 | /** |
rginda | feaf314 | 2012-01-31 15:14:20 -0800 | [diff] [blame] | 114 | * Set whether the meta key sends a leading escape or not. |
| 115 | */ |
Robert Ginda | 57f03b4 | 2012-09-13 11:02:48 -0700 | [diff] [blame] | 116 | this.metaSendsEscape = true; |
rginda | 30f20f6 | 2012-04-05 16:36:19 -0700 | [diff] [blame] | 117 | |
| 118 | /** |
Marius Schilder | 77857b3 | 2014-05-14 16:21:26 -0700 | [diff] [blame] | 119 | * Set whether meta-V gets passed to host. |
| 120 | */ |
| 121 | this.passMetaV = true; |
| 122 | |
| 123 | /** |
rginda | 39bdf6f | 2012-04-10 16:50:55 -0700 | [diff] [blame] | 124 | * Controls how the alt key is handled. |
| 125 | * |
| 126 | * escape....... Send an ESC prefix. |
| 127 | * 8-bit........ Add 128 to the unshifted character as in xterm. |
| 128 | * browser-key.. Wait for the keypress event and see what the browser says. |
| 129 | * (This won't work well on platforms where the browser |
| 130 | * performs a default action for some alt sequences.) |
rginda | 30f20f6 | 2012-04-05 16:36:19 -0700 | [diff] [blame] | 131 | * |
| 132 | * This setting only matters when alt is distinct from meta (altIsMeta is |
| 133 | * false.) |
| 134 | */ |
Robert Ginda | 57f03b4 | 2012-09-13 11:02:48 -0700 | [diff] [blame] | 135 | this.altSendsWhat = 'escape'; |
rginda | 42ad71d | 2012-02-16 14:06:28 -0800 | [diff] [blame] | 136 | |
| 137 | /** |
| 138 | * Set whether the alt key acts as a meta key, instead of producing 8-bit |
| 139 | * characters. |
rginda | 30f20f6 | 2012-04-05 16:36:19 -0700 | [diff] [blame] | 140 | * |
| 141 | * True to enable, false to disable, null to autodetect based on platform. |
rginda | 42ad71d | 2012-02-16 14:06:28 -0800 | [diff] [blame] | 142 | */ |
Robert Ginda | 57f03b4 | 2012-09-13 11:02:48 -0700 | [diff] [blame] | 143 | this.altIsMeta = false; |
Andrew de los Reyes | 574e10e | 2013-04-04 09:31:57 -0700 | [diff] [blame] | 144 | |
| 145 | /** |
| 146 | * If true, tries to detect DEL key events that are from alt-backspace on |
| 147 | * Chrome OS vs from a true DEL key press. |
| 148 | * |
| 149 | * Background: At the time of writing, on Chrome OS, alt-backspace is mapped |
| 150 | * to DEL. Some users may be happy with this, but others may be frustrated |
| 151 | * that it's impossible to do meta-backspace. If the user enables this pref, |
| 152 | * we use a trick to tell a true DEL keypress from alt-backspace: on |
| 153 | * alt-backspace, we will see the alt key go down, then get a DEL keystroke |
Mike Frysinger | e453318 | 2017-04-19 00:18:29 -0400 | [diff] [blame] | 154 | * that indicates that alt is not pressed. See https://crbug.com/174410 . |
Andrew de los Reyes | 574e10e | 2013-04-04 09:31:57 -0700 | [diff] [blame] | 155 | */ |
| 156 | this.altBackspaceIsMetaBackspace = false; |
| 157 | |
| 158 | /** |
| 159 | * Used to keep track of the current alt-key state, which is necessary for |
Robert Ginda | 034ffa7 | 2015-02-26 14:02:37 -0800 | [diff] [blame] | 160 | * the altBackspaceIsMetaBackspace preference above and for the altGrMode |
| 161 | * preference. This is a bitmap with where bit positions correspond to the |
| 162 | * "location" property of the key event. |
Andrew de los Reyes | 574e10e | 2013-04-04 09:31:57 -0700 | [diff] [blame] | 163 | */ |
Robert Ginda | 034ffa7 | 2015-02-26 14:02:37 -0800 | [diff] [blame] | 164 | this.altKeyPressed = 0; |
Andrew de los Reyes | 6af23ae | 2013-04-04 14:17:50 -0700 | [diff] [blame] | 165 | |
| 166 | /** |
| 167 | * If true, Chrome OS media keys will be mapped to their F-key equivalent. |
| 168 | * E.g. "Back" will be mapped to F1. If false, Chrome will handle the keys. |
| 169 | */ |
| 170 | this.mediaKeysAreFKeys = false; |
Brad Town | 5f375a4 | 2015-03-12 01:30:58 -0700 | [diff] [blame] | 171 | |
| 172 | /** |
| 173 | * Holds the previous setting of altSendsWhat when DECSET 1039 is used. When |
| 174 | * DECRST 1039 is used, altSendsWhat is changed back to this and this is |
| 175 | * nulled out. |
| 176 | */ |
| 177 | this.previousAltSendsWhat_ = null; |
rginda | feaf314 | 2012-01-31 15:14:20 -0800 | [diff] [blame] | 178 | }; |
| 179 | |
| 180 | /** |
rginda | feaf314 | 2012-01-31 15:14:20 -0800 | [diff] [blame] | 181 | * Special handling for keyCodes in a keyboard layout. |
| 182 | */ |
| 183 | hterm.Keyboard.KeyActions = { |
| 184 | /** |
| 185 | * Call preventDefault and stopPropagation for this key event and nothing |
| 186 | * else. |
| 187 | */ |
Mike Frysinger | c0d77ea | 2017-05-17 22:33:30 -0400 | [diff] [blame] | 188 | CANCEL: lib.f.createEnum('CANCEL'), |
rginda | feaf314 | 2012-01-31 15:14:20 -0800 | [diff] [blame] | 189 | |
| 190 | /** |
| 191 | * This performs the default terminal action for the key. If used in the |
| 192 | * 'normal' action and the the keystroke represents a printable key, the |
| 193 | * character will be sent to the host. If used in one of the modifier |
| 194 | * actions, the terminal will perform the normal action after (possibly) |
| 195 | * altering it. |
| 196 | * |
| 197 | * - If the normal sequence starts with CSI, the sequence will be adjusted |
| 198 | * to include the modifier parameter as described in [XTERM] in the final |
| 199 | * table of the "PC-Style Function Keys" section. |
| 200 | * |
| 201 | * - If the control key is down and the key represents a printable character, |
| 202 | * and the uppercase version of the unshifted keycap is between |
| 203 | * 64 (ASCII '@') and 95 (ASCII '_'), then the uppercase version of the |
| 204 | * unshifted keycap minus 64 is sent. This makes '^@' send '\x00' and |
| 205 | * '^_' send '\x1f'. (Note that one higher that 0x1f is 0x20, which is |
| 206 | * the first printable ASCII value.) |
| 207 | * |
| 208 | * - If the alt key is down and the key represents a printable character then |
| 209 | * the value of the character is shifted up by 128. |
| 210 | * |
| 211 | * - If meta is down and configured to send an escape, '\x1b' will be sent |
| 212 | * before the normal action is performed. |
| 213 | */ |
Mike Frysinger | c0d77ea | 2017-05-17 22:33:30 -0400 | [diff] [blame] | 214 | DEFAULT: lib.f.createEnum('DEFAULT'), |
rginda | feaf314 | 2012-01-31 15:14:20 -0800 | [diff] [blame] | 215 | |
| 216 | /** |
| 217 | * Causes the terminal to opt out of handling the key event, instead letting |
| 218 | * the browser deal with it. |
| 219 | */ |
Mike Frysinger | c0d77ea | 2017-05-17 22:33:30 -0400 | [diff] [blame] | 220 | PASS: lib.f.createEnum('PASS'), |
rginda | feaf314 | 2012-01-31 15:14:20 -0800 | [diff] [blame] | 221 | |
| 222 | /** |
| 223 | * Insert the first or second character of the keyCap, based on e.shiftKey. |
| 224 | * The key will be handled in onKeyDown, and e.preventDefault() will be |
| 225 | * called. |
| 226 | * |
| 227 | * It is useful for a modified key action, where it essentially strips the |
| 228 | * modifier while preventing the browser from reacting to the key. |
| 229 | */ |
Mike Frysinger | c0d77ea | 2017-05-17 22:33:30 -0400 | [diff] [blame] | 230 | STRIP: lib.f.createEnum('STRIP') |
rginda | feaf314 | 2012-01-31 15:14:20 -0800 | [diff] [blame] | 231 | }; |
| 232 | |
| 233 | /** |
Robert Ginda | 8cb7d90 | 2013-06-20 14:37:18 -0700 | [diff] [blame] | 234 | * Encode a string according to the 'send-encoding' preference. |
| 235 | */ |
| 236 | hterm.Keyboard.prototype.encode = function(str) { |
| 237 | if (this.characterEncoding == 'utf-8') |
| 238 | return this.terminal.vt.encodeUTF8(str); |
| 239 | |
| 240 | return str; |
| 241 | }; |
| 242 | |
| 243 | /** |
rginda | feaf314 | 2012-01-31 15:14:20 -0800 | [diff] [blame] | 244 | * Capture keyboard events sent to the associated element. |
| 245 | * |
| 246 | * This enables the keyboard. Captured events are consumed by this class |
| 247 | * and will not perform their default action or bubble to other elements. |
| 248 | * |
| 249 | * Passing a null element will uninstall the keyboard handlers. |
| 250 | * |
| 251 | * @param {HTMLElement} element The element whose events should be captured, or |
| 252 | * null to disable the keyboard. |
| 253 | */ |
| 254 | hterm.Keyboard.prototype.installKeyboard = function(element) { |
| 255 | if (element == this.keyboardElement_) |
| 256 | return; |
| 257 | |
| 258 | if (element && this.keyboardElement_) |
| 259 | this.installKeyboard(null); |
| 260 | |
| 261 | for (var i = 0; i < this.handlers_.length; i++) { |
| 262 | var handler = this.handlers_[i]; |
| 263 | if (element) { |
| 264 | element.addEventListener(handler[0], handler[1]); |
| 265 | } else { |
| 266 | this.keyboardElement_.removeEventListener(handler[0], handler[1]); |
| 267 | } |
| 268 | } |
| 269 | |
| 270 | this.keyboardElement_ = element; |
| 271 | }; |
| 272 | |
| 273 | /** |
| 274 | * Disable keyboard event capture. |
| 275 | * |
| 276 | * This will allow the browser to process key events normally. |
| 277 | */ |
| 278 | hterm.Keyboard.prototype.uninstallKeyboard = function() { |
| 279 | this.installKeyboard(null); |
| 280 | }; |
| 281 | |
| 282 | /** |
Robert Ginda | 11390c5 | 2012-09-13 14:53:34 -0700 | [diff] [blame] | 283 | * Handle onTextInput events. |
| 284 | * |
Mike Frysinger | e5a61b0 | 2017-09-13 21:58:29 -0400 | [diff] [blame] | 285 | * These are generated when using IMEs, Virtual Keyboards (VKs), compose keys, |
| 286 | * Unicode input, etc... |
Robert Ginda | 11390c5 | 2012-09-13 14:53:34 -0700 | [diff] [blame] | 287 | */ |
| 288 | hterm.Keyboard.prototype.onTextInput_ = function(e) { |
| 289 | if (!e.data) |
| 290 | return; |
| 291 | |
Mike Frysinger | e5a61b0 | 2017-09-13 21:58:29 -0400 | [diff] [blame] | 292 | // Just pass the generated buffer straight down. No need for us to split it |
| 293 | // up or otherwise parse it ahead of times. |
| 294 | this.terminal.onVTKeystroke(e.data); |
Robert Ginda | 11390c5 | 2012-09-13 14:53:34 -0700 | [diff] [blame] | 295 | }; |
| 296 | |
| 297 | /** |
rginda | feaf314 | 2012-01-31 15:14:20 -0800 | [diff] [blame] | 298 | * Handle onKeyPress events. |
| 299 | */ |
| 300 | hterm.Keyboard.prototype.onKeyPress_ = function(e) { |
rginda | 39bdf6f | 2012-04-10 16:50:55 -0700 | [diff] [blame] | 301 | var code; |
| 302 | |
Rob Spies | 0bec09b | 2014-06-06 15:58:09 -0700 | [diff] [blame] | 303 | var key = String.fromCharCode(e.which); |
| 304 | var lowerKey = key.toLowerCase(); |
| 305 | if ((e.ctrlKey || e.metaKey) && (lowerKey == 'c' || lowerKey == 'v')) { |
| 306 | // On FF the key press (not key down) event gets fired for copy/paste. |
Zhu Qunying | 30d4071 | 2017-03-14 16:27:00 -0700 | [diff] [blame] | 307 | // Let it fall through for the default browser behavior. |
Rob Spies | 0bec09b | 2014-06-06 15:58:09 -0700 | [diff] [blame] | 308 | return; |
| 309 | } |
| 310 | |
rginda | 39bdf6f | 2012-04-10 16:50:55 -0700 | [diff] [blame] | 311 | if (e.altKey && this.altSendsWhat == 'browser-key' && e.charCode == 0) { |
| 312 | // If we got here because we were expecting the browser to handle an |
| 313 | // alt sequence but it didn't do it, then we might be on an OS without |
| 314 | // an enabled IME system. In that case we fall back to xterm-like |
| 315 | // behavior. |
| 316 | // |
| 317 | // This happens here only as a fallback. Typically these platforms should |
| 318 | // set altSendsWhat to either 'escape' or '8-bit'. |
| 319 | var ch = String.fromCharCode(e.keyCode); |
| 320 | if (!e.shiftKey) |
| 321 | ch = ch.toLowerCase(); |
| 322 | code = ch.charCodeAt(0) + 128; |
| 323 | |
| 324 | } else if (e.charCode >= 32) { |
| 325 | ch = e.charCode; |
| 326 | } |
| 327 | |
Robert Ginda | 8cb7d90 | 2013-06-20 14:37:18 -0700 | [diff] [blame] | 328 | if (ch) |
| 329 | this.terminal.onVTKeystroke(String.fromCharCode(ch)); |
rginda | feaf314 | 2012-01-31 15:14:20 -0800 | [diff] [blame] | 330 | |
| 331 | e.preventDefault(); |
| 332 | e.stopPropagation(); |
| 333 | }; |
| 334 | |
Marius Schilder | 4570317 | 2014-12-22 17:10:01 -0800 | [diff] [blame] | 335 | /** |
Marius Schilder | 9dc111b | 2015-08-13 14:09:39 -0700 | [diff] [blame] | 336 | * Prevent default handling for non-ctrl-shifted event. |
Marius Schilder | 4570317 | 2014-12-22 17:10:01 -0800 | [diff] [blame] | 337 | * |
| 338 | * When combined with Chrome permission 'app.window.fullscreen.overrideEsc', |
| 339 | * and called for both key down and key up events, |
| 340 | * the ESC key remains usable within fullscreen Chrome app windows. |
| 341 | */ |
Marius Schilder | 9dc111b | 2015-08-13 14:09:39 -0700 | [diff] [blame] | 342 | hterm.Keyboard.prototype.preventChromeAppNonCtrlShiftDefault_ = function(e) { |
Marius Schilder | 4570317 | 2014-12-22 17:10:01 -0800 | [diff] [blame] | 343 | if (!window.chrome || !window.chrome.app || !window.chrome.app.window) |
| 344 | return; |
Marius Schilder | 9dc111b | 2015-08-13 14:09:39 -0700 | [diff] [blame] | 345 | if (!e.ctrlKey || !e.shiftKey) |
Marius Schilder | 4570317 | 2014-12-22 17:10:01 -0800 | [diff] [blame] | 346 | e.preventDefault(); |
| 347 | }; |
| 348 | |
Mike Frysinger | c527161 | 2017-04-12 02:24:30 -0400 | [diff] [blame] | 349 | hterm.Keyboard.prototype.onFocusOut_ = function(e) { |
Robert Ginda | 034ffa7 | 2015-02-26 14:02:37 -0800 | [diff] [blame] | 350 | this.altKeyPressed = 0; |
Andrew de los Reyes | 574e10e | 2013-04-04 09:31:57 -0700 | [diff] [blame] | 351 | }; |
| 352 | |
| 353 | hterm.Keyboard.prototype.onKeyUp_ = function(e) { |
| 354 | if (e.keyCode == 18) |
Robert Ginda | 034ffa7 | 2015-02-26 14:02:37 -0800 | [diff] [blame] | 355 | this.altKeyPressed = this.altKeyPressed & ~(1 << (e.location - 1)); |
| 356 | |
Marius Schilder | 4570317 | 2014-12-22 17:10:01 -0800 | [diff] [blame] | 357 | if (e.keyCode == 27) |
Marius Schilder | 9dc111b | 2015-08-13 14:09:39 -0700 | [diff] [blame] | 358 | this.preventChromeAppNonCtrlShiftDefault_(e); |
Andrew de los Reyes | 574e10e | 2013-04-04 09:31:57 -0700 | [diff] [blame] | 359 | }; |
| 360 | |
rginda | feaf314 | 2012-01-31 15:14:20 -0800 | [diff] [blame] | 361 | /** |
| 362 | * Handle onKeyDown events. |
| 363 | */ |
| 364 | hterm.Keyboard.prototype.onKeyDown_ = function(e) { |
Andrew de los Reyes | 574e10e | 2013-04-04 09:31:57 -0700 | [diff] [blame] | 365 | if (e.keyCode == 18) |
Robert Ginda | 034ffa7 | 2015-02-26 14:02:37 -0800 | [diff] [blame] | 366 | this.altKeyPressed = this.altKeyPressed | (1 << (e.location - 1)); |
| 367 | |
Marius Schilder | 4570317 | 2014-12-22 17:10:01 -0800 | [diff] [blame] | 368 | if (e.keyCode == 27) |
Marius Schilder | 9dc111b | 2015-08-13 14:09:39 -0700 | [diff] [blame] | 369 | this.preventChromeAppNonCtrlShiftDefault_(e); |
Andrew de los Reyes | 574e10e | 2013-04-04 09:31:57 -0700 | [diff] [blame] | 370 | |
rginda | feaf314 | 2012-01-31 15:14:20 -0800 | [diff] [blame] | 371 | var keyDef = this.keyMap.keyDefs[e.keyCode]; |
| 372 | if (!keyDef) { |
Mike Frysinger | 53e2999 | 2017-12-12 09:36:27 -0500 | [diff] [blame^] | 373 | // If this key hasn't been explicitly registered, fall back to the unknown |
| 374 | // key mapping (keyCode == 0), and then automatically register it to avoid |
| 375 | // any further warnings here. |
| 376 | console.warn(`No definition for key ${e.key} (keyCode ${e.keyCode})`); |
| 377 | keyDef = this.keyMap.keyDefs[0]; |
| 378 | this.keyMap.addKeyDef(e.keyCode, keyDef); |
rginda | feaf314 | 2012-01-31 15:14:20 -0800 | [diff] [blame] | 379 | } |
| 380 | |
Robert Ginda | 21de376 | 2012-09-20 16:38:18 -0700 | [diff] [blame] | 381 | // The type of action we're going to use. |
| 382 | var resolvedActionType = null; |
| 383 | |
rginda | feaf314 | 2012-01-31 15:14:20 -0800 | [diff] [blame] | 384 | var self = this; |
| 385 | function getAction(name) { |
| 386 | // Get the key action for the given action name. If the action is a |
| 387 | // function, dispatch it. If the action defers to the normal action, |
| 388 | // resolve that instead. |
| 389 | |
Robert Ginda | 21de376 | 2012-09-20 16:38:18 -0700 | [diff] [blame] | 390 | resolvedActionType = name; |
| 391 | |
rginda | feaf314 | 2012-01-31 15:14:20 -0800 | [diff] [blame] | 392 | var action = keyDef[name]; |
| 393 | if (typeof action == 'function') |
| 394 | action = action.apply(self.keyMap, [e, keyDef]); |
| 395 | |
| 396 | if (action === DEFAULT && name != 'normal') |
| 397 | action = getAction('normal'); |
| 398 | |
| 399 | return action; |
| 400 | } |
| 401 | |
| 402 | // Note that we use the triple-equals ('===') operator to test equality for |
Zhu Qunying | 30d4071 | 2017-03-14 16:27:00 -0700 | [diff] [blame] | 403 | // these constants, in order to distinguish usage of the constant from usage |
rginda | feaf314 | 2012-01-31 15:14:20 -0800 | [diff] [blame] | 404 | // of a literal string that happens to contain the same bytes. |
| 405 | var CANCEL = hterm.Keyboard.KeyActions.CANCEL; |
| 406 | var DEFAULT = hterm.Keyboard.KeyActions.DEFAULT; |
| 407 | var PASS = hterm.Keyboard.KeyActions.PASS; |
| 408 | var STRIP = hterm.Keyboard.KeyActions.STRIP; |
| 409 | |
rginda | feaf314 | 2012-01-31 15:14:20 -0800 | [diff] [blame] | 410 | var control = e.ctrlKey; |
rginda | 42ad71d | 2012-02-16 14:06:28 -0800 | [diff] [blame] | 411 | var alt = this.altIsMeta ? false : e.altKey; |
| 412 | var meta = this.altIsMeta ? (e.altKey || e.metaKey) : e.metaKey; |
rginda | feaf314 | 2012-01-31 15:14:20 -0800 | [diff] [blame] | 413 | |
Robert Ginda | 4c66c9a | 2015-02-18 13:16:21 -0800 | [diff] [blame] | 414 | // In the key-map, we surround the keyCap for non-printables in "[...]" |
| 415 | var isPrintable = !(/^\[\w+\]$/.test(keyDef.keyCap)); |
| 416 | |
Robert Ginda | 034ffa7 | 2015-02-26 14:02:37 -0800 | [diff] [blame] | 417 | switch (this.altGrMode) { |
| 418 | case 'ctrl-alt': |
| 419 | if (isPrintable && control && alt) { |
| 420 | // ctrl-alt-printable means altGr. We clear out the control and |
| 421 | // alt modifiers and wait to see the charCode in the keydown event. |
| 422 | control = false; |
| 423 | alt = false; |
| 424 | } |
| 425 | break; |
| 426 | |
| 427 | case 'right-alt': |
| 428 | if (isPrintable && (this.terminal.keyboard.altKeyPressed & 2)) { |
| 429 | control = false; |
| 430 | alt = false; |
| 431 | } |
| 432 | break; |
| 433 | |
| 434 | case 'left-alt': |
| 435 | if (isPrintable && (this.terminal.keyboard.altKeyPressed & 1)) { |
| 436 | control = false; |
| 437 | alt = false; |
| 438 | } |
| 439 | break; |
Robert Ginda | 4c66c9a | 2015-02-18 13:16:21 -0800 | [diff] [blame] | 440 | } |
| 441 | |
rginda | feaf314 | 2012-01-31 15:14:20 -0800 | [diff] [blame] | 442 | var action; |
| 443 | |
| 444 | if (control) { |
| 445 | action = getAction('control'); |
| 446 | } else if (alt) { |
| 447 | action = getAction('alt'); |
rginda | 42ad71d | 2012-02-16 14:06:28 -0800 | [diff] [blame] | 448 | } else if (meta) { |
| 449 | action = getAction('meta'); |
rginda | feaf314 | 2012-01-31 15:14:20 -0800 | [diff] [blame] | 450 | } else { |
| 451 | action = getAction('normal'); |
| 452 | } |
| 453 | |
Robert Ginda | f82267d | 2015-06-09 15:32:04 -0700 | [diff] [blame] | 454 | // If e.maskShiftKey was set (during getAction) it means the shift key is |
| 455 | // already accounted for in the action, and we should not act on it any |
| 456 | // further. This is currently only used for Ctrl-Shift-Tab, which should send |
| 457 | // "CSI Z", not "CSI 1 ; 2 Z". |
| 458 | var shift = !e.maskShiftKey && e.shiftKey; |
| 459 | |
| 460 | var keyDown = { |
| 461 | keyCode: e.keyCode, |
| 462 | shift: e.shiftKey, // not `var shift` from above. |
| 463 | ctrl: control, |
| 464 | alt: alt, |
| 465 | meta: meta |
| 466 | }; |
| 467 | |
| 468 | var binding = this.bindings.getBinding(keyDown); |
| 469 | |
| 470 | if (binding) { |
| 471 | // Clear out the modifier bits so we don't try to munge the sequence |
| 472 | // further. |
| 473 | shift = control = alt = meta = false; |
| 474 | resolvedActionType = 'normal'; |
| 475 | action = binding.action; |
| 476 | |
| 477 | if (typeof action == 'function') |
| 478 | action = action.call(this, this.terminal, keyDown); |
| 479 | } |
| 480 | |
rginda | 39bdf6f | 2012-04-10 16:50:55 -0700 | [diff] [blame] | 481 | if (alt && this.altSendsWhat == 'browser-key' && action == DEFAULT) { |
| 482 | // When altSendsWhat is 'browser-key', we wait for the keypress event. |
| 483 | // In keypress, the browser should have set the event.charCode to the |
| 484 | // appropriate character. |
| 485 | // TODO(rginda): Character compositions will need some black magic. |
| 486 | action = PASS; |
| 487 | } |
| 488 | |
rginda | feaf314 | 2012-01-31 15:14:20 -0800 | [diff] [blame] | 489 | if (action === PASS || (action === DEFAULT && !(control || alt || meta))) { |
| 490 | // If this key is supposed to be handled by the browser, or it is an |
| 491 | // unmodified key with the default action, then exit this event handler. |
| 492 | // If it's an unmodified key, it'll be handled in onKeyPress where we |
| 493 | // can tell for sure which ASCII code to insert. |
| 494 | // |
| 495 | // This block needs to come before the STRIP test, otherwise we'll strip |
| 496 | // the modifier and think it's ok to let the browser handle the keypress. |
| 497 | // The browser won't know we're trying to ignore the modifiers and might |
| 498 | // perform some default action. |
| 499 | return; |
| 500 | } |
| 501 | |
| 502 | if (action === STRIP) { |
| 503 | alt = control = false; |
| 504 | action = keyDef.normal; |
| 505 | if (typeof action == 'function') |
| 506 | action = action.apply(this.keyMap, [e, keyDef]); |
| 507 | |
| 508 | if (action == DEFAULT && keyDef.keyCap.length == 2) |
Robert Ginda | f82267d | 2015-06-09 15:32:04 -0700 | [diff] [blame] | 509 | action = keyDef.keyCap.substr((shift ? 1 : 0), 1); |
rginda | feaf314 | 2012-01-31 15:14:20 -0800 | [diff] [blame] | 510 | } |
| 511 | |
| 512 | e.preventDefault(); |
| 513 | e.stopPropagation(); |
| 514 | |
| 515 | if (action === CANCEL) |
| 516 | return; |
| 517 | |
rginda | 42ad71d | 2012-02-16 14:06:28 -0800 | [diff] [blame] | 518 | if (action !== DEFAULT && typeof action != 'string') { |
| 519 | console.warn('Invalid action: ' + JSON.stringify(action)); |
| 520 | return; |
| 521 | } |
| 522 | |
Robert Ginda | cc6d3a7 | 2012-09-24 14:06:08 -0700 | [diff] [blame] | 523 | // Strip the modifier that is associated with the action, since we assume that |
| 524 | // modifier has already been accounted for in the action. |
| 525 | if (resolvedActionType == 'control') { |
| 526 | control = false; |
| 527 | } else if (resolvedActionType == 'alt') { |
| 528 | alt = false; |
| 529 | } else if (resolvedActionType == 'meta') { |
| 530 | meta = false; |
| 531 | } |
| 532 | |
Mike Frysinger | 86780fb | 2017-09-15 23:17:05 -0400 | [diff] [blame] | 533 | if (action.substr(0, 2) == '\x1b[' && (alt || control || shift || meta)) { |
Robert Ginda | cc6d3a7 | 2012-09-24 14:06:08 -0700 | [diff] [blame] | 534 | // The action is an escape sequence that and it was triggered in the |
| 535 | // presence of a keyboard modifier, we may need to alter the action to |
| 536 | // include the modifier before sending it. |
rginda | 42ad71d | 2012-02-16 14:06:28 -0800 | [diff] [blame] | 537 | |
Mike Frysinger | 86780fb | 2017-09-15 23:17:05 -0400 | [diff] [blame] | 538 | // The math is funky but aligns w/xterm. |
| 539 | let imod = 1; |
| 540 | if (shift) |
| 541 | imod += 1; |
| 542 | if (alt) |
| 543 | imod += 2; |
| 544 | if (control) |
| 545 | imod += 4; |
| 546 | if (meta) |
| 547 | imod += 8; |
| 548 | let mod = ';' + imod; |
rginda | feaf314 | 2012-01-31 15:14:20 -0800 | [diff] [blame] | 549 | |
| 550 | if (action.length == 3) { |
| 551 | // Some of the CSI sequences have zero parameters unless modified. |
| 552 | action = '\x1b[1' + mod + action.substr(2, 1); |
| 553 | } else { |
| 554 | // Others always have at least one parameter. |
Robert Ginda | 21de376 | 2012-09-20 16:38:18 -0700 | [diff] [blame] | 555 | action = action.substr(0, action.length - 1) + mod + |
rginda | feaf314 | 2012-01-31 15:14:20 -0800 | [diff] [blame] | 556 | action.substr(action.length - 1); |
| 557 | } |
Robert Ginda | 21de376 | 2012-09-20 16:38:18 -0700 | [diff] [blame] | 558 | |
| 559 | } else { |
Robert Ginda | 21de376 | 2012-09-20 16:38:18 -0700 | [diff] [blame] | 560 | if (action === DEFAULT) { |
Robert Ginda | f82267d | 2015-06-09 15:32:04 -0700 | [diff] [blame] | 561 | action = keyDef.keyCap.substr((shift ? 1 : 0), 1); |
Robert Ginda | 8cb7d90 | 2013-06-20 14:37:18 -0700 | [diff] [blame] | 562 | |
Robert Ginda | 21de376 | 2012-09-20 16:38:18 -0700 | [diff] [blame] | 563 | if (control) { |
| 564 | var unshifted = keyDef.keyCap.substr(0, 1); |
| 565 | var code = unshifted.charCodeAt(0); |
| 566 | if (code >= 64 && code <= 95) { |
Robert Ginda | 8cb7d90 | 2013-06-20 14:37:18 -0700 | [diff] [blame] | 567 | action = String.fromCharCode(code - 64); |
Robert Ginda | 21de376 | 2012-09-20 16:38:18 -0700 | [diff] [blame] | 568 | } |
Robert Ginda | 21de376 | 2012-09-20 16:38:18 -0700 | [diff] [blame] | 569 | } |
| 570 | } |
| 571 | |
Robert Ginda | 8cb7d90 | 2013-06-20 14:37:18 -0700 | [diff] [blame] | 572 | if (alt && this.altSendsWhat == '8-bit' && action.length == 1) { |
| 573 | var code = action.charCodeAt(0) + 128; |
| 574 | action = String.fromCharCode(code); |
| 575 | } |
| 576 | |
Robert Ginda | 21de376 | 2012-09-20 16:38:18 -0700 | [diff] [blame] | 577 | // We respect alt/metaSendsEscape even if the keymap action was a literal |
| 578 | // string. Otherwise, every overridden alt/meta action would have to |
| 579 | // check alt/metaSendsEscape. |
Robert Ginda | cc6d3a7 | 2012-09-24 14:06:08 -0700 | [diff] [blame] | 580 | if ((alt && this.altSendsWhat == 'escape') || |
| 581 | (meta && this.metaSendsEscape)) { |
Robert Ginda | 21de376 | 2012-09-20 16:38:18 -0700 | [diff] [blame] | 582 | action = '\x1b' + action; |
| 583 | } |
rginda | feaf314 | 2012-01-31 15:14:20 -0800 | [diff] [blame] | 584 | } |
| 585 | |
| 586 | this.terminal.onVTKeystroke(action); |
| 587 | }; |