Skip to content

Commit

Permalink
Merge pull request #32 from isswp101/hotfix/use-event-code
Browse files Browse the repository at this point in the history
Use event.code for keypress event
  • Loading branch information
uetchy authored Aug 16, 2018
2 parents 90dc94a + eaa9675 commit 12dcaf9
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 1 deletion.
7 changes: 6 additions & 1 deletion Polyglot.safariextension/injected.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { getEventCode } from './keymap'

let settings = {}
let isPanelOpen = false
const PANEL_ID = 'polyglot__panel'
Expand Down Expand Up @@ -37,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 = settings.keyValue.charCodeAt(0) === e.keyCode
const applyKey = keyCode ? keyCode === e.code : keyValue.charCodeAt(0) === e.keyCode

if (applyMeta && applyShift && applyCtrl && applyAlt && applyKey) {
e.preventDefault()
Expand Down
55 changes: 55 additions & 0 deletions Polyglot.safariextension/keymap.js
Original file line number Diff line number Diff line change
@@ -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.toLowerCase()]
}

0 comments on commit 12dcaf9

Please sign in to comment.