From 17fd23baf05aca1fa2ef7a5fb441f3153d6ef639 Mon Sep 17 00:00:00 2001 From: Michael Jumper Date: Mon, 27 Apr 2015 14:06:12 -0700 Subject: [PATCH] GUAC-1170: Use keyLocation only if necessary. --- .../src/main/webapp/modules/Keyboard.js | 32 +++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) diff --git a/guacamole-common-js/src/main/webapp/modules/Keyboard.js b/guacamole-common-js/src/main/webapp/modules/Keyboard.js index 1835740c6..9a93864cb 100644 --- a/guacamole-common-js/src/main/webapp/modules/Keyboard.js +++ b/guacamole-common-js/src/main/webapp/modules/Keyboard.js @@ -954,6 +954,34 @@ Guacamole.Keyboard = function(element) { } + /** + * Returns the keyboard location of the key associated with the given + * keyboard event. The location differentiates key events which otherwise + * have the same keycode, such as left shift vs. right shift. + * + * @param {KeyboardEvent} e + * A JavaScript keyboard event, as received through the DOM via a + * "keydown", "keyup", or "keypress" handler. + * + * @returns {Number} + * The location of the key event on the keyboard, as defined at: + * http://www.w3.org/TR/DOM-Level-3-Events/#events-KeyboardEvent + */ + var getEventLocation = function getEventLocation(e) { + + // Use standard location, if possible + if ('location' in e) + return e.location; + + // Failing that, attempt to use deprecated keyLocation + if ('keyLocation' in e) + return e.keyLocation; + + // If no location is available, assume left side + return 0; + + }; + // When key pressed element.addEventListener("keydown", function(e) { @@ -973,7 +1001,7 @@ Guacamole.Keyboard = function(element) { return; // Log event - var keydownEvent = new KeydownEvent(keyCode, e.keyIdentifier, e.key, e.location || e.keyLocation); + var keydownEvent = new KeydownEvent(keyCode, e.keyIdentifier, e.key, getEventLocation(e)); eventLog.push(keydownEvent); // Interpret as many events as possible, prevent default if indicated @@ -1021,7 +1049,7 @@ Guacamole.Keyboard = function(element) { update_modifier_state(e); // Log event, call for interpretation - var keyupEvent = new KeyupEvent(keyCode, e.keyIdentifier, e.key, e.location || e.keyLocation); + var keyupEvent = new KeyupEvent(keyCode, e.keyIdentifier, e.key, getEventLocation(e)); eventLog.push(keyupEvent); interpret_events();