2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
15 * The Original Code is guacamole-common-js.
17 * The Initial Developer of the Original Code is
19 * Portions created by the Initial Developer are Copyright (C) 2010
20 * the Initial Developer. All Rights Reserved.
24 * Alternatively, the contents of this file may be used under the terms of
25 * either the GNU General Public License Version 2 or later (the "GPL"), or
26 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
36 * ***** END LICENSE BLOCK ***** */
38 // Guacamole namespace
39 var Guacamole = Guacamole || {};
42 * Provides cross-browser and cross-keyboard keyboard for a specific element.
43 * Browser and keyboard layout variation is abstracted away, providing events
44 * which represent keys as their corresponding X11 keysym.
47 * @param {Element} element The Element to use to provide keyboard events.
49 Guacamole.Keyboard = function(element) {
52 * Reference to this Guacamole.Keyboard.
55 var guac_keyboard = this;
58 * Fired whenever the user presses a key with the element associated
59 * with this Guacamole.Keyboard in focus.
62 * @param {Number} keysym The keysym of the key being pressed.
63 * @returns {Boolean} true if the originating event of this keypress should
64 * be allowed through to the browser, false or undefined
67 this.onkeydown = null;
70 * Fired whenever the user releases a key with the element associated
71 * with this Guacamole.Keyboard in focus.
74 * @param {Number} keysym The keysym of the key being released.
75 * @returns {Boolean} true if the originating event of this key release
76 * should be allowed through to the browser, false or
77 * undefined otherwise.
82 * Map of known JavaScript keycodes which do not map to typable characters
83 * to their unshifted X11 keysym equivalents.
86 var unshiftedKeySym = {
87 8: 0xFF08, // backspace
93 19: 0xFF13, // pause/break
94 20: 0xFFE5, // caps lock
97 33: 0xFF55, // page up
98 34: 0xFF56, // page down
101 37: 0xFF51, // left arrow
102 38: 0xFF52, // up arrow
103 39: 0xFF53, // right arrow
104 40: 0xFF54, // down arrow
105 45: 0xFF63, // insert
106 46: 0xFFFF, // delete
107 91: 0xFFEB, // left window key (super_l)
108 92: 0xFF67, // right window key (menu key?)
109 93: null, // select key
122 144: 0xFF7F, // num lock
123 145: 0xFF14 // scroll lock
127 * Map of known JavaScript keyidentifiers which do not map to typable
128 * characters to their unshifted X11 keysym equivalents.
131 var keyidentifier_keysym = {
132 "AllCandidates": 0xFF3D,
133 "Alphanumeric": 0xFF30,
177 "HangulMode": 0xFF31,
183 "JapaneseHiragana": 0xFF25,
184 "JapaneseKatakana": 0xFF26,
185 "JapaneseRomaji": 0xFF24,
196 "PreviousCandidate": 0xFF3E,
197 "PrintScreen": 0xFD1D,
199 "RomanCharacters": null,
209 * Map of known JavaScript keycodes which do not map to typable characters
210 * to their shifted X11 keysym equivalents. Keycodes must only be listed
211 * here if their shifted X11 keysym equivalents differ from their unshifted
215 var shiftedKeySym = {
220 * All modifiers and their states.
225 * Whether shift is currently pressed.
230 * Whether ctrl is currently pressed.
235 * Whether alt is currently pressed.
242 * The state of every key, indexed by keysym. If a particular key is
243 * pressed, the value of pressed for that keysym will be true. If a key
244 * is not currently pressed, the value for that keysym may be false or
249 var keydownChar = new Array();
251 // ID of routine repeating keystrokes. -1 = not repeating.
252 var repeatKeyTimeoutId = -1;
253 var repeatKeyIntervalId = -1;
255 // Starts repeating keystrokes
256 function startRepeat(keySym) {
257 repeatKeyIntervalId = setInterval(function() {
258 sendKeyReleased(keySym);
259 sendKeyPressed(keySym);
263 // Stops repeating keystrokes
264 function stopRepeat() {
265 if (repeatKeyTimeoutId != -1) clearTimeout(repeatKeyTimeoutId);
266 if (repeatKeyIntervalId != -1) clearInterval(repeatKeyIntervalId);
270 function getKeySymFromKeyIdentifier(shifted, keyIdentifier) {
272 var unicodePrefixLocation = keyIdentifier.indexOf("U+");
273 if (unicodePrefixLocation >= 0) {
275 var hex = keyIdentifier.substring(unicodePrefixLocation+2);
276 var codepoint = parseInt(hex, 16);
279 // Convert case if shifted
281 typedCharacter = String.fromCharCode(codepoint).toLowerCase();
283 typedCharacter = String.fromCharCode(codepoint).toUpperCase();
286 codepoint = typedCharacter.charCodeAt(0);
288 return getKeySymFromCharCode(codepoint);
292 return keyidentifier_keysym[keyIdentifier];
296 function isControlCharacter(codepoint) {
297 return codepoint <= 0x1F || (codepoint >= 0x7F && codepoint <= 0x9F);
300 function getKeySymFromCharCode(codepoint) {
302 // Keysyms for control characters
303 if (isControlCharacter(codepoint)) return 0xFF00 | codepoint;
305 // Keysyms for ASCII chars
306 if (codepoint >= 0x0000 && codepoint <= 0x00FF)
309 // Keysyms for Unicode
310 if (codepoint >= 0x0100 && codepoint <= 0x10FFFF)
311 return 0x01000000 | codepoint;
317 function getKeySymFromKeyCode(keyCode) {
320 if (!guac_keyboard.modifiers.shift) keysym = unshiftedKeySym[keyCode];
322 keysym = shiftedKeySym[keyCode];
323 if (keysym == null) keysym = unshiftedKeySym[keyCode];
331 // Sends a single keystroke over the network
332 function sendKeyPressed(keysym) {
334 // Mark key as pressed
335 guac_keyboard.pressed[keysym] = true;
338 if (keysym != null && guac_keyboard.onkeydown)
339 return guac_keyboard.onkeydown(keysym) != false;
345 // Sends a single keystroke over the network
346 function sendKeyReleased(keysym) {
348 // Mark key as released
349 guac_keyboard.pressed[keysym] = false;
352 if (keysym != null && guac_keyboard.onkeyup)
353 return guac_keyboard.onkeyup(keysym) != false;
360 var keydown_code = null;
362 var deferred_keypress = null;
363 var keydown_keysym = null;
364 var keypress_keysym = null;
366 function handleKeyEvents() {
368 // Prefer keysym from keypress
369 var keysym = keypress_keysym || keydown_keysym;
370 var keynum = keydown_code;
372 if (keydownChar[keynum] != keysym) {
374 // If this button is already pressed, release first
375 var lastKeyDownChar = keydownChar[keydown_code];
377 sendKeyReleased(lastKeyDownChar);
380 keydownChar[keynum] = keysym;
381 sendKeyPressed(keysym);
383 // Clear old key repeat, if any.
386 // Start repeating (if not a modifier key) after a short delay
387 if (keynum != 16 && keynum != 17 && keynum != 18)
388 repeatKeyTimeoutId = setTimeout(function() { startRepeat(keysym); }, 500);
392 // Done with deferred key event
393 deferred_keypress = null;
394 keypress_keysym = null;
395 keydown_keysym = null;
400 function isTypable(keyIdentifier) {
402 // Find unicode prefix
403 var unicodePrefixLocation = keyIdentifier.indexOf("U+");
404 if (unicodePrefixLocation == -1)
407 // Parse codepoint value
408 var hex = keyIdentifier.substring(unicodePrefixLocation+2);
409 var codepoint = parseInt(hex, 16);
411 // If control character, not typable
412 if (isControlCharacter(codepoint)) return false;
414 // Otherwise, typable
420 element.onkeydown = function(e) {
422 // Only intercept if handler set
423 if (!guac_keyboard.onkeydown) return;
426 if (window.event) keynum = window.event.keyCode;
427 else if (e.which) keynum = e.which;
429 // Ignore any unknown key events
435 var expect_keypress = true;
438 if (keynum == 16) guac_keyboard.modifiers.shift = true;
439 else if (keynum == 17) guac_keyboard.modifiers.ctrl = true;
440 else if (keynum == 18) guac_keyboard.modifiers.alt = true;
442 // Try to get keysym from keycode
443 keydown_keysym = getKeySymFromKeyCode(keynum);
445 // If key is known from keycode, prevent default
447 expect_keypress = false;
449 // Also try to get get keysym from keyIdentifier
450 if (e.keyIdentifier) {
452 keydown_keysym = keydown_keysym ||
453 getKeySymFromKeyIdentifier(guac_keyboard.modifiers.shift, e.keyIdentifier);
455 // Prevent default if non-typable character or if modifier combination
456 // likely to be eaten by browser otherwise (NOTE: We must not prevent
457 // default for Ctrl+Alt, as that combination is commonly used for
458 // AltGr. If we receive AltGr, we need to handle keypress, which
459 // means we cannot cancel keydown).
460 if (!isTypable(e.keyIdentifier)
461 || ( guac_keyboard.modifiers.ctrl && !guac_keyboard.modifiers.alt)
462 || (!guac_keyboard.modifiers.ctrl && guac_keyboard.modifiers.alt))
463 expect_keypress = false;
467 // Set keycode which will be associated with any future keypress
468 keydown_code = keynum;
470 // If we expect to handle via keypress, set failsafe timeout and
471 // wait for keypress.
472 if (expect_keypress) {
473 if (!deferred_keypress)
474 deferred_keypress = window.setTimeout(handleKeyEvents, 0);
477 // Otherwise, handle now
486 element.onkeypress = function(e) {
488 // Only intercept if handler set
489 if (!guac_keyboard.onkeydown) return;
494 if (window.event) keynum = window.event.keyCode;
495 else if (e.which) keynum = e.which;
497 keypress_keysym = getKeySymFromCharCode(keynum);
499 // If event identified as a typable character, and we're holding Ctrl+Alt,
500 // assume Ctrl+Alt is actually AltGr, and release both.
501 if (!isControlCharacter(keynum) && guac_keyboard.modifiers.ctrl && guac_keyboard.modifiers.alt) {
502 sendKeyReleased(0xFFE3);
503 sendKeyReleased(0xFFE9);
506 // Clear timeout, if any
507 if (deferred_keypress)
508 window.clearTimeout(deferred_keypress);
510 // Handle event with all aggregated data
516 element.onkeyup = function(e) {
518 // Only intercept if handler set
519 if (!guac_keyboard.onkeyup) return;
524 if (window.event) keynum = window.event.keyCode;
525 else if (e.which) keynum = e.which;
528 if (keynum == 16) guac_keyboard.modifiers.shift = false;
529 else if (keynum == 17) guac_keyboard.modifiers.ctrl = false;
530 else if (keynum == 18) guac_keyboard.modifiers.alt = false;
534 // Get corresponding character
535 var lastKeyDownChar = keydownChar[keynum];
537 // Clear character record
538 keydownChar[keynum] = null;
540 // Send release event
541 sendKeyReleased(lastKeyDownChar);
545 // When focus is lost, clear modifiers.
546 element.onblur = function() {
547 guac_keyboard.modifiers.alt = false;
548 guac_keyboard.modifiers.ctrl = false;
549 guac_keyboard.modifiers.shift = false;