Skip to content

Commit

Permalink
Update hotkeyListener to support non modifier keys
Browse files Browse the repository at this point in the history
This will allow future features like automatic bracket closing
  • Loading branch information
tomrule007 committed Sep 30, 2021
1 parent c5edccf commit a23f51c
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions helpers/hotkeyListener.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@ type keydownEventListener = (e: React.KeyboardEvent) => void
/**
* Given a hotkeyMap object
*
* ie: {"ctrl+z": someCallback, "cmd+z": someCallback}
* ie: {"ctrl+z": someCallback, "cmd+z": someCallback, ")": someOtherCallback}
*
* return a keydown eventListener that will call the corresponding callback and prevent default if key is detected
* \* Currently hardcoded to only support these modifier combinations: "crtl+key" or "cmd+key" or "shift+cmd+key"
*/
export const getHotkeyListener =
(hotkeyMap: { [hotkey: string]: () => void }): keydownEventListener =>
(hotkeyMap: {
[hotkey: string]: (key: string) => void
}): keydownEventListener =>
e => {
if (e.key === 'Control' || e.key === 'Shift' || !(e.ctrlKey || e.metaKey))
return
if (e.key === 'Control' || e.key === 'Shift') return

const detectedKeys = `${e.ctrlKey ? 'ctrl+' : ''}${
e.shiftKey ? 'shift+' : ''
Expand All @@ -23,6 +23,6 @@ export const getHotkeyListener =
const callback = hotkeyMap[detectedKeys]
if (callback) {
e.preventDefault()
callback()
callback(e.key)
}
}

0 comments on commit a23f51c

Please sign in to comment.