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_ = [ |
| 29 | ['keypress', this.onKeyPress_.bind(this)], |
Robert Ginda | 11390c5 | 2012-09-13 14:53:34 -0700 | [diff] [blame] | 30 | ['keydown', this.onKeyDown_.bind(this)], |
| 31 | ['textInput', this.onTextInput_.bind(this)] |
rginda | feaf314 | 2012-01-31 15:14:20 -0800 | [diff] [blame] | 32 | ]; |
| 33 | |
| 34 | /** |
| 35 | * The current key map. |
| 36 | */ |
rginda | cbbd748 | 2012-06-13 15:06:16 -0700 | [diff] [blame] | 37 | this.keyMap = new hterm.Keyboard.KeyMap(this); |
rginda | feaf314 | 2012-01-31 15:14:20 -0800 | [diff] [blame] | 38 | |
| 39 | /** |
rginda | 4bba5e1 | 2012-06-20 16:15:30 -0700 | [diff] [blame] | 40 | * If true, Shift-Insert will fall through to the browser as a paste. |
| 41 | * If false, the keystroke will be sent to the host. |
| 42 | */ |
| 43 | this.shiftInsertPaste = terminal.prefs_.get('shift-insert-paste'); |
| 44 | |
| 45 | /** |
rginda | feaf314 | 2012-01-31 15:14:20 -0800 | [diff] [blame] | 46 | * If true, home/end will control the terminal scrollbar and shift home/end |
| 47 | * will send the VT keycodes. If false then home/end sends VT codes and |
| 48 | * shift home/end scrolls. |
| 49 | */ |
rginda | 7e9460a | 2012-04-11 11:36:02 -0700 | [diff] [blame] | 50 | this.homeKeysScroll = terminal.prefs_.get('home-keys-scroll'); |
rginda | feaf314 | 2012-01-31 15:14:20 -0800 | [diff] [blame] | 51 | |
| 52 | /** |
| 53 | * Same as above, except for page up/page down. |
| 54 | */ |
rginda | 30f20f6 | 2012-04-05 16:36:19 -0700 | [diff] [blame] | 55 | this.pageKeysScroll = terminal.prefs_.get('page-keys-scroll'); |
rginda | feaf314 | 2012-01-31 15:14:20 -0800 | [diff] [blame] | 56 | |
| 57 | /** |
| 58 | * Enable/disable application keypad. |
| 59 | * |
| 60 | * This changes the way numeric keys are sent from the keyboard. |
| 61 | */ |
| 62 | this.applicationKeypad = false; |
| 63 | |
| 64 | /** |
| 65 | * Enable/disable the application cursor mode. |
| 66 | * |
| 67 | * This changes the way cursor keys are sent from the keyboard. |
| 68 | */ |
| 69 | this.applicationCursor = false; |
| 70 | |
| 71 | /** |
| 72 | * If true, the backspace should send BS ('\x08', aka ^H). Otherwise |
| 73 | * the backspace key should send '\x7f'. |
| 74 | */ |
rginda | 30f20f6 | 2012-04-05 16:36:19 -0700 | [diff] [blame] | 75 | this.backspaceSendsBackspace = terminal.prefs_.get( |
| 76 | 'backspace-sends-backspace'); |
rginda | feaf314 | 2012-01-31 15:14:20 -0800 | [diff] [blame] | 77 | |
| 78 | /** |
| 79 | * Set whether the meta key sends a leading escape or not. |
| 80 | */ |
rginda | 30f20f6 | 2012-04-05 16:36:19 -0700 | [diff] [blame] | 81 | this.metaSendsEscape = terminal.prefs_.get('meta-sends-escape'); |
| 82 | |
| 83 | /** |
rginda | 39bdf6f | 2012-04-10 16:50:55 -0700 | [diff] [blame] | 84 | * Controls how the alt key is handled. |
| 85 | * |
| 86 | * escape....... Send an ESC prefix. |
| 87 | * 8-bit........ Add 128 to the unshifted character as in xterm. |
| 88 | * browser-key.. Wait for the keypress event and see what the browser says. |
| 89 | * (This won't work well on platforms where the browser |
| 90 | * performs a default action for some alt sequences.) |
rginda | 30f20f6 | 2012-04-05 16:36:19 -0700 | [diff] [blame] | 91 | * |
| 92 | * This setting only matters when alt is distinct from meta (altIsMeta is |
| 93 | * false.) |
| 94 | */ |
rginda | 39bdf6f | 2012-04-10 16:50:55 -0700 | [diff] [blame] | 95 | this.altSendsWhat = terminal.prefs_.get('alt-sends-what'); |
rginda | 42ad71d | 2012-02-16 14:06:28 -0800 | [diff] [blame] | 96 | |
| 97 | /** |
| 98 | * Set whether the alt key acts as a meta key, instead of producing 8-bit |
| 99 | * characters. |
rginda | 30f20f6 | 2012-04-05 16:36:19 -0700 | [diff] [blame] | 100 | * |
| 101 | * True to enable, false to disable, null to autodetect based on platform. |
rginda | 42ad71d | 2012-02-16 14:06:28 -0800 | [diff] [blame] | 102 | */ |
rginda | 30f20f6 | 2012-04-05 16:36:19 -0700 | [diff] [blame] | 103 | this.altIsMeta = terminal.prefs_.get('alt-is-meta'); |
rginda | feaf314 | 2012-01-31 15:14:20 -0800 | [diff] [blame] | 104 | }; |
| 105 | |
| 106 | /** |
rginda | feaf314 | 2012-01-31 15:14:20 -0800 | [diff] [blame] | 107 | * Special handling for keyCodes in a keyboard layout. |
| 108 | */ |
| 109 | hterm.Keyboard.KeyActions = { |
| 110 | /** |
| 111 | * Call preventDefault and stopPropagation for this key event and nothing |
| 112 | * else. |
| 113 | */ |
| 114 | CANCEL: new String('CANCEL'), |
| 115 | |
| 116 | /** |
| 117 | * This performs the default terminal action for the key. If used in the |
| 118 | * 'normal' action and the the keystroke represents a printable key, the |
| 119 | * character will be sent to the host. If used in one of the modifier |
| 120 | * actions, the terminal will perform the normal action after (possibly) |
| 121 | * altering it. |
| 122 | * |
| 123 | * - If the normal sequence starts with CSI, the sequence will be adjusted |
| 124 | * to include the modifier parameter as described in [XTERM] in the final |
| 125 | * table of the "PC-Style Function Keys" section. |
| 126 | * |
| 127 | * - If the control key is down and the key represents a printable character, |
| 128 | * and the uppercase version of the unshifted keycap is between |
| 129 | * 64 (ASCII '@') and 95 (ASCII '_'), then the uppercase version of the |
| 130 | * unshifted keycap minus 64 is sent. This makes '^@' send '\x00' and |
| 131 | * '^_' send '\x1f'. (Note that one higher that 0x1f is 0x20, which is |
| 132 | * the first printable ASCII value.) |
| 133 | * |
| 134 | * - If the alt key is down and the key represents a printable character then |
| 135 | * the value of the character is shifted up by 128. |
| 136 | * |
| 137 | * - If meta is down and configured to send an escape, '\x1b' will be sent |
| 138 | * before the normal action is performed. |
| 139 | */ |
| 140 | DEFAULT: new String('DEFAULT'), |
| 141 | |
| 142 | /** |
| 143 | * Causes the terminal to opt out of handling the key event, instead letting |
| 144 | * the browser deal with it. |
| 145 | */ |
| 146 | PASS: new String('PASS'), |
| 147 | |
| 148 | /** |
| 149 | * Insert the first or second character of the keyCap, based on e.shiftKey. |
| 150 | * The key will be handled in onKeyDown, and e.preventDefault() will be |
| 151 | * called. |
| 152 | * |
| 153 | * It is useful for a modified key action, where it essentially strips the |
| 154 | * modifier while preventing the browser from reacting to the key. |
| 155 | */ |
| 156 | STRIP: new String('STRIP') |
| 157 | }; |
| 158 | |
| 159 | /** |
| 160 | * Capture keyboard events sent to the associated element. |
| 161 | * |
| 162 | * This enables the keyboard. Captured events are consumed by this class |
| 163 | * and will not perform their default action or bubble to other elements. |
| 164 | * |
| 165 | * Passing a null element will uninstall the keyboard handlers. |
| 166 | * |
| 167 | * @param {HTMLElement} element The element whose events should be captured, or |
| 168 | * null to disable the keyboard. |
| 169 | */ |
| 170 | hterm.Keyboard.prototype.installKeyboard = function(element) { |
| 171 | if (element == this.keyboardElement_) |
| 172 | return; |
| 173 | |
| 174 | if (element && this.keyboardElement_) |
| 175 | this.installKeyboard(null); |
| 176 | |
| 177 | for (var i = 0; i < this.handlers_.length; i++) { |
| 178 | var handler = this.handlers_[i]; |
| 179 | if (element) { |
| 180 | element.addEventListener(handler[0], handler[1]); |
| 181 | } else { |
| 182 | this.keyboardElement_.removeEventListener(handler[0], handler[1]); |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | this.keyboardElement_ = element; |
| 187 | }; |
| 188 | |
| 189 | /** |
| 190 | * Disable keyboard event capture. |
| 191 | * |
| 192 | * This will allow the browser to process key events normally. |
| 193 | */ |
| 194 | hterm.Keyboard.prototype.uninstallKeyboard = function() { |
| 195 | this.installKeyboard(null); |
| 196 | }; |
| 197 | |
| 198 | /** |
Robert Ginda | 11390c5 | 2012-09-13 14:53:34 -0700 | [diff] [blame] | 199 | * Handle onTextInput events. |
| 200 | * |
| 201 | * We're not actually supposed to get these, but we do on the Mac in the case |
| 202 | * where a third party app sends synthetic keystrokes to Chrome. |
| 203 | */ |
| 204 | hterm.Keyboard.prototype.onTextInput_ = function(e) { |
| 205 | if (!e.data) |
| 206 | return; |
| 207 | |
| 208 | e.data.split('').forEach(this.terminal.onVTKeystroke.bind(this.terminal)); |
| 209 | }; |
| 210 | |
| 211 | /** |
rginda | feaf314 | 2012-01-31 15:14:20 -0800 | [diff] [blame] | 212 | * Handle onKeyPress events. |
| 213 | */ |
| 214 | hterm.Keyboard.prototype.onKeyPress_ = function(e) { |
rginda | 39bdf6f | 2012-04-10 16:50:55 -0700 | [diff] [blame] | 215 | var code; |
| 216 | |
| 217 | if (e.altKey && this.altSendsWhat == 'browser-key' && e.charCode == 0) { |
| 218 | // If we got here because we were expecting the browser to handle an |
| 219 | // alt sequence but it didn't do it, then we might be on an OS without |
| 220 | // an enabled IME system. In that case we fall back to xterm-like |
| 221 | // behavior. |
| 222 | // |
| 223 | // This happens here only as a fallback. Typically these platforms should |
| 224 | // set altSendsWhat to either 'escape' or '8-bit'. |
| 225 | var ch = String.fromCharCode(e.keyCode); |
| 226 | if (!e.shiftKey) |
| 227 | ch = ch.toLowerCase(); |
| 228 | code = ch.charCodeAt(0) + 128; |
| 229 | |
| 230 | } else if (e.charCode >= 32) { |
| 231 | ch = e.charCode; |
| 232 | } |
| 233 | |
| 234 | if (ch) { |
| 235 | var str = this.terminal.vt.encodeUTF8(String.fromCharCode(ch)); |
| 236 | this.terminal.onVTKeystroke(str); |
| 237 | } |
rginda | feaf314 | 2012-01-31 15:14:20 -0800 | [diff] [blame] | 238 | |
| 239 | e.preventDefault(); |
| 240 | e.stopPropagation(); |
| 241 | }; |
| 242 | |
| 243 | /** |
| 244 | * Handle onKeyDown events. |
| 245 | */ |
| 246 | hterm.Keyboard.prototype.onKeyDown_ = function(e) { |
| 247 | var keyDef = this.keyMap.keyDefs[e.keyCode]; |
| 248 | if (!keyDef) { |
| 249 | console.warn('No definition for keyCode: ' + e.keyCode); |
| 250 | return; |
| 251 | } |
| 252 | |
Robert Ginda | 21de376 | 2012-09-20 16:38:18 -0700 | [diff] [blame^] | 253 | // The type of action we're going to use. |
| 254 | var resolvedActionType = null; |
| 255 | |
rginda | feaf314 | 2012-01-31 15:14:20 -0800 | [diff] [blame] | 256 | var self = this; |
| 257 | function getAction(name) { |
| 258 | // Get the key action for the given action name. If the action is a |
| 259 | // function, dispatch it. If the action defers to the normal action, |
| 260 | // resolve that instead. |
| 261 | |
Robert Ginda | 21de376 | 2012-09-20 16:38:18 -0700 | [diff] [blame^] | 262 | resolvedActionType = name; |
| 263 | |
rginda | feaf314 | 2012-01-31 15:14:20 -0800 | [diff] [blame] | 264 | var action = keyDef[name]; |
| 265 | if (typeof action == 'function') |
| 266 | action = action.apply(self.keyMap, [e, keyDef]); |
| 267 | |
| 268 | if (action === DEFAULT && name != 'normal') |
| 269 | action = getAction('normal'); |
| 270 | |
| 271 | return action; |
| 272 | } |
| 273 | |
| 274 | // Note that we use the triple-equals ('===') operator to test equality for |
| 275 | // these constants, in order to distingush usage of the constant from usage |
| 276 | // of a literal string that happens to contain the same bytes. |
| 277 | var CANCEL = hterm.Keyboard.KeyActions.CANCEL; |
| 278 | var DEFAULT = hterm.Keyboard.KeyActions.DEFAULT; |
| 279 | var PASS = hterm.Keyboard.KeyActions.PASS; |
| 280 | var STRIP = hterm.Keyboard.KeyActions.STRIP; |
| 281 | |
| 282 | var shift = e.shiftKey; |
| 283 | var control = e.ctrlKey; |
rginda | 42ad71d | 2012-02-16 14:06:28 -0800 | [diff] [blame] | 284 | var alt = this.altIsMeta ? false : e.altKey; |
| 285 | var meta = this.altIsMeta ? (e.altKey || e.metaKey) : e.metaKey; |
rginda | feaf314 | 2012-01-31 15:14:20 -0800 | [diff] [blame] | 286 | |
| 287 | var action; |
| 288 | |
| 289 | if (control) { |
| 290 | action = getAction('control'); |
| 291 | } else if (alt) { |
| 292 | action = getAction('alt'); |
rginda | 42ad71d | 2012-02-16 14:06:28 -0800 | [diff] [blame] | 293 | } else if (meta) { |
| 294 | action = getAction('meta'); |
rginda | feaf314 | 2012-01-31 15:14:20 -0800 | [diff] [blame] | 295 | } else { |
| 296 | action = getAction('normal'); |
| 297 | } |
| 298 | |
rginda | 39bdf6f | 2012-04-10 16:50:55 -0700 | [diff] [blame] | 299 | if (alt && this.altSendsWhat == 'browser-key' && action == DEFAULT) { |
| 300 | // When altSendsWhat is 'browser-key', we wait for the keypress event. |
| 301 | // In keypress, the browser should have set the event.charCode to the |
| 302 | // appropriate character. |
| 303 | // TODO(rginda): Character compositions will need some black magic. |
| 304 | action = PASS; |
| 305 | } |
| 306 | |
rginda | feaf314 | 2012-01-31 15:14:20 -0800 | [diff] [blame] | 307 | if (action === PASS || (action === DEFAULT && !(control || alt || meta))) { |
| 308 | // If this key is supposed to be handled by the browser, or it is an |
| 309 | // unmodified key with the default action, then exit this event handler. |
| 310 | // If it's an unmodified key, it'll be handled in onKeyPress where we |
| 311 | // can tell for sure which ASCII code to insert. |
| 312 | // |
| 313 | // This block needs to come before the STRIP test, otherwise we'll strip |
| 314 | // the modifier and think it's ok to let the browser handle the keypress. |
| 315 | // The browser won't know we're trying to ignore the modifiers and might |
| 316 | // perform some default action. |
| 317 | return; |
| 318 | } |
| 319 | |
| 320 | if (action === STRIP) { |
| 321 | alt = control = false; |
| 322 | action = keyDef.normal; |
| 323 | if (typeof action == 'function') |
| 324 | action = action.apply(this.keyMap, [e, keyDef]); |
| 325 | |
| 326 | if (action == DEFAULT && keyDef.keyCap.length == 2) |
| 327 | action = keyDef.keyCap.substr((e.shiftKey ? 1 : 0), 1); |
| 328 | } |
| 329 | |
| 330 | e.preventDefault(); |
| 331 | e.stopPropagation(); |
| 332 | |
| 333 | if (action === CANCEL) |
| 334 | return; |
| 335 | |
rginda | 42ad71d | 2012-02-16 14:06:28 -0800 | [diff] [blame] | 336 | if (action !== DEFAULT && typeof action != 'string') { |
| 337 | console.warn('Invalid action: ' + JSON.stringify(action)); |
| 338 | return; |
| 339 | } |
| 340 | |
Robert Ginda | 21de376 | 2012-09-20 16:38:18 -0700 | [diff] [blame^] | 341 | if (resolvedActionType == 'normal' && action.substr(0, 2) == '\x1b[' && |
| 342 | (alt || control || shift)) { |
| 343 | // The action is an escape sequence that came from the "normal" definition, |
| 344 | // and it was triggered in the presence of a keyboard modifier, we may |
| 345 | // need to alter the action to include the modifier before sending it. |
rginda | 42ad71d | 2012-02-16 14:06:28 -0800 | [diff] [blame] | 346 | |
rginda | feaf314 | 2012-01-31 15:14:20 -0800 | [diff] [blame] | 347 | var mod; |
| 348 | |
| 349 | if (shift && !(alt || control)) { |
| 350 | mod = ';2'; |
| 351 | } else if (alt && !(shift || control)) { |
| 352 | mod = ';3'; |
| 353 | } else if (shift && alt && !control) { |
| 354 | mod = ';4'; |
| 355 | } else if (control && !(shift || alt)) { |
| 356 | mod = ';5'; |
| 357 | } else if (shift && control && !alt) { |
| 358 | mod = ';6'; |
| 359 | } else if (alt && control && !shift) { |
| 360 | mod = ';7'; |
| 361 | } else if (shift && alt && control) { |
| 362 | mod = ';8'; |
| 363 | } |
| 364 | |
| 365 | if (action.length == 3) { |
| 366 | // Some of the CSI sequences have zero parameters unless modified. |
| 367 | action = '\x1b[1' + mod + action.substr(2, 1); |
| 368 | } else { |
| 369 | // Others always have at least one parameter. |
Robert Ginda | 21de376 | 2012-09-20 16:38:18 -0700 | [diff] [blame^] | 370 | action = action.substr(0, action.length - 1) + mod + |
rginda | feaf314 | 2012-01-31 15:14:20 -0800 | [diff] [blame] | 371 | action.substr(action.length - 1); |
| 372 | } |
Robert Ginda | 21de376 | 2012-09-20 16:38:18 -0700 | [diff] [blame^] | 373 | |
| 374 | } else { |
| 375 | // Just send it as-is. |
| 376 | |
| 377 | if (action === DEFAULT) { |
| 378 | if (control) { |
| 379 | var unshifted = keyDef.keyCap.substr(0, 1); |
| 380 | var code = unshifted.charCodeAt(0); |
| 381 | if (code >= 64 && code <= 95) { |
| 382 | action = String.fromCharCode(code - 64); |
| 383 | } |
| 384 | } else if (alt && this.altSendsWhat == '8-bit') { |
| 385 | var ch = keyDef.keyCap.substr((e.shiftKey ? 1 : 0), 1); |
| 386 | var code = ch.charCodeAt(0) + 128; |
| 387 | action = this.terminal.vt.encodeUTF8(String.fromCharCode(code)); |
| 388 | } else { |
| 389 | action = keyDef.keyCap.substr((e.shiftKey ? 1 : 0), 1); |
| 390 | } |
| 391 | } |
| 392 | |
| 393 | // We respect alt/metaSendsEscape even if the keymap action was a literal |
| 394 | // string. Otherwise, every overridden alt/meta action would have to |
| 395 | // check alt/metaSendsEscape. |
| 396 | if (resolvedActionType == 'normal' && |
| 397 | ((alt && this.altSendsWhat == 'escape') || |
| 398 | (meta && this.metaSendsEscape))) { |
| 399 | action = '\x1b' + action; |
| 400 | } |
rginda | feaf314 | 2012-01-31 15:14:20 -0800 | [diff] [blame] | 401 | } |
| 402 | |
| 403 | this.terminal.onVTKeystroke(action); |
| 404 | }; |