From afdef94ec9b1006b1d3ae8ede32d83b84dc32bc1 Mon Sep 17 00:00:00 2001 From: Sergey Sorokin Date: Tue, 7 Aug 2018 12:26:05 +0300 Subject: [PATCH 1/3] Use event.code for keypress event --- Polyglot.safariextension/injected.js | 4 +- Polyglot.safariextension/keymap.js | 55 ++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+), 1 deletion(-) create mode 100644 Polyglot.safariextension/keymap.js diff --git a/Polyglot.safariextension/injected.js b/Polyglot.safariextension/injected.js index 1f1dbf6..e4ffa80 100644 --- a/Polyglot.safariextension/injected.js +++ b/Polyglot.safariextension/injected.js @@ -1,3 +1,5 @@ +import { getEventCode } from './keymap' + let settings = {} let isPanelOpen = false const PANEL_ID = 'polyglot__panel' @@ -41,7 +43,7 @@ function handleKeypress(e) { const applyShift = settings.useShiftKey ? e.shiftKey : true const applyCtrl = settings.useCtrlKey ? e.ctrlKey : true const applyAlt = settings.useAltKey ? e.altKey : true - const applyKey = settings.keyValue.charCodeAt(0) === e.keyCode + const applyKey = getEventCode(settings.keyValue.charAt(0)) === e.code if (applyMeta && applyShift && applyCtrl && applyAlt && applyKey) { e.preventDefault() diff --git a/Polyglot.safariextension/keymap.js b/Polyglot.safariextension/keymap.js new file mode 100644 index 0000000..ba0a9ad --- /dev/null +++ b/Polyglot.safariextension/keymap.js @@ -0,0 +1,55 @@ +const keymap = { + ' ': 'Space', + '0': 'Digit0', + '1': 'Digit1', + '2': 'Digit2', + '3': 'Digit3', + '4': 'Digit4', + '5': 'Digit5', + '6': 'Digit6', + '7': 'Digit7', + '8': 'Digit8', + '9': 'Digit9', + 'a': 'KeyA', + 'b': 'KeyB', + 'c': 'KeyC', + 'd': 'KeyD', + 'e': 'KeyE', + 'f': 'KeyF', + 'g': 'KeyG', + 'h': 'KeyH', + 'i': 'KeyI', + 'j': 'KeyJ', + 'k': 'KeyK', + 'l': 'KeyL', + 'm': 'KeyM', + 'n': 'KeyN', + 'o': 'KeyO', + 'p': 'KeyP', + 'q': 'KeyQ', + 'r': 'KeyR', + 's': 'KeyS', + 't': 'KeyT', + 'u': 'KeyU', + 'v': 'KeyV', + 'w': 'KeyW', + 'x': 'KeyX', + 'y': 'KeyY', + 'z': 'KeyZ', + ';': 'Semicolon', + '=': 'Equal', + ',': 'Comma', + '-': 'Minus', + '.': 'Period', + '/': 'Slash', + '`': 'Backquote', + '[': 'BracketLeft', + '\\': 'Backslash', + ']': 'BracketRight', + '\'': 'Quote', + 'ยง': 'IntlBackslash', +} + +export function getEventCode(char) { + return keymap[char] +} From 99559f4523769e730479ff3a41ec3afbff5f9ebb Mon Sep 17 00:00:00 2001 From: Sergey Sorokin Date: Wed, 8 Aug 2018 14:09:47 +0300 Subject: [PATCH 2/3] Convert shortcut char to lower case --- Polyglot.safariextension/keymap.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Polyglot.safariextension/keymap.js b/Polyglot.safariextension/keymap.js index ba0a9ad..acecce7 100644 --- a/Polyglot.safariextension/keymap.js +++ b/Polyglot.safariextension/keymap.js @@ -51,5 +51,5 @@ const keymap = { } export function getEventCode(char) { - return keymap[char] + return keymap[char.toLowerCase()] } From eaa9675e33e7972952bfb7d8793e4deaabda7937 Mon Sep 17 00:00:00 2001 From: Sergey Sorokin Date: Wed, 15 Aug 2018 10:44:18 +0300 Subject: [PATCH 3/3] Backward compatibility --- Polyglot.safariextension/injected.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/Polyglot.safariextension/injected.js b/Polyglot.safariextension/injected.js index e4ffa80..da62fb5 100644 --- a/Polyglot.safariextension/injected.js +++ b/Polyglot.safariextension/injected.js @@ -39,11 +39,14 @@ function handleMouseUp(e) { function handleKeypress(e) { // Check if shortcut key is properly configured if (settings.keyValue !== '') { + const keyValue = settings.keyValue + const keyCode = getEventCode(keyValue.charAt(0)) + const applyMeta = settings.useMetaKey ? e.metaKey : true const applyShift = settings.useShiftKey ? e.shiftKey : true const applyCtrl = settings.useCtrlKey ? e.ctrlKey : true const applyAlt = settings.useAltKey ? e.altKey : true - const applyKey = getEventCode(settings.keyValue.charAt(0)) === e.code + const applyKey = keyCode ? keyCode === e.code : keyValue.charCodeAt(0) === e.keyCode if (applyMeta && applyShift && applyCtrl && applyAlt && applyKey) { e.preventDefault()