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