From 3768575bad672afd55edefbe0d1b2076f809c482 Mon Sep 17 00:00:00 2001 From: Tamius Han Date: Tue, 22 Oct 2019 01:33:56 +0200 Subject: [PATCH] Allow default keyboard shortcuts to work on non-ASCII layouts --- src/common/js/KeyboardShortcutParser.js | 2 +- src/ext/lib/ActionHandler.js | 25 +++++++++++++++++-- .../SetShortcutButton.vue | 1 + 3 files changed, 25 insertions(+), 3 deletions(-) diff --git a/src/common/js/KeyboardShortcutParser.js b/src/common/js/KeyboardShortcutParser.js index 03236fc..f87c86c 100644 --- a/src/common/js/KeyboardShortcutParser.js +++ b/src/common/js/KeyboardShortcutParser.js @@ -1,6 +1,6 @@ class KeyboardShortcutParser { static parseShortcut(keypress) { - var shortcutCombo = ''; + let shortcutCombo = ''; if (keypress.ctrlKey) { shortcutCombo += 'Ctrl + '; diff --git a/src/ext/lib/ActionHandler.js b/src/ext/lib/ActionHandler.js index 791e72b..2048d8e 100644 --- a/src/ext/lib/ActionHandler.js +++ b/src/ext/lib/ActionHandler.js @@ -181,19 +181,40 @@ class ActionHandler { return false; } - isActionMatch(shortcut, event) { + isLatin(key) { + return 'abcdefghijklmnopqrstuvwxyz,.-+1234567890'.indexOf(key.toLocaleLowerCase()) !== -1; + } + + isActionMatchStandard(shortcut, event) { return shortcut.key === event.key && shortcut.ctrlKey === event.ctrlKey && shortcut.metaKey === event.metaKey && shortcut.altKey === event.altKey && shortcut.shiftKey === event.shiftKey } + isActionMatchKeycode(shortcut, event) { + return shortcut.keyCode === event.key && + shortcut.ctrlKey === event.ctrlKey && + shortcut.metaKey === event.metaKey && + shortcut.altKey === event.altKey && + shortcut.shiftKey === event.shiftKey + } + + isActionMatch(shortcut, event, isLatin = true) { + // ASCII and symbols fall back to keycode matching, because we don't know for sure that + // regular matching by key is going to work + return isLatin ? + this.isActionMatchStandard(shortcut, event) : + this.isActionMatchStandard(shortcut, event) || this.isActionMatchKeycode(shortcut, event); + } execAction(actions, event, videoData) { this.logger.log('info', 'keyboard', "%c[ActionHandler::execAction] Trying to find and execute action for event. Actions/event: ", "color: #ff0", actions, event); + const isLatin = this.isLatin(event.key); + for (var action of actions) { - if (this.isActionMatch(action.shortcut, event)) { + if (this.isActionMatch(action.shortcut, event, isLatin)) { this.logger.log('info', 'keyboard', "%c[ActionHandler::execAction] found an action associated with keypress/event: ", "color: #ff0", action); for (var cmd of action.cmd) { diff --git a/src/options/controls-settings/scope-settings-component/SetShortcutButton.vue b/src/options/controls-settings/scope-settings-component/SetShortcutButton.vue index 7810b4d..0a07260 100644 --- a/src/options/controls-settings/scope-settings-component/SetShortcutButton.vue +++ b/src/options/controls-settings/scope-settings-component/SetShortcutButton.vue @@ -38,6 +38,7 @@ export default { if (this.waitingForPress) { const shortcut = { key: event.key, + keyCode: event.keyCode, ctrlKey: event.ctrlKey, metaKey: event.metaKey, altKey: event.altKey,