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
96 33: 0xFF55, // page up
97 34: 0xFF56, // page down
100 37: 0xFF51, // left arrow
101 38: 0xFF52, // up arrow
102 39: 0xFF53, // right arrow
103 40: 0xFF54, // down arrow
104 45: 0xFF63, // insert
105 46: 0xFFFF, // delete
106 91: 0xFFEB, // left window key (super_l)
107 92: 0xFF67, // right window key (menu key?)
108 93: null, // select key
121 144: 0xFF7F, // num lock
122 145: 0xFF14 // scroll lock
126 * Map of known JavaScript keycodes which do not map to typable characters
127 * to their shifted X11 keysym equivalents. Keycodes must only be listed
128 * here if their shifted X11 keysym equivalents differ from their unshifted
132 var shiftedKeySym = {
137 * All modifiers and their states.
142 * Whether shift is currently pressed.
147 * Whether ctrl is currently pressed.
152 * Whether alt is currently pressed.
159 * The state of every key, indexed by keysym. If a particular key is
160 * pressed, the value of pressed for that keysym will be true. If a key
161 * is not currently pressed, the value for that keysym may be false or
166 var keydownChar = new Array();
168 // ID of routine repeating keystrokes. -1 = not repeating.
169 var repeatKeyTimeoutId = -1;
170 var repeatKeyIntervalId = -1;
172 // Starts repeating keystrokes
173 function startRepeat(keySym) {
174 repeatKeyIntervalId = setInterval(function() {
175 sendKeyReleased(keySym);
176 sendKeyPressed(keySym);
180 // Stops repeating keystrokes
181 function stopRepeat() {
182 if (repeatKeyTimeoutId != -1) clearInterval(repeatKeyTimeoutId);
183 if (repeatKeyIntervalId != -1) clearInterval(repeatKeyIntervalId);
187 function getKeySymFromKeyIdentifier(shifted, keyIdentifier) {
189 var unicodePrefixLocation = keyIdentifier.indexOf("U+");
190 if (unicodePrefixLocation >= 0) {
192 var hex = keyIdentifier.substring(unicodePrefixLocation+2);
193 var codepoint = parseInt(hex, 16);
196 // Convert case if shifted
198 typedCharacter = String.fromCharCode(codepoint).toLowerCase();
200 typedCharacter = String.fromCharCode(codepoint).toUpperCase();
203 codepoint = typedCharacter.charCodeAt(0);
205 return getKeySymFromCharCode(codepoint);
213 function getKeySymFromCharCode(keyCode) {
215 if (keyCode >= 0x0000 && keyCode <= 0x00FF)
218 if (keyCode >= 0x0100 && keyCode <= 0x10FFFF)
219 return 0x01000000 | keyCode;
225 function getKeySymFromKeyCode(keyCode) {
228 if (!guac_keyboard.modifiers.shift) keysym = unshiftedKeySym[keyCode];
230 keysym = shiftedKeySym[keyCode];
231 if (keysym == null) keysym = unshiftedKeySym[keyCode];
239 // Sends a single keystroke over the network
240 function sendKeyPressed(keysym) {
242 // Mark key as pressed
243 guac_keyboard.pressed[keysym] = true;
246 if (keysym != null && guac_keyboard.onkeydown)
247 return guac_keyboard.onkeydown(keysym) != false;
253 // Sends a single keystroke over the network
254 function sendKeyReleased(keysym) {
256 // Mark key as released
257 guac_keyboard.pressed[keysym] = false;
260 if (keysym != null && guac_keyboard.onkeyup)
261 return guac_keyboard.onkeyup(keysym) != false;
268 var keydown_code = null;
270 var deferred_keypress = null;
271 var keydown_keysym = null;
272 var keypress_keysym = null;
274 function fireKeyPress() {
276 // Prefer keysym from keypress
277 var keysym = keypress_keysym || keydown_keysym;
278 var keynum = keydown_code;
280 if (keydownChar[keynum] != keysym) {
282 // If this button is already pressed, release first
283 var lastKeyDownChar = keydownChar[keydown_code];
285 sendKeyReleased(lastKeyDownChar);
288 keydownChar[keynum] = keysym;
289 sendKeyPressed(keysym);
291 // Clear old key repeat, if any.
294 // Start repeating (if not a modifier key) after a short delay
295 if (keynum != 16 && keynum != 17 && keynum != 18)
296 repeatKeyTimeoutId = setTimeout(function() { startRepeat(keysym); }, 500);
300 // Done with deferred key event
301 deferred_keypress = null;
306 element.onkeydown = function(e) {
308 // Only intercept if handler set
309 if (!guac_keyboard.onkeydown) return true;
312 if (window.event) keynum = window.event.keyCode;
313 else if (e.which) keynum = e.which;
316 if (keynum == 16) guac_keyboard.modifiers.shift = true;
317 else if (keynum == 17) guac_keyboard.modifiers.ctrl = true;
318 else if (keynum == 18) guac_keyboard.modifiers.alt = true;
320 // Try to get keysym from keycode
321 keydown_keysym = getKeySymFromKeyCode(keynum);
323 // Also try to get get keysym from keyIdentifier
325 keydown_keysym = getKeySymFromKeyIdentifier(guac_keyboard.modifiers.shift, e.keyIdentifier);
327 // Set keycode which will be associated with any future keypress
328 keydown_code = keynum;
330 // Defer handling of event until after any other pending
332 if (!deferred_keypress)
333 deferred_keypress = window.setTimeout(fireKeyPress, 0);
340 element.onkeypress = function(e) {
342 // Only intercept if handler set
343 if (!guac_keyboard.onkeydown) return true;
345 if (keySymSource != KEYPRESS) return false;
348 if (window.event) keynum = window.event.keyCode;
349 else if (e.which) keynum = e.which;
351 keypress_keysym = getKeySymFromCharCode(keynum);
353 // Defer handling of event until after any other pending
355 if (!deferred_keypress)
356 deferred_keypress = window.setTimeout(fireKeyPress, 0);
363 element.onkeyup = function(e) {
365 // Only intercept if handler set
366 if (!guac_keyboard.onkeyup) return true;
369 if (window.event) keynum = window.event.keyCode;
370 else if (e.which) keynum = e.which;
373 if (keynum == 16) guac_keyboard.modifiers.shift = false;
374 else if (keynum == 17) guac_keyboard.modifiers.ctrl = false;
375 else if (keynum == 18) guac_keyboard.modifiers.alt = false;
379 // Get corresponding character
380 var lastKeyDownChar = keydownChar[keynum];
382 // Clear character record
383 keydownChar[keynum] = null;
385 // Send release event
386 return sendKeyReleased(lastKeyDownChar);
390 // When focus is lost, clear modifiers.
391 element.onblur = function() {
392 guac_keyboard.modifiers.alt = false;
393 guac_keyboard.modifiers.ctrl = false;
394 guac_keyboard.modifiers.shift = false;