Source: main/webapp/modules/Keyboard.js

  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing,
  13. * software distributed under the License is distributed on an
  14. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. * KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations
  17. * under the License.
  18. */
  19. var Guacamole = Guacamole || {};
  20. /**
  21. * Provides cross-browser and cross-keyboard keyboard for a specific element.
  22. * Browser and keyboard layout variation is abstracted away, providing events
  23. * which represent keys as their corresponding X11 keysym.
  24. *
  25. * @constructor
  26. * @param {Element|Document} [element]
  27. * The Element to use to provide keyboard events. If omitted, at least one
  28. * Element must be manually provided through the listenTo() function for
  29. * the Guacamole.Keyboard instance to have any effect.
  30. */
  31. Guacamole.Keyboard = function Keyboard(element) {
  32. /**
  33. * Reference to this Guacamole.Keyboard.
  34. *
  35. * @private
  36. * @type {!Guacamole.Keyboard}
  37. */
  38. var guac_keyboard = this;
  39. /**
  40. * An integer value which uniquely identifies this Guacamole.Keyboard
  41. * instance with respect to other Guacamole.Keyboard instances.
  42. *
  43. * @private
  44. * @type {!number}
  45. */
  46. var guacKeyboardID = Guacamole.Keyboard._nextID++;
  47. /**
  48. * The name of the property which is added to event objects via markEvent()
  49. * to note that they have already been handled by this Guacamole.Keyboard.
  50. *
  51. * @private
  52. * @constant
  53. * @type {!string}
  54. */
  55. var EVENT_MARKER = '_GUAC_KEYBOARD_HANDLED_BY_' + guacKeyboardID;
  56. /**
  57. * Fired whenever the user presses a key with the element associated
  58. * with this Guacamole.Keyboard in focus.
  59. *
  60. * @event
  61. * @param {!number} keysym
  62. * The keysym of the key being pressed.
  63. *
  64. * @return {!boolean}
  65. * true if the key event should be allowed through to the browser,
  66. * false otherwise.
  67. */
  68. this.onkeydown = null;
  69. /**
  70. * Fired whenever the user releases a key with the element associated
  71. * with this Guacamole.Keyboard in focus.
  72. *
  73. * @event
  74. * @param {!number} keysym
  75. * The keysym of the key being released.
  76. */
  77. this.onkeyup = null;
  78. /**
  79. * Set of known platform-specific or browser-specific quirks which must be
  80. * accounted for to properly interpret key events, even if the only way to
  81. * reliably detect that quirk is to platform/browser-sniff.
  82. *
  83. * @private
  84. * @type {!Object.<string, boolean>}
  85. */
  86. var quirks = {
  87. /**
  88. * Whether keyup events are universally unreliable.
  89. *
  90. * @type {!boolean}
  91. */
  92. keyupUnreliable: false,
  93. /**
  94. * Whether the Alt key is actually a modifier for typable keys and is
  95. * thus never used for keyboard shortcuts.
  96. *
  97. * @type {!boolean}
  98. */
  99. altIsTypableOnly: false,
  100. /**
  101. * Whether we can rely on receiving a keyup event for the Caps Lock
  102. * key.
  103. *
  104. * @type {!boolean}
  105. */
  106. capsLockKeyupUnreliable: false
  107. };
  108. // Set quirk flags depending on platform/browser, if such information is
  109. // available
  110. if (navigator && navigator.platform) {
  111. // All keyup events are unreliable on iOS (sadly)
  112. if (navigator.platform.match(/ipad|iphone|ipod/i))
  113. quirks.keyupUnreliable = true;
  114. // The Alt key on Mac is never used for keyboard shortcuts, and the
  115. // Caps Lock key never dispatches keyup events
  116. else if (navigator.platform.match(/^mac/i)) {
  117. quirks.altIsTypableOnly = true;
  118. quirks.capsLockKeyupUnreliable = true;
  119. }
  120. }
  121. /**
  122. * A key event having a corresponding timestamp. This event is non-specific.
  123. * Its subclasses should be used instead when recording specific key
  124. * events.
  125. *
  126. * @private
  127. * @constructor
  128. * @param {KeyboardEvent} [orig]
  129. * The relevant DOM keyboard event.
  130. */
  131. var KeyEvent = function KeyEvent(orig) {
  132. /**
  133. * Reference to this key event.
  134. *
  135. * @private
  136. * @type {!KeyEvent}
  137. */
  138. var key_event = this;
  139. /**
  140. * The JavaScript key code of the key pressed. For most events (keydown
  141. * and keyup), this is a scancode-like value related to the position of
  142. * the key on the US English "Qwerty" keyboard. For keypress events,
  143. * this is the Unicode codepoint of the character that would be typed
  144. * by the key pressed.
  145. *
  146. * @type {!number}
  147. */
  148. this.keyCode = orig ? (orig.which || orig.keyCode) : 0;
  149. /**
  150. * The legacy DOM3 "keyIdentifier" of the key pressed, as defined at:
  151. * http://www.w3.org/TR/2009/WD-DOM-Level-3-Events-20090908/#events-Events-KeyboardEvent
  152. *
  153. * @type {!string}
  154. */
  155. this.keyIdentifier = orig && orig.keyIdentifier;
  156. /**
  157. * The standard name of the key pressed, as defined at:
  158. * http://www.w3.org/TR/DOM-Level-3-Events/#events-KeyboardEvent
  159. *
  160. * @type {!string}
  161. */
  162. this.key = orig && orig.key;
  163. /**
  164. * The location on the keyboard corresponding to the key pressed, as
  165. * defined at:
  166. * http://www.w3.org/TR/DOM-Level-3-Events/#events-KeyboardEvent
  167. *
  168. * @type {!number}
  169. */
  170. this.location = orig ? getEventLocation(orig) : 0;
  171. /**
  172. * The state of all local keyboard modifiers at the time this event was
  173. * received.
  174. *
  175. * @type {!Guacamole.Keyboard.ModifierState}
  176. */
  177. this.modifiers = orig ? Guacamole.Keyboard.ModifierState.fromKeyboardEvent(orig) : new Guacamole.Keyboard.ModifierState();
  178. /**
  179. * An arbitrary timestamp in milliseconds, indicating this event's
  180. * position in time relative to other events.
  181. *
  182. * @type {!number}
  183. */
  184. this.timestamp = new Date().getTime();
  185. /**
  186. * Whether the default action of this key event should be prevented.
  187. *
  188. * @type {!boolean}
  189. */
  190. this.defaultPrevented = false;
  191. /**
  192. * The keysym of the key associated with this key event, as determined
  193. * by a best-effort guess using available event properties and keyboard
  194. * state.
  195. *
  196. * @type {number}
  197. */
  198. this.keysym = null;
  199. /**
  200. * Whether the keysym value of this key event is known to be reliable.
  201. * If false, the keysym may still be valid, but it's only a best guess,
  202. * and future key events may be a better source of information.
  203. *
  204. * @type {!boolean}
  205. */
  206. this.reliable = false;
  207. /**
  208. * Returns the number of milliseconds elapsed since this event was
  209. * received.
  210. *
  211. * @return {!number}
  212. * The number of milliseconds elapsed since this event was
  213. * received.
  214. */
  215. this.getAge = function() {
  216. return new Date().getTime() - key_event.timestamp;
  217. };
  218. };
  219. /**
  220. * Information related to the pressing of a key, which need not be a key
  221. * associated with a printable character. The presence or absence of any
  222. * information within this object is browser-dependent.
  223. *
  224. * @private
  225. * @constructor
  226. * @augments Guacamole.Keyboard.KeyEvent
  227. * @param {!KeyboardEvent} orig
  228. * The relevant DOM "keydown" event.
  229. */
  230. var KeydownEvent = function KeydownEvent(orig) {
  231. // We extend KeyEvent
  232. KeyEvent.call(this, orig);
  233. // If key is known from keyCode or DOM3 alone, use that
  234. this.keysym = keysym_from_key_identifier(this.key, this.location)
  235. || keysym_from_keycode(this.keyCode, this.location);
  236. /**
  237. * Whether the keyup following this keydown event is known to be
  238. * reliable. If false, we cannot rely on the keyup event to occur.
  239. *
  240. * @type {!boolean}
  241. */
  242. this.keyupReliable = !quirks.keyupUnreliable;
  243. // DOM3 and keyCode are reliable sources if the corresponding key is
  244. // not a printable key
  245. if (this.keysym && !isPrintable(this.keysym))
  246. this.reliable = true;
  247. // Use legacy keyIdentifier as a last resort, if it looks sane
  248. if (!this.keysym && key_identifier_sane(this.keyCode, this.keyIdentifier))
  249. this.keysym = keysym_from_key_identifier(this.keyIdentifier, this.location, this.modifiers.shift);
  250. // If a key is pressed while meta is held down, the keyup will
  251. // never be sent in Chrome (bug #108404)
  252. if (this.modifiers.meta && this.keysym !== 0xFFE7 && this.keysym !== 0xFFE8)
  253. this.keyupReliable = false;
  254. // We cannot rely on receiving keyup for Caps Lock on certain platforms
  255. else if (this.keysym === 0xFFE5 && quirks.capsLockKeyupUnreliable)
  256. this.keyupReliable = false;
  257. // Determine whether default action for Alt+combinations must be prevented
  258. var prevent_alt = !this.modifiers.ctrl && !quirks.altIsTypableOnly;
  259. // Determine whether default action for Ctrl+combinations must be prevented
  260. var prevent_ctrl = !this.modifiers.alt;
  261. // We must rely on the (potentially buggy) keyIdentifier if preventing
  262. // the default action is important
  263. if ((prevent_ctrl && this.modifiers.ctrl)
  264. || (prevent_alt && this.modifiers.alt)
  265. || this.modifiers.meta
  266. || this.modifiers.hyper)
  267. this.reliable = true;
  268. // Record most recently known keysym by associated key code
  269. recentKeysym[this.keyCode] = this.keysym;
  270. };
  271. KeydownEvent.prototype = new KeyEvent();
  272. /**
  273. * Information related to the pressing of a key, which MUST be
  274. * associated with a printable character. The presence or absence of any
  275. * information within this object is browser-dependent.
  276. *
  277. * @private
  278. * @constructor
  279. * @augments Guacamole.Keyboard.KeyEvent
  280. * @param {!KeyboardEvent} orig
  281. * The relevant DOM "keypress" event.
  282. */
  283. var KeypressEvent = function KeypressEvent(orig) {
  284. // We extend KeyEvent
  285. KeyEvent.call(this, orig);
  286. // Pull keysym from char code
  287. this.keysym = keysym_from_charcode(this.keyCode);
  288. // Keypress is always reliable
  289. this.reliable = true;
  290. };
  291. KeypressEvent.prototype = new KeyEvent();
  292. /**
  293. * Information related to the releasing of a key, which need not be a key
  294. * associated with a printable character. The presence or absence of any
  295. * information within this object is browser-dependent.
  296. *
  297. * @private
  298. * @constructor
  299. * @augments Guacamole.Keyboard.KeyEvent
  300. * @param {!KeyboardEvent} orig
  301. * The relevant DOM "keyup" event.
  302. */
  303. var KeyupEvent = function KeyupEvent(orig) {
  304. // We extend KeyEvent
  305. KeyEvent.call(this, orig);
  306. // If key is known from keyCode or DOM3 alone, use that (keyCode is
  307. // still more reliable for keyup when dead keys are in use)
  308. this.keysym = keysym_from_keycode(this.keyCode, this.location)
  309. || keysym_from_key_identifier(this.key, this.location);
  310. // Fall back to the most recently pressed keysym associated with the
  311. // keyCode if the inferred key doesn't seem to actually be pressed
  312. if (!guac_keyboard.pressed[this.keysym])
  313. this.keysym = recentKeysym[this.keyCode] || this.keysym;
  314. // Keyup is as reliable as it will ever be
  315. this.reliable = true;
  316. };
  317. KeyupEvent.prototype = new KeyEvent();
  318. /**
  319. * An array of recorded events, which can be instances of the private
  320. * KeydownEvent, KeypressEvent, and KeyupEvent classes.
  321. *
  322. * @private
  323. * @type {!KeyEvent[]}
  324. */
  325. var eventLog = [];
  326. /**
  327. * Map of known JavaScript keycodes which do not map to typable characters
  328. * to their X11 keysym equivalents.
  329. *
  330. * @private
  331. * @type {!Object.<number, number[]>}
  332. */
  333. var keycodeKeysyms = {
  334. 8: [0xFF08], // backspace
  335. 9: [0xFF09], // tab
  336. 12: [0xFF0B, 0xFF0B, 0xFF0B, 0xFFB5], // clear / KP 5
  337. 13: [0xFF0D], // enter
  338. 16: [0xFFE1, 0xFFE1, 0xFFE2], // shift
  339. 17: [0xFFE3, 0xFFE3, 0xFFE4], // ctrl
  340. 18: [0xFFE9, 0xFFE9, 0xFE03], // alt
  341. 19: [0xFF13], // pause/break
  342. 20: [0xFFE5], // caps lock
  343. 27: [0xFF1B], // escape
  344. 32: [0x0020], // space
  345. 33: [0xFF55, 0xFF55, 0xFF55, 0xFFB9], // page up / KP 9
  346. 34: [0xFF56, 0xFF56, 0xFF56, 0xFFB3], // page down / KP 3
  347. 35: [0xFF57, 0xFF57, 0xFF57, 0xFFB1], // end / KP 1
  348. 36: [0xFF50, 0xFF50, 0xFF50, 0xFFB7], // home / KP 7
  349. 37: [0xFF51, 0xFF51, 0xFF51, 0xFFB4], // left arrow / KP 4
  350. 38: [0xFF52, 0xFF52, 0xFF52, 0xFFB8], // up arrow / KP 8
  351. 39: [0xFF53, 0xFF53, 0xFF53, 0xFFB6], // right arrow / KP 6
  352. 40: [0xFF54, 0xFF54, 0xFF54, 0xFFB2], // down arrow / KP 2
  353. 45: [0xFF63, 0xFF63, 0xFF63, 0xFFB0], // insert / KP 0
  354. 46: [0xFFFF, 0xFFFF, 0xFFFF, 0xFFAE], // delete / KP decimal
  355. 91: [0xFFE7], // left windows/command key (meta_l)
  356. 92: [0xFFE8], // right window/command key (meta_r)
  357. 93: [0xFF67], // menu key
  358. 96: [0xFFB0], // KP 0
  359. 97: [0xFFB1], // KP 1
  360. 98: [0xFFB2], // KP 2
  361. 99: [0xFFB3], // KP 3
  362. 100: [0xFFB4], // KP 4
  363. 101: [0xFFB5], // KP 5
  364. 102: [0xFFB6], // KP 6
  365. 103: [0xFFB7], // KP 7
  366. 104: [0xFFB8], // KP 8
  367. 105: [0xFFB9], // KP 9
  368. 106: [0xFFAA], // KP multiply
  369. 107: [0xFFAB], // KP add
  370. 109: [0xFFAD], // KP subtract
  371. 110: [0xFFAE], // KP decimal
  372. 111: [0xFFAF], // KP divide
  373. 112: [0xFFBE], // f1
  374. 113: [0xFFBF], // f2
  375. 114: [0xFFC0], // f3
  376. 115: [0xFFC1], // f4
  377. 116: [0xFFC2], // f5
  378. 117: [0xFFC3], // f6
  379. 118: [0xFFC4], // f7
  380. 119: [0xFFC5], // f8
  381. 120: [0xFFC6], // f9
  382. 121: [0xFFC7], // f10
  383. 122: [0xFFC8], // f11
  384. 123: [0xFFC9], // f12
  385. 144: [0xFF7F], // num lock
  386. 145: [0xFF14], // scroll lock
  387. 225: [0xFE03] // altgraph (iso_level3_shift)
  388. };
  389. /**
  390. * Map of known JavaScript keyidentifiers which do not map to typable
  391. * characters to their unshifted X11 keysym equivalents.
  392. *
  393. * @private
  394. * @type {!Object.<string, number[]>}
  395. */
  396. var keyidentifier_keysym = {
  397. "Again": [0xFF66],
  398. "AllCandidates": [0xFF3D],
  399. "Alphanumeric": [0xFF30],
  400. "Alt": [0xFFE9, 0xFFE9, 0xFE03],
  401. "Attn": [0xFD0E],
  402. "AltGraph": [0xFE03],
  403. "ArrowDown": [0xFF54],
  404. "ArrowLeft": [0xFF51],
  405. "ArrowRight": [0xFF53],
  406. "ArrowUp": [0xFF52],
  407. "Backspace": [0xFF08],
  408. "CapsLock": [0xFFE5],
  409. "Cancel": [0xFF69],
  410. "Clear": [0xFF0B],
  411. "Convert": [0xFF21],
  412. "Copy": [0xFD15],
  413. "Crsel": [0xFD1C],
  414. "CrSel": [0xFD1C],
  415. "CodeInput": [0xFF37],
  416. "Compose": [0xFF20],
  417. "Control": [0xFFE3, 0xFFE3, 0xFFE4],
  418. "ContextMenu": [0xFF67],
  419. "Delete": [0xFFFF],
  420. "Down": [0xFF54],
  421. "End": [0xFF57],
  422. "Enter": [0xFF0D],
  423. "EraseEof": [0xFD06],
  424. "Escape": [0xFF1B],
  425. "Execute": [0xFF62],
  426. "Exsel": [0xFD1D],
  427. "ExSel": [0xFD1D],
  428. "F1": [0xFFBE],
  429. "F2": [0xFFBF],
  430. "F3": [0xFFC0],
  431. "F4": [0xFFC1],
  432. "F5": [0xFFC2],
  433. "F6": [0xFFC3],
  434. "F7": [0xFFC4],
  435. "F8": [0xFFC5],
  436. "F9": [0xFFC6],
  437. "F10": [0xFFC7],
  438. "F11": [0xFFC8],
  439. "F12": [0xFFC9],
  440. "F13": [0xFFCA],
  441. "F14": [0xFFCB],
  442. "F15": [0xFFCC],
  443. "F16": [0xFFCD],
  444. "F17": [0xFFCE],
  445. "F18": [0xFFCF],
  446. "F19": [0xFFD0],
  447. "F20": [0xFFD1],
  448. "F21": [0xFFD2],
  449. "F22": [0xFFD3],
  450. "F23": [0xFFD4],
  451. "F24": [0xFFD5],
  452. "Find": [0xFF68],
  453. "GroupFirst": [0xFE0C],
  454. "GroupLast": [0xFE0E],
  455. "GroupNext": [0xFE08],
  456. "GroupPrevious": [0xFE0A],
  457. "FullWidth": null,
  458. "HalfWidth": null,
  459. "HangulMode": [0xFF31],
  460. "Hankaku": [0xFF29],
  461. "HanjaMode": [0xFF34],
  462. "Help": [0xFF6A],
  463. "Hiragana": [0xFF25],
  464. "HiraganaKatakana": [0xFF27],
  465. "Home": [0xFF50],
  466. "Hyper": [0xFFED, 0xFFED, 0xFFEE],
  467. "Insert": [0xFF63],
  468. "JapaneseHiragana": [0xFF25],
  469. "JapaneseKatakana": [0xFF26],
  470. "JapaneseRomaji": [0xFF24],
  471. "JunjaMode": [0xFF38],
  472. "KanaMode": [0xFF2D],
  473. "KanjiMode": [0xFF21],
  474. "Katakana": [0xFF26],
  475. "Left": [0xFF51],
  476. "Meta": [0xFFE7, 0xFFE7, 0xFFE8],
  477. "ModeChange": [0xFF7E],
  478. "NumLock": [0xFF7F],
  479. "PageDown": [0xFF56],
  480. "PageUp": [0xFF55],
  481. "Pause": [0xFF13],
  482. "Play": [0xFD16],
  483. "PreviousCandidate": [0xFF3E],
  484. "PrintScreen": [0xFF61],
  485. "Redo": [0xFF66],
  486. "Right": [0xFF53],
  487. "RomanCharacters": null,
  488. "Scroll": [0xFF14],
  489. "Select": [0xFF60],
  490. "Separator": [0xFFAC],
  491. "Shift": [0xFFE1, 0xFFE1, 0xFFE2],
  492. "SingleCandidate": [0xFF3C],
  493. "Super": [0xFFEB, 0xFFEB, 0xFFEC],
  494. "Tab": [0xFF09],
  495. "UIKeyInputDownArrow": [0xFF54],
  496. "UIKeyInputEscape": [0xFF1B],
  497. "UIKeyInputLeftArrow": [0xFF51],
  498. "UIKeyInputRightArrow": [0xFF53],
  499. "UIKeyInputUpArrow": [0xFF52],
  500. "Up": [0xFF52],
  501. "Undo": [0xFF65],
  502. "Win": [0xFFE7, 0xFFE7, 0xFFE8],
  503. "Zenkaku": [0xFF28],
  504. "ZenkakuHankaku": [0xFF2A]
  505. };
  506. /**
  507. * All keysyms which should not repeat when held down.
  508. *
  509. * @private
  510. * @type {!Object.<number, boolean>}
  511. */
  512. var no_repeat = {
  513. 0xFE03: true, // ISO Level 3 Shift (AltGr)
  514. 0xFFE1: true, // Left shift
  515. 0xFFE2: true, // Right shift
  516. 0xFFE3: true, // Left ctrl
  517. 0xFFE4: true, // Right ctrl
  518. 0xFFE5: true, // Caps Lock
  519. 0xFFE7: true, // Left meta
  520. 0xFFE8: true, // Right meta
  521. 0xFFE9: true, // Left alt
  522. 0xFFEA: true, // Right alt
  523. 0xFFEB: true, // Left super/hyper
  524. 0xFFEC: true // Right super/hyper
  525. };
  526. /**
  527. * All modifiers and their states.
  528. *
  529. * @type {!Guacamole.Keyboard.ModifierState}
  530. */
  531. this.modifiers = new Guacamole.Keyboard.ModifierState();
  532. /**
  533. * The state of every key, indexed by keysym. If a particular key is
  534. * pressed, the value of pressed for that keysym will be true. If a key
  535. * is not currently pressed, it will not be defined.
  536. *
  537. * @type {!Object.<number, boolean>}
  538. */
  539. this.pressed = {};
  540. /**
  541. * The state of every key, indexed by keysym, for strictly those keys whose
  542. * status has been indirectly determined thorugh observation of other key
  543. * events. If a particular key is implicitly pressed, the value of
  544. * implicitlyPressed for that keysym will be true. If a key
  545. * is not currently implicitly pressed (the key is not pressed OR the state
  546. * of the key is explicitly known), it will not be defined.
  547. *
  548. * @private
  549. * @type {!Object.<number, boolean>}
  550. */
  551. var implicitlyPressed = {};
  552. /**
  553. * The last result of calling the onkeydown handler for each key, indexed
  554. * by keysym. This is used to prevent/allow default actions for key events,
  555. * even when the onkeydown handler cannot be called again because the key
  556. * is (theoretically) still pressed.
  557. *
  558. * @private
  559. * @type {!Object.<number, boolean>}
  560. */
  561. var last_keydown_result = {};
  562. /**
  563. * The keysym most recently associated with a given keycode when keydown
  564. * fired. This object maps keycodes to keysyms.
  565. *
  566. * @private
  567. * @type {!Object.<number, number>}
  568. */
  569. var recentKeysym = {};
  570. /**
  571. * Timeout before key repeat starts.
  572. *
  573. * @private
  574. * @type {number}
  575. */
  576. var key_repeat_timeout = null;
  577. /**
  578. * Interval which presses and releases the last key pressed while that
  579. * key is still being held down.
  580. *
  581. * @private
  582. * @type {number}
  583. */
  584. var key_repeat_interval = null;
  585. /**
  586. * Given an array of keysyms indexed by location, returns the keysym
  587. * for the given location, or the keysym for the standard location if
  588. * undefined.
  589. *
  590. * @private
  591. * @param {number[]} keysyms
  592. * An array of keysyms, where the index of the keysym in the array is
  593. * the location value.
  594. *
  595. * @param {!number} location
  596. * The location on the keyboard corresponding to the key pressed, as
  597. * defined at: http://www.w3.org/TR/DOM-Level-3-Events/#events-KeyboardEvent
  598. */
  599. var get_keysym = function get_keysym(keysyms, location) {
  600. if (!keysyms)
  601. return null;
  602. return keysyms[location] || keysyms[0];
  603. };
  604. /**
  605. * Returns true if the given keysym corresponds to a printable character,
  606. * false otherwise.
  607. *
  608. * @param {!number} keysym
  609. * The keysym to check.
  610. *
  611. * @returns {!boolean}
  612. * true if the given keysym corresponds to a printable character,
  613. * false otherwise.
  614. */
  615. var isPrintable = function isPrintable(keysym) {
  616. // Keysyms with Unicode equivalents are printable
  617. return (keysym >= 0x00 && keysym <= 0xFF)
  618. || (keysym & 0xFFFF0000) === 0x01000000;
  619. };
  620. function keysym_from_key_identifier(identifier, location, shifted) {
  621. if (!identifier)
  622. return null;
  623. var typedCharacter;
  624. // If identifier is U+xxxx, decode Unicode character
  625. var unicodePrefixLocation = identifier.indexOf("U+");
  626. if (unicodePrefixLocation >= 0) {
  627. var hex = identifier.substring(unicodePrefixLocation+2);
  628. typedCharacter = String.fromCharCode(parseInt(hex, 16));
  629. }
  630. // If single character and not keypad, use that as typed character
  631. else if (identifier.length === 1 && location !== 3)
  632. typedCharacter = identifier;
  633. // Otherwise, look up corresponding keysym
  634. else
  635. return get_keysym(keyidentifier_keysym[identifier], location);
  636. // Alter case if necessary
  637. if (shifted === true)
  638. typedCharacter = typedCharacter.toUpperCase();
  639. else if (shifted === false)
  640. typedCharacter = typedCharacter.toLowerCase();
  641. // Get codepoint
  642. var codepoint = typedCharacter.charCodeAt(0);
  643. return keysym_from_charcode(codepoint);
  644. }
  645. function isControlCharacter(codepoint) {
  646. return codepoint <= 0x1F || (codepoint >= 0x7F && codepoint <= 0x9F);
  647. }
  648. function keysym_from_charcode(codepoint) {
  649. // Keysyms for control characters
  650. if (isControlCharacter(codepoint)) return 0xFF00 | codepoint;
  651. // Keysyms for ASCII chars
  652. if (codepoint >= 0x0000 && codepoint <= 0x00FF)
  653. return codepoint;
  654. // Keysyms for Unicode
  655. if (codepoint >= 0x0100 && codepoint <= 0x10FFFF)
  656. return 0x01000000 | codepoint;
  657. return null;
  658. }
  659. function keysym_from_keycode(keyCode, location) {
  660. return get_keysym(keycodeKeysyms[keyCode], location);
  661. }
  662. /**
  663. * Heuristically detects if the legacy keyIdentifier property of
  664. * a keydown/keyup event looks incorrectly derived. Chrome, and
  665. * presumably others, will produce the keyIdentifier by assuming
  666. * the keyCode is the Unicode codepoint for that key. This is not
  667. * correct in all cases.
  668. *
  669. * @private
  670. * @param {!number} keyCode
  671. * The keyCode from a browser keydown/keyup event.
  672. *
  673. * @param {string} keyIdentifier
  674. * The legacy keyIdentifier from a browser keydown/keyup event.
  675. *
  676. * @returns {!boolean}
  677. * true if the keyIdentifier looks sane, false if the keyIdentifier
  678. * appears incorrectly derived or is missing entirely.
  679. */
  680. var key_identifier_sane = function key_identifier_sane(keyCode, keyIdentifier) {
  681. // Missing identifier is not sane
  682. if (!keyIdentifier)
  683. return false;
  684. // Assume non-Unicode keyIdentifier values are sane
  685. var unicodePrefixLocation = keyIdentifier.indexOf("U+");
  686. if (unicodePrefixLocation === -1)
  687. return true;
  688. // If the Unicode codepoint isn't identical to the keyCode,
  689. // then the identifier is likely correct
  690. var codepoint = parseInt(keyIdentifier.substring(unicodePrefixLocation+2), 16);
  691. if (keyCode !== codepoint)
  692. return true;
  693. // The keyCodes for A-Z and 0-9 are actually identical to their
  694. // Unicode codepoints
  695. if ((keyCode >= 65 && keyCode <= 90) || (keyCode >= 48 && keyCode <= 57))
  696. return true;
  697. // The keyIdentifier does NOT appear sane
  698. return false;
  699. };
  700. /**
  701. * Marks a key as pressed, firing the keydown event if registered. Key
  702. * repeat for the pressed key will start after a delay if that key is
  703. * not a modifier. The return value of this function depends on the
  704. * return value of the keydown event handler, if any.
  705. *
  706. * @param {number} keysym
  707. * The keysym of the key to press.
  708. *
  709. * @return {boolean}
  710. * true if event should NOT be canceled, false otherwise.
  711. */
  712. this.press = function(keysym) {
  713. // Don't bother with pressing the key if the key is unknown
  714. if (keysym === null) return;
  715. // Only press if released
  716. if (!guac_keyboard.pressed[keysym]) {
  717. // Mark key as pressed
  718. guac_keyboard.pressed[keysym] = true;
  719. // Send key event
  720. if (guac_keyboard.onkeydown) {
  721. var result = guac_keyboard.onkeydown(keysym);
  722. last_keydown_result[keysym] = result;
  723. // Stop any current repeat
  724. window.clearTimeout(key_repeat_timeout);
  725. window.clearInterval(key_repeat_interval);
  726. // Repeat after a delay as long as pressed
  727. if (!no_repeat[keysym])
  728. key_repeat_timeout = window.setTimeout(function() {
  729. key_repeat_interval = window.setInterval(function() {
  730. guac_keyboard.onkeyup(keysym);
  731. guac_keyboard.onkeydown(keysym);
  732. }, 50);
  733. }, 500);
  734. return result;
  735. }
  736. }
  737. // Return the last keydown result by default, resort to false if unknown
  738. return last_keydown_result[keysym] || false;
  739. };
  740. /**
  741. * Marks a key as released, firing the keyup event if registered.
  742. *
  743. * @param {number} keysym
  744. * The keysym of the key to release.
  745. */
  746. this.release = function(keysym) {
  747. // Only release if pressed
  748. if (guac_keyboard.pressed[keysym]) {
  749. // Mark key as released
  750. delete guac_keyboard.pressed[keysym];
  751. delete implicitlyPressed[keysym];
  752. // Stop repeat
  753. window.clearTimeout(key_repeat_timeout);
  754. window.clearInterval(key_repeat_interval);
  755. // Send key event
  756. if (keysym !== null && guac_keyboard.onkeyup)
  757. guac_keyboard.onkeyup(keysym);
  758. }
  759. };
  760. /**
  761. * Presses and releases the keys necessary to type the given string of
  762. * text.
  763. *
  764. * @param {!string} str
  765. * The string to type.
  766. */
  767. this.type = function type(str) {
  768. // Press/release the key corresponding to each character in the string
  769. for (var i = 0; i < str.length; i++) {
  770. // Determine keysym of current character
  771. var codepoint = str.codePointAt ? str.codePointAt(i) : str.charCodeAt(i);
  772. var keysym = keysym_from_charcode(codepoint);
  773. // Press and release key for current character
  774. guac_keyboard.press(keysym);
  775. guac_keyboard.release(keysym);
  776. }
  777. };
  778. /**
  779. * Resets the state of this keyboard, releasing all keys, and firing keyup
  780. * events for each released key.
  781. */
  782. this.reset = function() {
  783. // Release all pressed keys
  784. for (var keysym in guac_keyboard.pressed)
  785. guac_keyboard.release(parseInt(keysym));
  786. // Clear event log
  787. eventLog = [];
  788. };
  789. /**
  790. * Resynchronizes the remote state of the given modifier with its
  791. * corresponding local modifier state, as dictated by
  792. * {@link KeyEvent#modifiers} within the given key event, by pressing or
  793. * releasing keysyms.
  794. *
  795. * @private
  796. * @param {!string} modifier
  797. * The name of the {@link Guacamole.Keyboard.ModifierState} property
  798. * being updated.
  799. *
  800. * @param {!number[]} keysyms
  801. * The keysyms which represent the modifier being updated.
  802. *
  803. * @param {!KeyEvent} keyEvent
  804. * Guacamole's current best interpretation of the key event being
  805. * processed.
  806. */
  807. var updateModifierState = function updateModifierState(modifier,
  808. keysyms, keyEvent) {
  809. var localState = keyEvent.modifiers[modifier];
  810. var remoteState = guac_keyboard.modifiers[modifier];
  811. var i;
  812. // Do not trust changes in modifier state for events directly involving
  813. // that modifier: (1) the flag may erroneously be cleared despite
  814. // another version of the same key still being held and (2) the change
  815. // in flag may be due to the current event being processed, thus
  816. // updating things here is at best redundant and at worst incorrect
  817. if (keysyms.indexOf(keyEvent.keysym) !== -1)
  818. return;
  819. // Release all related keys if modifier is implicitly released
  820. if (remoteState && localState === false) {
  821. for (i = 0; i < keysyms.length; i++) {
  822. guac_keyboard.release(keysyms[i]);
  823. }
  824. }
  825. // Press if modifier is implicitly pressed
  826. else if (!remoteState && localState) {
  827. // Verify that modifier flag isn't already pressed or already set
  828. // due to another version of the same key being held down
  829. for (i = 0; i < keysyms.length; i++) {
  830. if (guac_keyboard.pressed[keysyms[i]])
  831. return;
  832. }
  833. // Mark as implicitly pressed only if there is other information
  834. // within the key event relating to a different key. Some
  835. // platforms, such as iOS, will send essentially empty key events
  836. // for modifier keys, using only the modifier flags to signal the
  837. // identity of the key.
  838. var keysym = keysyms[0];
  839. if (keyEvent.keysym)
  840. implicitlyPressed[keysym] = true;
  841. guac_keyboard.press(keysym);
  842. }
  843. };
  844. /**
  845. * Given a keyboard event, updates the remote key state to match the local
  846. * modifier state and remote based on the modifier flags within the event.
  847. * This function pays no attention to keycodes.
  848. *
  849. * @private
  850. * @param {!KeyEvent} keyEvent
  851. * Guacamole's current best interpretation of the key event being
  852. * processed.
  853. */
  854. var syncModifierStates = function syncModifierStates(keyEvent) {
  855. // Resync state of alt
  856. updateModifierState('alt', [
  857. 0xFFE9, // Left alt
  858. 0xFFEA, // Right alt
  859. 0xFE03 // AltGr
  860. ], keyEvent);
  861. // Resync state of shift
  862. updateModifierState('shift', [
  863. 0xFFE1, // Left shift
  864. 0xFFE2 // Right shift
  865. ], keyEvent);
  866. // Resync state of ctrl
  867. updateModifierState('ctrl', [
  868. 0xFFE3, // Left ctrl
  869. 0xFFE4 // Right ctrl
  870. ], keyEvent);
  871. // Resync state of meta
  872. updateModifierState('meta', [
  873. 0xFFE7, // Left meta
  874. 0xFFE8 // Right meta
  875. ], keyEvent);
  876. // Resync state of hyper
  877. updateModifierState('hyper', [
  878. 0xFFEB, // Left super/hyper
  879. 0xFFEC // Right super/hyper
  880. ], keyEvent);
  881. // Update state
  882. guac_keyboard.modifiers = keyEvent.modifiers;
  883. };
  884. /**
  885. * Returns whether all currently pressed keys were implicitly pressed. A
  886. * key is implicitly pressed if its status was inferred indirectly from
  887. * inspection of other key events.
  888. *
  889. * @private
  890. * @returns {!boolean}
  891. * true if all currently pressed keys were implicitly pressed, false
  892. * otherwise.
  893. */
  894. var isStateImplicit = function isStateImplicit() {
  895. for (var keysym in guac_keyboard.pressed) {
  896. if (!implicitlyPressed[keysym])
  897. return false;
  898. }
  899. return true;
  900. };
  901. /**
  902. * Reads through the event log, removing events from the head of the log
  903. * when the corresponding true key presses are known (or as known as they
  904. * can be).
  905. *
  906. * @private
  907. * @return {boolean}
  908. * Whether the default action of the latest event should be prevented.
  909. */
  910. function interpret_events() {
  911. // Do not prevent default if no event could be interpreted
  912. var handled_event = interpret_event();
  913. if (!handled_event)
  914. return false;
  915. // Interpret as much as possible
  916. var last_event;
  917. do {
  918. last_event = handled_event;
  919. handled_event = interpret_event();
  920. } while (handled_event !== null);
  921. // Reset keyboard state if we cannot expect to receive any further
  922. // keyup events
  923. if (isStateImplicit())
  924. guac_keyboard.reset();
  925. return last_event.defaultPrevented;
  926. }
  927. /**
  928. * Releases Ctrl+Alt, if both are currently pressed and the given keysym
  929. * looks like a key that may require AltGr.
  930. *
  931. * @private
  932. * @param {!number} keysym
  933. * The key that was just pressed.
  934. */
  935. var release_simulated_altgr = function release_simulated_altgr(keysym) {
  936. // Both Ctrl+Alt must be pressed if simulated AltGr is in use
  937. if (!guac_keyboard.modifiers.ctrl || !guac_keyboard.modifiers.alt)
  938. return;
  939. // Assume [A-Z] never require AltGr
  940. if (keysym >= 0x0041 && keysym <= 0x005A)
  941. return;
  942. // Assume [a-z] never require AltGr
  943. if (keysym >= 0x0061 && keysym <= 0x007A)
  944. return;
  945. // Release Ctrl+Alt if the keysym is printable
  946. if (keysym <= 0xFF || (keysym & 0xFF000000) === 0x01000000) {
  947. guac_keyboard.release(0xFFE3); // Left ctrl
  948. guac_keyboard.release(0xFFE4); // Right ctrl
  949. guac_keyboard.release(0xFFE9); // Left alt
  950. guac_keyboard.release(0xFFEA); // Right alt
  951. }
  952. };
  953. /**
  954. * Reads through the event log, interpreting the first event, if possible,
  955. * and returning that event. If no events can be interpreted, due to a
  956. * total lack of events or the need for more events, null is returned. Any
  957. * interpreted events are automatically removed from the log.
  958. *
  959. * @private
  960. * @return {KeyEvent}
  961. * The first key event in the log, if it can be interpreted, or null
  962. * otherwise.
  963. */
  964. var interpret_event = function interpret_event() {
  965. // Peek at first event in log
  966. var first = eventLog[0];
  967. if (!first)
  968. return null;
  969. // Keydown event
  970. if (first instanceof KeydownEvent) {
  971. var keysym = null;
  972. var accepted_events = [];
  973. // Defer handling of Meta until it is known to be functioning as a
  974. // modifier (it may otherwise actually be an alternative method for
  975. // pressing a single key, such as Meta+Left for Home on ChromeOS)
  976. if (first.keysym === 0xFFE7 || first.keysym === 0xFFE8) {
  977. // Defer handling until further events exist to provide context
  978. if (eventLog.length === 1)
  979. return null;
  980. // Drop keydown if it turns out Meta does not actually apply
  981. if (eventLog[1].keysym !== first.keysym) {
  982. if (!eventLog[1].modifiers.meta)
  983. return eventLog.shift();
  984. }
  985. // Drop duplicate keydown events while waiting to determine
  986. // whether to acknowledge Meta (browser may repeat keydown
  987. // while the key is held)
  988. else if (eventLog[1] instanceof KeydownEvent)
  989. return eventLog.shift();
  990. }
  991. // If event itself is reliable, no need to wait for other events
  992. if (first.reliable) {
  993. keysym = first.keysym;
  994. accepted_events = eventLog.splice(0, 1);
  995. }
  996. // If keydown is immediately followed by a keypress, use the indicated character
  997. else if (eventLog[1] instanceof KeypressEvent) {
  998. keysym = eventLog[1].keysym;
  999. accepted_events = eventLog.splice(0, 2);
  1000. }
  1001. // If keydown is immediately followed by anything else, then no
  1002. // keypress can possibly occur to clarify this event, and we must
  1003. // handle it now
  1004. else if (eventLog[1]) {
  1005. keysym = first.keysym;
  1006. accepted_events = eventLog.splice(0, 1);
  1007. }
  1008. // Fire a key press if valid events were found
  1009. if (accepted_events.length > 0) {
  1010. syncModifierStates(first);
  1011. if (keysym) {
  1012. // Fire event
  1013. release_simulated_altgr(keysym);
  1014. var defaultPrevented = !guac_keyboard.press(keysym);
  1015. recentKeysym[first.keyCode] = keysym;
  1016. // Release the key now if we cannot rely on the associated
  1017. // keyup event
  1018. if (!first.keyupReliable)
  1019. guac_keyboard.release(keysym);
  1020. // Record whether default was prevented
  1021. for (var i=0; i<accepted_events.length; i++)
  1022. accepted_events[i].defaultPrevented = defaultPrevented;
  1023. }
  1024. return first;
  1025. }
  1026. } // end if keydown
  1027. // Keyup event
  1028. else if (first instanceof KeyupEvent && !quirks.keyupUnreliable) {
  1029. // Release specific key if known
  1030. var keysym = first.keysym;
  1031. if (keysym) {
  1032. guac_keyboard.release(keysym);
  1033. delete recentKeysym[first.keyCode];
  1034. first.defaultPrevented = true;
  1035. }
  1036. // Otherwise, fall back to releasing all keys
  1037. else {
  1038. guac_keyboard.reset();
  1039. return first;
  1040. }
  1041. syncModifierStates(first);
  1042. return eventLog.shift();
  1043. } // end if keyup
  1044. // Ignore any other type of event (keypress by itself is invalid, and
  1045. // unreliable keyup events should simply be dumped)
  1046. else
  1047. return eventLog.shift();
  1048. // No event interpreted
  1049. return null;
  1050. };
  1051. /**
  1052. * Returns the keyboard location of the key associated with the given
  1053. * keyboard event. The location differentiates key events which otherwise
  1054. * have the same keycode, such as left shift vs. right shift.
  1055. *
  1056. * @private
  1057. * @param {!KeyboardEvent} e
  1058. * A JavaScript keyboard event, as received through the DOM via a
  1059. * "keydown", "keyup", or "keypress" handler.
  1060. *
  1061. * @returns {!number}
  1062. * The location of the key event on the keyboard, as defined at:
  1063. * http://www.w3.org/TR/DOM-Level-3-Events/#events-KeyboardEvent
  1064. */
  1065. var getEventLocation = function getEventLocation(e) {
  1066. // Use standard location, if possible
  1067. if ('location' in e)
  1068. return e.location;
  1069. // Failing that, attempt to use deprecated keyLocation
  1070. if ('keyLocation' in e)
  1071. return e.keyLocation;
  1072. // If no location is available, assume left side
  1073. return 0;
  1074. };
  1075. /**
  1076. * Attempts to mark the given Event as having been handled by this
  1077. * Guacamole.Keyboard. If the Event has already been marked as handled,
  1078. * false is returned.
  1079. *
  1080. * @param {!Event} e
  1081. * The Event to mark.
  1082. *
  1083. * @returns {!boolean}
  1084. * true if the given Event was successfully marked, false if the given
  1085. * Event was already marked.
  1086. */
  1087. var markEvent = function markEvent(e) {
  1088. // Fail if event is already marked
  1089. if (e[EVENT_MARKER])
  1090. return false;
  1091. // Mark event otherwise
  1092. e[EVENT_MARKER] = true;
  1093. return true;
  1094. };
  1095. /**
  1096. * Attaches event listeners to the given Element, automatically translating
  1097. * received key, input, and composition events into simple keydown/keyup
  1098. * events signalled through this Guacamole.Keyboard's onkeydown and
  1099. * onkeyup handlers.
  1100. *
  1101. * @param {!(Element|Document)} element
  1102. * The Element to attach event listeners to for the sake of handling
  1103. * key or input events.
  1104. */
  1105. this.listenTo = function listenTo(element) {
  1106. // When key pressed
  1107. element.addEventListener("keydown", function(e) {
  1108. // Only intercept if handler set
  1109. if (!guac_keyboard.onkeydown) return;
  1110. // Ignore events which have already been handled
  1111. if (!markEvent(e)) return;
  1112. var keydownEvent = new KeydownEvent(e);
  1113. // Ignore (but do not prevent) the "composition" keycode sent by some
  1114. // browsers when an IME is in use (see: http://lists.w3.org/Archives/Public/www-dom/2010JulSep/att-0182/keyCode-spec.html)
  1115. if (keydownEvent.keyCode === 229)
  1116. return;
  1117. // Log event
  1118. eventLog.push(keydownEvent);
  1119. // Interpret as many events as possible, prevent default if indicated
  1120. if (interpret_events())
  1121. e.preventDefault();
  1122. }, true);
  1123. // When key pressed
  1124. element.addEventListener("keypress", function(e) {
  1125. // Only intercept if handler set
  1126. if (!guac_keyboard.onkeydown && !guac_keyboard.onkeyup) return;
  1127. // Ignore events which have already been handled
  1128. if (!markEvent(e)) return;
  1129. // Log event
  1130. eventLog.push(new KeypressEvent(e));
  1131. // Interpret as many events as possible, prevent default if indicated
  1132. if (interpret_events())
  1133. e.preventDefault();
  1134. }, true);
  1135. // When key released
  1136. element.addEventListener("keyup", function(e) {
  1137. // Only intercept if handler set
  1138. if (!guac_keyboard.onkeyup) return;
  1139. // Ignore events which have already been handled
  1140. if (!markEvent(e)) return;
  1141. e.preventDefault();
  1142. // Log event, call for interpretation
  1143. eventLog.push(new KeyupEvent(e));
  1144. interpret_events();
  1145. }, true);
  1146. /**
  1147. * Handles the given "input" event, typing the data within the input text.
  1148. * If the event is complete (text is provided), handling of "compositionend"
  1149. * events is suspended, as such events may conflict with input events.
  1150. *
  1151. * @private
  1152. * @param {!InputEvent} e
  1153. * The "input" event to handle.
  1154. */
  1155. var handleInput = function handleInput(e) {
  1156. // Only intercept if handler set
  1157. if (!guac_keyboard.onkeydown && !guac_keyboard.onkeyup) return;
  1158. // Ignore events which have already been handled
  1159. if (!markEvent(e)) return;
  1160. // Type all content written
  1161. if (e.data && !e.isComposing) {
  1162. element.removeEventListener("compositionend", handleComposition, false);
  1163. guac_keyboard.type(e.data);
  1164. }
  1165. };
  1166. /**
  1167. * Handles the given "compositionend" event, typing the data within the
  1168. * composed text. If the event is complete (composed text is provided),
  1169. * handling of "input" events is suspended, as such events may conflict
  1170. * with composition events.
  1171. *
  1172. * @private
  1173. * @param {!CompositionEvent} e
  1174. * The "compositionend" event to handle.
  1175. */
  1176. var handleComposition = function handleComposition(e) {
  1177. // Only intercept if handler set
  1178. if (!guac_keyboard.onkeydown && !guac_keyboard.onkeyup) return;
  1179. // Ignore events which have already been handled
  1180. if (!markEvent(e)) return;
  1181. // Type all content written
  1182. if (e.data) {
  1183. element.removeEventListener("input", handleInput, false);
  1184. guac_keyboard.type(e.data);
  1185. }
  1186. };
  1187. // Automatically type text entered into the wrapped field
  1188. element.addEventListener("input", handleInput, false);
  1189. element.addEventListener("compositionend", handleComposition, false);
  1190. };
  1191. // Listen to given element, if any
  1192. if (element)
  1193. guac_keyboard.listenTo(element);
  1194. };
  1195. /**
  1196. * The unique numerical identifier to assign to the next Guacamole.Keyboard
  1197. * instance.
  1198. *
  1199. * @private
  1200. * @type {!number}
  1201. */
  1202. Guacamole.Keyboard._nextID = 0;
  1203. /**
  1204. * The state of all supported keyboard modifiers.
  1205. * @constructor
  1206. */
  1207. Guacamole.Keyboard.ModifierState = function() {
  1208. /**
  1209. * Whether shift is currently pressed.
  1210. *
  1211. * @type {!boolean}
  1212. */
  1213. this.shift = false;
  1214. /**
  1215. * Whether ctrl is currently pressed.
  1216. *
  1217. * @type {!boolean}
  1218. */
  1219. this.ctrl = false;
  1220. /**
  1221. * Whether alt is currently pressed.
  1222. *
  1223. * @type {!boolean}
  1224. */
  1225. this.alt = false;
  1226. /**
  1227. * Whether meta (apple key) is currently pressed.
  1228. *
  1229. * @type {!boolean}
  1230. */
  1231. this.meta = false;
  1232. /**
  1233. * Whether hyper (windows key) is currently pressed.
  1234. *
  1235. * @type {!boolean}
  1236. */
  1237. this.hyper = false;
  1238. };
  1239. /**
  1240. * Returns the modifier state applicable to the keyboard event given.
  1241. *
  1242. * @param {!KeyboardEvent} e
  1243. * The keyboard event to read.
  1244. *
  1245. * @returns {!Guacamole.Keyboard.ModifierState}
  1246. * The current state of keyboard modifiers.
  1247. */
  1248. Guacamole.Keyboard.ModifierState.fromKeyboardEvent = function(e) {
  1249. var state = new Guacamole.Keyboard.ModifierState();
  1250. // Assign states from old flags
  1251. state.shift = e.shiftKey;
  1252. state.ctrl = e.ctrlKey;
  1253. state.alt = e.altKey;
  1254. state.meta = e.metaKey;
  1255. // Use DOM3 getModifierState() for others
  1256. if (e.getModifierState) {
  1257. state.hyper = e.getModifierState("OS")
  1258. || e.getModifierState("Super")
  1259. || e.getModifierState("Hyper")
  1260. || e.getModifierState("Win");
  1261. }
  1262. return state;
  1263. };