Skip to content

Commit

Permalink
Remove spurious ControlLeft-key when AltRight is pressed under Windows (
Browse files Browse the repository at this point in the history
#1144)

Remove ControlLeft from keyboard input sequence ControlLeft + AltRight if platform is Windows and AltRight is pressed.
  • Loading branch information
chschnell authored Sep 3, 2024
1 parent f3339aa commit 3c77b98
Showing 1 changed file with 67 additions and 4 deletions.
71 changes: 67 additions & 4 deletions src/browser/keyboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ var SHIFT_SCAN_CODE = 0x2A;
/** @const */
var SCAN_CODE_RELEASE = 0x80;

/** @const */
const PLATFOM_WINDOWS = typeof window !== "undefined" && window.navigator.platform.toString().toLowerCase().search("win") >= 0;

/**
* @constructor
*
Expand All @@ -19,6 +22,24 @@ function KeyboardAdapter(bus)
*/
keys_pressed = {},

/**
* Deferred KeyboardEvent or null (Windows AltGr-Filter)
* @type {KeyboardEvent|Object|null}
*/
deferred_event = null,

/**
* Deferred keydown state (Windows AltGr-Filter)
* @type {boolean}
*/
deferred_keydown = false,

/**
* Timeout-ID returned by setTimeout() or 0 (Windows AltGr-Filter)
* @type {number}
*/
deferred_timeout_id = 0,

keyboard = this;

/**
Expand Down Expand Up @@ -366,6 +387,7 @@ function KeyboardAdapter(bus)
}

/**
* @param {KeyboardEvent|Object} e
* @param {boolean} keydown
*/
function handler(e, keydown)
Expand All @@ -380,6 +402,51 @@ function KeyboardAdapter(bus)
return;
}

e.preventDefault && e.preventDefault();

if(PLATFOM_WINDOWS)
{
// Remove ControlLeft from key sequence [ControlLeft, AltRight] when
// AltGraph-key is pressed or released.
//
// NOTE: AltGraph is false for the 1st key (ControlLeft-Down), becomes
// true with the 2nd (AltRight-Down) and stays true until key AltGraph
// is released (AltRight-Up).
if(deferred_event)
{
clearTimeout(deferred_timeout_id);
if(!(e.getModifierState && e.getModifierState("AltGraph") &&
deferred_keydown === keydown &&
deferred_event.code === "ControlLeft" && e.code === "AltRight"))
{
handle_event(deferred_event, deferred_keydown);
}
deferred_event = null;
}

if(e.code === "ControlLeft")
{
// defer ControlLeft-Down/-Up until the next invocation of this method or 10ms have passed, whichever comes first
deferred_event = e;
deferred_keydown = keydown;
deferred_timeout_id = setTimeout(() => {
handle_event(deferred_event, deferred_keydown);
deferred_event = null;
}, 10);
return false;
}
}

handle_event(e, keydown);
return false;
}

/**
* @param {KeyboardEvent|Object} e
* @param {boolean} keydown
*/
function handle_event(e, keydown)
{
var code = translate(e);

if(!code)
Expand All @@ -389,10 +456,6 @@ function KeyboardAdapter(bus)
}

handle_code(code, keydown, e.repeat);

e.preventDefault && e.preventDefault();

return false;
}

/**
Expand Down

0 comments on commit 3c77b98

Please sign in to comment.