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