Fixed regression with key repeat.
[guacamole-common-js.git] / src / main / resources / keyboard.js
index 3dfc31a..07e4dcf 100644 (file)
@@ -1,21 +1,39 @@
 
-/*
- *  Guacamole - Clientless Remote Desktop
- *  Copyright (C) 2010  Michael Jumper
+/* ***** BEGIN LICENSE BLOCK *****
+ * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  *
- *  This program is free software: you can redistribute it and/or modify
- *  it under the terms of the GNU Affero General Public License as published by
- *  the Free Software Foundation, either version 3 of the License, or
- *  (at your option) any later version.
+ * The contents of this file are subject to the Mozilla Public License Version
+ * 1.1 (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ * http://www.mozilla.org/MPL/
  *
- *  This program is distributed in the hope that it will be useful,
- *  but WITHOUT ANY WARRANTY; without even the implied warranty of
- *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- *  GNU Affero General Public License for more details.
+ * Software distributed under the License is distributed on an "AS IS" basis,
+ * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
+ * for the specific language governing rights and limitations under the
+ * License.
  *
- *  You should have received a copy of the GNU Affero General Public License
- *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
- */
+ * The Original Code is guacamole-common-js.
+ *
+ * The Initial Developer of the Original Code is
+ * Michael Jumper.
+ * Portions created by the Initial Developer are Copyright (C) 2010
+ * the Initial Developer. All Rights Reserved.
+ *
+ * Contributor(s):
+ *
+ * Alternatively, the contents of this file may be used under the terms of
+ * either the GNU General Public License Version 2 or later (the "GPL"), or
+ * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
+ * in which case the provisions of the GPL or the LGPL are applicable instead
+ * of those above. If you wish to allow use of your version of this file only
+ * under the terms of either the GPL or the LGPL, and not to allow others to
+ * use your version of this file under the terms of the MPL, indicate your
+ * decision by deleting the provisions above and replace them with the notice
+ * and other provisions required by the GPL or the LGPL. If you do not delete
+ * the provisions above, a recipient may use your version of this file under
+ * the terms of any one of the MPL, the GPL or the LGPL.
+ *
+ * ***** END LICENSE BLOCK ***** */
 
 // Guacamole namespace
 var Guacamole = Guacamole || {};
