GUACAMOLE-1386: Do not synchronize modifiers based on key events that were dropped/rejected.

This commit is contained in:
Michael Jumper
2021-08-12 21:53:27 -07:00
parent b347a118fa
commit b629b2137e

View File

@@ -136,14 +136,25 @@ Guacamole.Keyboard = function Keyboard(element) {
* *
* @private * @private
* @constructor * @constructor
* @param {Guacamole.Keyboard.ModifierState} [modifiers]
* The state of all local keyboard modifiers at the time this event was
* received.
*/ */
var KeyEvent = function() { var KeyEvent = function KeyEvent(modifiers) {
/** /**
* Reference to this key event. * Reference to this key event.
*/ */
var key_event = this; var key_event = this;
/**
* The state of all local keyboard modifiers at the time this event was
* received.
*
* @type {Guacamole.Keyboard.ModifierState}
*/
this.modifiers = modifiers || new Guacamole.Keyboard.ModifierState();
/** /**
* An arbitrary timestamp in milliseconds, indicating this event's * An arbitrary timestamp in milliseconds, indicating this event's
* position in time relative to other events. * position in time relative to other events.
@@ -198,20 +209,30 @@ Guacamole.Keyboard = function Keyboard(element) {
* @private * @private
* @constructor * @constructor
* @augments Guacamole.Keyboard.KeyEvent * @augments Guacamole.Keyboard.KeyEvent
* @param {Number} keyCode The JavaScript key code of the key pressed. *
* @param {String} keyIdentifier The legacy DOM3 "keyIdentifier" of the key * @param {Guacamole.Keyboard.ModifierState} [modifiers]
* pressed, as defined at: * The state of all local keyboard modifiers at the time this event was
* received.
*
* @param {number} keyCode
* The JavaScript key code of the key pressed.
*
* @param {string} keyIdentifier
* The legacy DOM3 "keyIdentifier" of the key pressed, as defined at:
* http://www.w3.org/TR/2009/WD-DOM-Level-3-Events-20090908/#events-Events-KeyboardEvent * http://www.w3.org/TR/2009/WD-DOM-Level-3-Events-20090908/#events-Events-KeyboardEvent
* @param {String} key The standard name of the key pressed, as defined at: *
* http://www.w3.org/TR/DOM-Level-3-Events/#events-KeyboardEvent * @param {string} key
* @param {Number} location The location on the keyboard corresponding to * The standard name of the key pressed, as defined at:
* the key pressed, as defined at:
* http://www.w3.org/TR/DOM-Level-3-Events/#events-KeyboardEvent * http://www.w3.org/TR/DOM-Level-3-Events/#events-KeyboardEvent
*
* @param {number} location
* The location on the keyboard corresponding to the key pressed, as
* defined at: http://www.w3.org/TR/DOM-Level-3-Events/#events-KeyboardEvent
*/ */
var KeydownEvent = function(keyCode, keyIdentifier, key, location) { var KeydownEvent = function KeydownEvent(modifiers, keyCode, keyIdentifier, key, location) {
// We extend KeyEvent // We extend KeyEvent
KeyEvent.apply(this); KeyEvent.call(this, modifiers);
/** /**
* The JavaScript key code of the key pressed. * The JavaScript key code of the key pressed.
@@ -304,13 +325,14 @@ Guacamole.Keyboard = function Keyboard(element) {
* @private * @private
* @constructor * @constructor
* @augments Guacamole.Keyboard.KeyEvent * @augments Guacamole.Keyboard.KeyEvent
* @param {Number} charCode The Unicode codepoint of the character that * @param {number} charCode
* would be typed by the key pressed. * The Unicode codepoint of the character that would be typed by the
* key pressed.
*/ */
var KeypressEvent = function(charCode) { var KeypressEvent = function KeypressEvent(modifiers, charCode) {
// We extend KeyEvent // We extend KeyEvent
KeyEvent.apply(this); KeyEvent.call(this, modifiers);
/** /**
* The Unicode codepoint of the character that would be typed by the * The Unicode codepoint of the character that would be typed by the
@@ -348,10 +370,10 @@ Guacamole.Keyboard = function Keyboard(element) {
* the key released, as defined at: * the key released, as defined at:
* http://www.w3.org/TR/DOM-Level-3-Events/#events-KeyboardEvent * http://www.w3.org/TR/DOM-Level-3-Events/#events-KeyboardEvent
*/ */
var KeyupEvent = function(keyCode, keyIdentifier, key, location) { var KeyupEvent = function(modifiers, keyCode, keyIdentifier, key, location) {
// We extend KeyEvent // We extend KeyEvent
KeyEvent.apply(this); KeyEvent.call(this, modifiers);
/** /**
* The JavaScript key code of the key released. * The JavaScript key code of the key released.
@@ -922,27 +944,28 @@ Guacamole.Keyboard = function Keyboard(element) {
}; };
/** /**
* Given the remote and local state of a particular key, resynchronizes the * Resynchronizes the remote state of the given modifier with its
* remote state of that key with the local state through pressing or * corresponding local modifier state, as dictated by
* {@link KeyEvent#modifiers} within the given key event, by pressing or
* releasing keysyms. * releasing keysyms.
* *
* @private * @private
* @param {Boolean} remoteState * @param {string} modifier
* Whether the key is currently pressed remotely. * The name of the {@link Guacamole.Keyboard.ModifierState} property
* being updated.
* *
* @param {Boolean} localState * @param {number[]} keysyms
* Whether the key is currently pressed remotely locally. If the state * The keysyms which represent the modifier being updated.
* of the key is not known, this may be undefined.
*
* @param {Number[]} keysyms
* The keysyms which represent the key being updated.
* *
* @param {KeyEvent} keyEvent * @param {KeyEvent} keyEvent
* Guacamole's current best interpretation of the key event being * Guacamole's current best interpretation of the key event being
* processed. * processed.
*/ */
var updateModifierState = function updateModifierState(remoteState, var updateModifierState = function updateModifierState(modifier,
localState, keysyms, keyEvent) { keysyms, keyEvent) {
var localState = keyEvent.modifiers[modifier];
var remoteState = guac_keyboard.modifiers[modifier];
var i; var i;
@@ -987,56 +1010,50 @@ Guacamole.Keyboard = function Keyboard(element) {
}; };
/** /**
* Given a keyboard event, updates the local modifier state and remote * Given a keyboard event, updates the remote key state to match the local
* key state based on the modifier flags within the event. This function * modifier state and remote based on the modifier flags within the event.
* pays no attention to keycodes. * This function pays no attention to keycodes.
* *
* @private * @private
* @param {KeyboardEvent} e
* The keyboard event containing the flags to update.
*
* @param {KeyEvent} keyEvent * @param {KeyEvent} keyEvent
* Guacamole's current best interpretation of the key event being * Guacamole's current best interpretation of the key event being
* processed. * processed.
*/ */
var syncModifierStates = function syncModifierStates(e, keyEvent) { var syncModifierStates = function syncModifierStates(keyEvent) {
// Get state
var state = Guacamole.Keyboard.ModifierState.fromKeyboardEvent(e);
// Resync state of alt // Resync state of alt
updateModifierState(guac_keyboard.modifiers.alt, state.alt, [ updateModifierState('alt', [
0xFFE9, // Left alt 0xFFE9, // Left alt
0xFFEA, // Right alt 0xFFEA, // Right alt
0xFE03 // AltGr 0xFE03 // AltGr
], keyEvent); ], keyEvent);
// Resync state of shift // Resync state of shift
updateModifierState(guac_keyboard.modifiers.shift, state.shift, [ updateModifierState('shift', [
0xFFE1, // Left shift 0xFFE1, // Left shift
0xFFE2 // Right shift 0xFFE2 // Right shift
], keyEvent); ], keyEvent);
// Resync state of ctrl // Resync state of ctrl
updateModifierState(guac_keyboard.modifiers.ctrl, state.ctrl, [ updateModifierState('ctrl', [
0xFFE3, // Left ctrl 0xFFE3, // Left ctrl
0xFFE4 // Right ctrl 0xFFE4 // Right ctrl
], keyEvent); ], keyEvent);
// Resync state of meta // Resync state of meta
updateModifierState(guac_keyboard.modifiers.meta, state.meta, [ updateModifierState('meta', [
0xFFE7, // Left meta 0xFFE7, // Left meta
0xFFE8 // Right meta 0xFFE8 // Right meta
], keyEvent); ], keyEvent);
// Resync state of hyper // Resync state of hyper
updateModifierState(guac_keyboard.modifiers.hyper, state.hyper, [ updateModifierState('hyper', [
0xFFEB, // Left super/hyper 0xFFEB, // Left super/hyper
0xFFEC // Right super/hyper 0xFFEC // Right super/hyper
], keyEvent); ], keyEvent);
// Update state // Update state
guac_keyboard.modifiers = state; guac_keyboard.modifiers = keyEvent.modifiers;
}; };
@@ -1157,15 +1174,18 @@ Guacamole.Keyboard = function Keyboard(element) {
if (eventLog.length === 1) if (eventLog.length === 1)
return null; return null;
// Ignore keydown of Meta unless immediately followed by a // Drop keydown if it turns out Meta does not actually apply
// reliable keyup of the same (it will instead be pressed if (eventLog[1].keysym !== first.keysym) {
// implicitly if truly recognized as Meta in conjunction with if (!eventLog[1].modifiers.meta)
// another non-Meta keypress) return eventLog.shift();
if (quirks.keyupUnreliable || !(eventLog[1] instanceof KeyupEvent) || eventLog[1].keysym !== first.keysym) {
eventLog.pop();
return null;
} }
// Drop duplicate keydown events while waiting to determine
// whether to acknowledge Meta (browser may repeat keydown
// while the key is held)
else if (eventLog[1] instanceof KeydownEvent)
return eventLog.shift();
} }
// If event itself is reliable, no need to wait for other events // If event itself is reliable, no need to wait for other events
@@ -1191,6 +1211,8 @@ Guacamole.Keyboard = function Keyboard(element) {
// Fire a key press if valid events were found // Fire a key press if valid events were found
if (accepted_events.length > 0) { if (accepted_events.length > 0) {
syncModifierStates(first);
if (keysym) { if (keysym) {
// Fire event // Fire event
@@ -1232,6 +1254,7 @@ Guacamole.Keyboard = function Keyboard(element) {
return first; return first;
} }
syncModifierStates(first);
return eventLog.shift(); return eventLog.shift();
} // end if keyup } // end if keyup
@@ -1324,9 +1347,8 @@ Guacamole.Keyboard = function Keyboard(element) {
if (window.event) keyCode = window.event.keyCode; if (window.event) keyCode = window.event.keyCode;
else if (e.which) keyCode = e.which; else if (e.which) keyCode = e.which;
// Fix modifier states var modifiers = Guacamole.Keyboard.ModifierState.fromKeyboardEvent(e);
var keydownEvent = new KeydownEvent(keyCode, e.keyIdentifier, e.key, getEventLocation(e)); var keydownEvent = new KeydownEvent(modifiers, keyCode, e.keyIdentifier, e.key, getEventLocation(e));
syncModifierStates(e, keydownEvent);
// Ignore (but do not prevent) the "composition" keycode sent by some // Ignore (but do not prevent) the "composition" keycode sent by some
// browsers when an IME is in use (see: http://lists.w3.org/Archives/Public/www-dom/2010JulSep/att-0182/keyCode-spec.html) // browsers when an IME is in use (see: http://lists.w3.org/Archives/Public/www-dom/2010JulSep/att-0182/keyCode-spec.html)
@@ -1356,8 +1378,8 @@ Guacamole.Keyboard = function Keyboard(element) {
else if (e.which) charCode = e.which; else if (e.which) charCode = e.which;
// Fix modifier states // Fix modifier states
var keypressEvent = new KeypressEvent(charCode); var modifiers = Guacamole.Keyboard.ModifierState.fromKeyboardEvent(e);
syncModifierStates(e, keypressEvent); var keypressEvent = new KeypressEvent(modifiers, charCode);
// Log event // Log event
eventLog.push(keypressEvent); eventLog.push(keypressEvent);
@@ -1384,8 +1406,8 @@ Guacamole.Keyboard = function Keyboard(element) {
else if (e.which) keyCode = e.which; else if (e.which) keyCode = e.which;
// Fix modifier states // Fix modifier states
var keyupEvent = new KeyupEvent(keyCode, e.keyIdentifier, e.key, getEventLocation(e)); var modifiers = Guacamole.Keyboard.ModifierState.fromKeyboardEvent(e);
syncModifierStates(e, keyupEvent); var keyupEvent = new KeyupEvent(modifiers, keyCode, e.keyIdentifier, e.key, getEventLocation(e));
// Log event, call for interpretation // Log event, call for interpretation
eventLog.push(keyupEvent); eventLog.push(keyupEvent);