Renamed deferred handler.
[guacamole-common-js.git] / src / main / resources / keyboard.js
index 6787af8..82a85c5 100644 (file)
@@ -93,6 +93,7 @@ Guacamole.Keyboard = function(element) {
         19:  0xFF13, // pause/break
         20:  0xFFE5, // caps lock
         27:  0xFF1B, // escape
+        32:  0x0020, // space
         33:  0xFF55, // page up
         34:  0xFF56, // page down
         35:  0xFF57, // end
@@ -261,7 +262,7 @@ Guacamole.Keyboard = function(element) {
 
     // Stops repeating keystrokes
     function stopRepeat() {
-        if (repeatKeyTimeoutId != -1) clearInterval(repeatKeyTimeoutId);
+        if (repeatKeyTimeoutId != -1) clearTimeout(repeatKeyTimeoutId);
         if (repeatKeyIntervalId != -1) clearInterval(repeatKeyIntervalId);
     }
 
@@ -292,13 +293,22 @@ Guacamole.Keyboard = function(element) {
 
     }
 
-    function getKeySymFromCharCode(keyCode) {
+    function isControlCharacter(codepoint) {
+        return codepoint <= 0x1F || (codepoint >= 0x7F && codepoint <= 0x9F);
+    }
+
+    function getKeySymFromCharCode(codepoint) {
 
-        if (keyCode >= 0x0000 && keyCode <= 0x00FF)
-            return keyCode;
+        // Keysyms for control characters
+        if (isControlCharacter(codepoint)) return 0xFF00 | codepoint;
 
-        if (keyCode >= 0x0100 && keyCode <= 0x10FFFF)
-            return 0x01000000 | keyCode;
+        // Keysyms for ASCII chars
+        if (codepoint >= 0x0000 && codepoint <= 0x00FF)
+            return codepoint;
+
+        // Keysyms for Unicode
+        if (codepoint >= 0x0100 && codepoint <= 0x10FFFF)
+            return 0x01000000 | codepoint;
 
         return null;
 
@@ -353,7 +363,7 @@ Guacamole.Keyboard = function(element) {
     var keydown_keysym = null;
     var keypress_keysym = null;
 
-    function fireKeyPress() {
+    function handleKeyEvents() {
 
         // Prefer keysym from keypress
         var keysym = keypress_keysym || keydown_keysym;
@@ -399,8 +409,7 @@ Guacamole.Keyboard = function(element) {
         var codepoint = parseInt(hex, 16);
 
         // If control character, not typable
-        if (codepoint <= 0x1F) return false;
-        if (codepoint >= 0x7F && codepoint <= 0x9F) return false;
+        if (isControlCharacter(codepoint)) return false;
 
         // Otherwise, typable
         return true;
@@ -417,6 +426,12 @@ Guacamole.Keyboard = function(element) {
         if (window.event) keynum = window.event.keyCode;
         else if (e.which) keynum = e.which;
 
+        // Ignore any unknown key events
+        if (keynum == 0) {
+            e.preventDefault();
+            return;
+        }
+
         // Ctrl/Alt/Shift
         if (keynum == 16)      guac_keyboard.modifiers.shift = true;
         else if (keynum == 17) guac_keyboard.modifiers.ctrl  = true;
@@ -425,6 +440,10 @@ Guacamole.Keyboard = function(element) {
         // Try to get keysym from keycode
         keydown_keysym = getKeySymFromKeyCode(keynum);
 
+        // If key is known from keycode, prevent default
+        if (keydown_keysym)
+            e.preventDefault();
+        
         // Also try to get get keysym from keyIdentifier
         if (e.keyIdentifier) {
 
@@ -449,7 +468,7 @@ Guacamole.Keyboard = function(element) {
         // Defer handling of event until after any other pending
         // key events.
         if (!deferred_keypress)
-            deferred_keypress = window.setTimeout(fireKeyPress, 0);
+            deferred_keypress = window.setTimeout(handleKeyEvents, 0);
 
     };
 
@@ -457,7 +476,9 @@ Guacamole.Keyboard = function(element) {
     element.onkeypress = function(e) {
 
         // Only intercept if handler set
-        if (!guac_keyboard.onkeydown) return true;
+        if (!guac_keyboard.onkeydown) return;
+
+        e.preventDefault();
 
         var keynum;
         if (window.event) keynum = window.event.keyCode;
@@ -465,17 +486,17 @@ Guacamole.Keyboard = function(element) {
 
         keypress_keysym = getKeySymFromCharCode(keynum);
 
-        // If event identified as a typable character (keypress involved)
-        // then release Ctrl and Alt (if pressed)
-        if (guac_keyboard.modifiers.ctrl) sendKeyReleased(0xFFE3);
-        if (guac_keyboard.modifiers.alt)  sendKeyReleased(0xFFE9);
+        // If event identified as a typable character, and we're holding Ctrl+Alt,
+        // assume Ctrl+Alt is actually AltGr, and release both.
+        if (!isControlCharacter(keynum) && guac_keyboard.modifiers.ctrl && guac_keyboard.modifiers.alt) {
+            sendKeyReleased(0xFFE3);
+            sendKeyReleased(0xFFE9);
+        }
 
         // Defer handling of event until after any other pending
         // key events.
         if (!deferred_keypress)
-            deferred_keypress = window.setTimeout(fireKeyPress, 0);
-
-        return false;
+            deferred_keypress = window.setTimeout(handleKeyEvents, 0);
 
     };
 
@@ -483,27 +504,35 @@ Guacamole.Keyboard = function(element) {
     element.onkeyup = function(e) {
 
         // Only intercept if handler set
-        if (!guac_keyboard.onkeyup) return true;
+        if (!guac_keyboard.onkeyup) return;
+
+        e.preventDefault();
 
         var keynum;
         if (window.event) keynum = window.event.keyCode;
         else if (e.which) keynum = e.which;
         
-        // Ctrl/Alt/Shift
-        if (keynum == 16)      guac_keyboard.modifiers.shift = false;
-        else if (keynum == 17) guac_keyboard.modifiers.ctrl  = false;
-        else if (keynum == 18) guac_keyboard.modifiers.alt   = false;
-        else
-            stopRepeat();
+        // Defer handling of keyup (otherwise, keyup may happen before
+        // deferred handling of keydown/keypress).
+        window.setTimeout(function() {
+
+            // Ctrl/Alt/Shift
+            if (keynum == 16)      guac_keyboard.modifiers.shift = false;
+            else if (keynum == 17) guac_keyboard.modifiers.ctrl  = false;
+            else if (keynum == 18) guac_keyboard.modifiers.alt   = false;
+            else
+                stopRepeat();
+
+            // Get corresponding character
+            var lastKeyDownChar = keydownChar[keynum];
 
-        // Get corresponding character
-        var lastKeyDownChar = keydownChar[keynum];
+            // Clear character record
+            keydownChar[keynum] = null;
 
-        // Clear character record
-        keydownChar[keynum] = null;
+            // Send release event
+            sendKeyReleased(lastKeyDownChar);
 
-        // Send release event
-        return sendKeyReleased(lastKeyDownChar);
+        }, 0);
 
     };