@@ -42,8 +60,11 @@ Guacamole.Keyboard = function(element) {
      * 
      * @event
      * @param {Number} keysym The keysym of the key being pressed.
+     * @returns {Boolean} true if the originating event of this keypress should
+     *                    be allowed through to the browser, false or undefined
+     *                    otherwise.
      */
-       this.onkeydown = null;
+    this.onkeydown = null;
 
     /**
      * Fired whenever the user releases a key with the element associated
@@ -51,8 +72,11 @@ Guacamole.Keyboard = function(element) {
      * 
      * @event
      * @param {Number} keysym The keysym of the key being released.
+     * @returns {Boolean} true if the originating event of this key release 
+     *                    should be allowed through to the browser, false or
+     *                    undefined otherwise.
      */
-       this.onkeyup = null;
+    this.onkeyup = null;
 
     /**
      * Map of known JavaScript keycodes which do not map to typable characters
@@ -109,10 +133,10 @@ Guacamole.Keyboard = function(element) {
         18:  0xFFE7  // alt
     };
 
-       // Single key state/modifier buffer
-       var modShift = false;
-       var modCtrl = false;
-       var modAlt = false;
+    // Single key state/modifier buffer
+    var modShift = false;
+    var modCtrl = false;
+    var modAlt = false;
 
     var keydownChar = new Array();
 
@@ -120,19 +144,19 @@ Guacamole.Keyboard = function(element) {
     var repeatKeyTimeoutId = -1;
     var repeatKeyIntervalId = -1;
 
-       // Starts repeating keystrokes
-       function startRepeat(keySym) {
-               repeatKeyIntervalId = setInterval(function() {
+    // Starts repeating keystrokes
+    function startRepeat(keySym) {
+        repeatKeyIntervalId = setInterval(function() {
             sendKeyReleased(keySym);
             sendKeyPressed(keySym);
         }, 50);
-       }
+    }
 
-       // Stops repeating keystrokes
-       function stopRepeat() {
-               if (repeatKeyTimeoutId != -1) clearInterval(repeatKeyTimeoutId);
-               if (repeatKeyIntervalId != -1) clearInterval(repeatKeyIntervalId);
-       }
+    // Stops repeating keystrokes
+    function stopRepeat() {
+        if (repeatKeyTimeoutId != -1) clearInterval(repeatKeyTimeoutId);
+        if (repeatKeyIntervalId != -1) clearInterval(repeatKeyIntervalId);
+    }
 
 
     function getKeySymFromKeyIdentifier(shifted, keyIdentifier) {
@@ -176,8 +200,8 @@ Guacamole.Keyboard = function(element) {
     function getKeySymFromKeyCode(keyCode) {
 
         var keysym = null;
-               if (!modShift) keysym = unshiftedKeySym[keyCode];
-               else {
+        if (!modShift) keysym = unshiftedKeySym[keyCode];
+        else {
             keysym = shiftedKeySym[keyCode];
             if (keysym == null) keysym = unshiftedKeySym[keyCode];
         }
@@ -187,17 +211,19 @@ Guacamole.Keyboard = function(element) {
     }
 
 
-       // Sends a single keystroke over the network
-       function sendKeyPressed(keysym) {
-               if (keysym != null && guac_keyboard.onkeydown)
-                       guac_keyboard.onkeydown(keysym);
-       }
+    // Sends a single keystroke over the network
+    function sendKeyPressed(keysym) {
+        if (keysym != null && guac_keyboard.onkeydown)
+            return guac_keyboard.onkeydown(keysym) != false;
+        return true;
+    }
 
-       // Sends a single keystroke over the network
-       function sendKeyReleased(keysym) {
-               if (keysym != null && guac_keyboard.onkeyup)
-                       guac_keyboard.onkeyup(keysym);
-       }
+    // Sends a single keystroke over the network
+    function sendKeyReleased(keysym) {
+        if (keysym != null && guac_keyboard.onkeyup)
+            return guac_keyboard.onkeyup(keysym) != false;
+        return true;
+    }
 
 
     var KEYDOWN = 1;
@@ -205,24 +231,24 @@ Guacamole.Keyboard = function(element) {
 
     var keySymSource = null;
 
-       // When key pressed
+    // When key pressed
     var keydownCode = null;
-       element.onkeydown = function(e) {
+    element.onkeydown = function(e) {
 
         // Only intercept if handler set
         if (!guac_keyboard.onkeydown) return true;
 
-               var keynum;
-               if (window.event) keynum = window.event.keyCode;
-               else if (e.which) keynum = e.which;
+        var keynum;
+        if (window.event) keynum = window.event.keyCode;
+        else if (e.which) keynum = e.which;
 
-               // Ctrl/Alt/Shift
-               if (keynum == 16)
-                       modShift = true;
-               else if (keynum == 17)
-                       modCtrl = true;
-               else if (keynum == 18)
-                       modAlt = true;
+        // Ctrl/Alt/Shift
+        if (keynum == 16)
+            modShift = true;
+        else if (keynum == 17)
+            modCtrl = true;
+        else if (keynum == 18)
+            modAlt = true;
 
         var keysym = getKeySymFromKeyCode(keynum);
         if (keysym) {
@@ -255,7 +281,7 @@ Guacamole.Keyboard = function(element) {
 
                 // Send event
                 keydownChar[keynum] = keysym;
-                sendKeyPressed(keysym);
+                var returnValue = sendKeyPressed(keysym);
 
                 // Clear old key repeat, if any.
                 stopRepeat();
@@ -263,16 +289,23 @@ Guacamole.Keyboard = function(element) {
                 // Start repeating (if not a modifier key) after a short delay
                 if (keynum != 16 && keynum != 17 && keynum != 18)
                     repeatKeyTimeoutId = setTimeout(function() { startRepeat(keysym); }, 500);
+
+                // Use return code provided by handler
+                return returnValue;
+
             }
 
+            // Default to canceling event if no keypress is being sent, but
+            // source of events is keydown.
             return false;
+
         }
 
         return true;
 
-       };
+    };
 
-       // When key pressed
+    // When key pressed
     element.onkeypress = function(e) {
 
         // Only intercept if handler set
@@ -280,9 +313,9 @@ Guacamole.Keyboard = function(element) {
 
         if (keySymSource != KEYPRESS) return false;
 
-               var keynum;
-               if (window.event) keynum = window.event.keyCode;
-               else if (e.which) keynum = e.which;
+        var keynum;
+        if (window.event) keynum = window.event.keyCode;
+        else if (e.which) keynum = e.which;
 
         var keysym = getKeySymFromCharCode(keynum);
         if (keysym && keydownChar[keynum] != keysym) {
@@ -298,32 +331,37 @@ Guacamole.Keyboard = function(element) {
             stopRepeat();
 
             // Send key event
-            sendKeyPressed(keysym);
+            var returnValue = sendKeyPressed(keysym);
 
             // Start repeating (if not a modifier key) after a short delay
             repeatKeyTimeoutId = setTimeout(function() { startRepeat(keysym); }, 500);
+
+            return returnValue;
         }
 
+        // Default to canceling event if no keypress is being sent, but
+        // source of events is keypress.
         return false;
-       };
 
-       // When key released
-       element.onkeyup = function(e) {
+    };
+
+    // When key released
+    element.onkeyup = function(e) {
 
         // Only intercept if handler set
         if (!guac_keyboard.onkeyup) return true;
 
-               var keynum;
-               if (window.event) keynum = window.event.keyCode;
-               else if (e.which) keynum = e.which;
-               
-               // Ctrl/Alt/Shift
-               if (keynum == 16)
-                       modShift = false;
-               else if (keynum == 17)
-                       modCtrl = false;
-               else if (keynum == 18)
-                       modAlt = false;
+        var keynum;
+        if (window.event) keynum = window.event.keyCode;
+        else if (e.which) keynum = e.which;
+        
+        // Ctrl/Alt/Shift
+        if (keynum == 16)
+            modShift = false;
+        else if (keynum == 17)
+            modCtrl = false;
+        else if (keynum == 18)
+            modAlt = false;
         else
             stopRepeat();
 
@@ -334,16 +372,15 @@ Guacamole.Keyboard = function(element) {
         keydownChar[keynum] = null;
 
         // Send release event
-        sendKeyReleased(lastKeyDownChar);
+        return sendKeyReleased(lastKeyDownChar);
 
-               return false;
-       };
+    };
 
-       // When focus is lost, clear modifiers.
-       element.onblur = function() {
-               modAlt = false;
-               modCtrl = false;
-               modShift = false;
-       };
+    // When focus is lost, clear modifiers.
+    element.onblur = function() {
+        modAlt = false;
+        modCtrl = false;
+        modShift = false;
+    };
 
 };