GUACAMOLE-232: Semantically represent platform/browser key event quirks.

This commit is contained in:
Michael Jumper
2018-01-14 20:35:45 -08:00
parent b5361a5889
commit d84f03afea

View File

@@ -55,6 +55,36 @@ Guacamole.Keyboard = function(element) {
*/ */
this.onkeyup = null; this.onkeyup = null;
/**
* Set of known platform-specific or browser-specific quirks which must be
* accounted for to properly interpret key events, even if the only way to
* reliably detect that quirk is to platform/browser-sniff.
*
* @private
* @type {Object.<String, Boolean>}
*/
var quirks = {
/**
* Whether the Alt key is actually a modifier for typable keys and is
* thus never used for keyboard shortcuts.
*
* @type {Boolean}
*/
altIsTypableOnly: false
};
// Set quirk flags depending on platform/browser, if such information is
// available
if (navigator && navigator.platform) {
// The Alt key on Mac is never used for keyboard shortcuts
if (navigator.platform.match(/^mac/i))
quirks.altIsTypableOnly = true;
}
/** /**
* A key event having a corresponding timestamp. This event is non-specific. * A key event having a corresponding timestamp. This event is non-specific.
* Its subclasses should be used instead when recording specific key * Its subclasses should be used instead when recording specific key
@@ -175,6 +205,14 @@ Guacamole.Keyboard = function(element) {
this.keysym = keysym_from_key_identifier(key, location) this.keysym = keysym_from_key_identifier(key, location)
|| keysym_from_keycode(keyCode, location); || keysym_from_keycode(keyCode, location);
/**
* Whether the keyup following this keydown event is known to be
* reliable. If false, we cannot rely on the keyup event to occur.
*
* @type {Boolean}
*/
this.keyupReliable = true;
// DOM3 and keyCode are reliable sources if the corresponding key is // DOM3 and keyCode are reliable sources if the corresponding key is
// not a printable key // not a printable key
if (this.keysym && !isPrintable(this.keysym)) if (this.keysym && !isPrintable(this.keysym))
@@ -184,9 +222,13 @@ Guacamole.Keyboard = function(element) {
if (!this.keysym && key_identifier_sane(keyCode, keyIdentifier)) if (!this.keysym && key_identifier_sane(keyCode, keyIdentifier))
this.keysym = keysym_from_key_identifier(keyIdentifier, location, guac_keyboard.modifiers.shift); this.keysym = keysym_from_key_identifier(keyIdentifier, location, guac_keyboard.modifiers.shift);
// If a key is pressed while meta is held down, the keyup will
// never be sent in Chrome (bug #108404)
if (guac_keyboard.modifiers.meta && this.keysym !== 0xFFE7 && this.keysym !== 0xFFE8)
this.keyupReliable = false;
// Determine whether default action for Alt+combinations must be prevented // Determine whether default action for Alt+combinations must be prevented
var prevent_alt = !guac_keyboard.modifiers.ctrl var prevent_alt = !guac_keyboard.modifiers.ctrl && !quirks.altIsTypableOnly;
&& !(navigator && navigator.platform && navigator.platform.match(/^mac/i));
// Determine whether default action for Ctrl+combinations must be prevented // Determine whether default action for Ctrl+combinations must be prevented
var prevent_ctrl = !guac_keyboard.modifiers.alt; var prevent_ctrl = !guac_keyboard.modifiers.alt;
@@ -961,9 +1003,9 @@ Guacamole.Keyboard = function(element) {
var defaultPrevented = !guac_keyboard.press(keysym); var defaultPrevented = !guac_keyboard.press(keysym);
recentKeysym[first.keyCode] = keysym; recentKeysym[first.keyCode] = keysym;
// If a key is pressed while meta is held down, the keyup will // Release the key now if we cannot rely on the associated
// never be sent in Chrome, so send it now. (bug #108404) // keyup event
if (guac_keyboard.modifiers.meta && keysym !== 0xFFE7 && keysym !== 0xFFE8) if (!first.keyupReliable)
guac_keyboard.release(keysym); guac_keyboard.release(keysym);
// Record whether default was prevented // Record whether default was prevented
@@ -998,7 +1040,8 @@ Guacamole.Keyboard = function(element) {
} // end if keyup } // end if keyup
// Ignore any other type of event (keypress by itself is invalid) // Ignore any other type of event (keypress by itself is invalid, and
// unreliable keyup events should simply be dumped)
else else
return eventLog.shift(); return eventLog.shift();