-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
65 changed files
with
628 additions
and
413 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import { KEY_CODE } from './keycode.js' | ||
|
||
class HotKeyManager { | ||
registeredHotkeys = {} | ||
pressedKeys = new Map() | ||
|
||
constructor() { | ||
if (typeof window !== 'undefined') { | ||
window.addEventListener('keydown', (e) => { | ||
this.pressedKeys.set(e.key, e.repeat) | ||
const keyComb = this.getKeyComb(Array.from(this.pressedKeys.keys())) | ||
this.registeredHotkeys[keyComb]?.forEach((hotKey) => { | ||
if (!e.repeat || hotKey?.repeat) { | ||
if (hotKey.preventDefault) e.preventDefault() | ||
hotKey.handler([...hotKey.keys], e) | ||
} | ||
}) | ||
}) | ||
window.addEventListener('keyup', (e) => { | ||
if (this.pressedKeys.has(e.key)) this.pressedKeys.delete(e.key) | ||
}) | ||
window.addEventListener('blur', () => { | ||
this.pressedKeys.clear() | ||
}) | ||
} | ||
} | ||
|
||
registerHotKey = (hotkey) => { | ||
const keyComb = this.getKeyComb([...hotkey.keys]) | ||
if (!this.registeredHotkeys[keyComb]) this.registeredHotkeys[keyComb] = [] | ||
|
||
this.registeredHotkeys[keyComb].push(hotkey) | ||
} | ||
|
||
getKeyComb = (keys) => { | ||
const convertKeys = keys.map((item) => { | ||
const stringKey = KEY_CODE[item] | ||
if (stringKey) return stringKey | ||
|
||
return item | ||
}) | ||
return convertKeys.sort().join(' ') | ||
} | ||
|
||
removeHotKey = (hotKey) => { | ||
const keyComb = this.getKeyComb([...hotKey.keys]) | ||
const index = this.registeredHotkeys[keyComb]?.indexOf(hotKey) ?? -1 | ||
if (index !== -1) { | ||
this.registeredHotkeys[keyComb].splice(index, 1) | ||
return true | ||
} | ||
return false | ||
} | ||
} | ||
|
||
export default new HotKeyManager() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
export const KEY_CODE = { | ||
ctrl: 'Control', | ||
space: ' ', | ||
enter: 'Enter', | ||
tab: 'Tab', | ||
delete: 'Delete', | ||
esc: 'Escape', | ||
alt: 'Alt', | ||
option: 'Alt', | ||
shift: 'Shift', | ||
back: 'Backspace', | ||
meta: 'Meta', | ||
command: 'Meta', | ||
up: 'ArrowUp', | ||
left: 'ArrowLeft', | ||
right: 'ArrowRight', | ||
bottom: 'ArrowDown' | ||
} |
Oops, something went wrong.