Skip to content

Commit

Permalink
fix: nuxt cannot find window
Browse files Browse the repository at this point in the history
  • Loading branch information
caoyugang committed Sep 29, 2023
1 parent 6d523c4 commit c4bef1b
Showing 1 changed file with 15 additions and 13 deletions.
28 changes: 15 additions & 13 deletions src/hotkeyManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,23 @@ class HotKeyManager {
readonly pressedKeys = new Map<string, boolean>()

constructor () {
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)
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('keyup', (e) => {
if (this.pressedKeys.has(e.key)) {
this.pressedKeys.delete(e.key)
}
})
}
}

registerHotKey = (hotkey: HotKey) => {
Expand Down

0 comments on commit c4bef1b

Please sign in to comment.