diff --git a/src/core/main/custom/hotkey.ts b/src/core/main/custom/hotkey.ts index f632d57..81fca20 100644 --- a/src/core/main/custom/hotkey.ts +++ b/src/core/main/custom/hotkey.ts @@ -151,7 +151,7 @@ export class Hotkey extends EventEmitter { * @param emit 是否触发set事件,当且仅当从fromJSON方法调用时为false */ set(id: string, key: KeyCode, assist: number, emit: boolean = true) { - const { ctrl, shift, alt } = this.unwarpBinary(assist); + const { ctrl, shift, alt } = unwarpBinary(assist); const data = this.data[id]; const before = this.keyMap.get(data.key)!; deleteWith(before, data); @@ -175,13 +175,13 @@ export class Hotkey extends EventEmitter { assist: number, type: KeyEmitType, ev: KeyboardEvent - ) { - if (!this.enabled) return; + ): boolean { + if (!this.enabled) return false; const when = this.conditionMap.get(this.scope)!; - if (!when()) return; + if (!when()) return false; const toEmit = this.keyMap.get(key); - if (!toEmit) return; - const { ctrl, shift, alt } = this.unwarpBinary(assist); + if (!toEmit) return false; + const { ctrl, shift, alt } = unwarpBinary(assist); toEmit.forEach(v => { if (type !== v.type) return; if (ctrl === v.ctrl && shift === v.shift && alt === v.alt) { @@ -193,6 +193,7 @@ export class Hotkey extends EventEmitter { } }); this.emit('emit', key, assist, type); + return toEmit.length > 0; } /** @@ -248,14 +249,6 @@ export class Hotkey extends EventEmitter { } } - private unwarpBinary(bin: number): AssistHoykey { - return { - ctrl: !!(bin & (1 << 0)), - shift: !!(bin & (1 << 1)), - alt: !!(bin & (1 << 2)) - }; - } - private ensureMap(key: KeyCode) { if (!this.keyMap.has(key)) { this.keyMap.set(key, []); @@ -270,3 +263,11 @@ export class Hotkey extends EventEmitter { return this.list.find(v => v.id === id); } } + +export function unwarpBinary(bin: number): AssistHoykey { + return { + ctrl: !!(bin & (1 << 0)), + shift: !!(bin & (1 << 1)), + alt: !!(bin & (1 << 2)) + }; +} diff --git a/src/core/main/init/hotkey.ts b/src/core/main/init/hotkey.ts index 14cc820..2a04fd0 100644 --- a/src/core/main/init/hotkey.ts +++ b/src/core/main/init/hotkey.ts @@ -507,10 +507,14 @@ gameKey.fromJSON(keyStorage.toJSON()); document.addEventListener('keyup', e => { const assist = generateBinary([e.ctrlKey, e.shiftKey, e.altKey]); const code = keycode(e.keyCode); - gameKey.emitKey(code, assist, 'up', e); + if (gameKey.emitKey(code, assist, 'up', e)) { + e.preventDefault(); + } }); document.addEventListener('keydown', e => { const assist = generateBinary([e.ctrlKey, e.shiftKey, e.altKey]); const code = keycode(e.keyCode); - gameKey.emitKey(code, assist, 'down', e); + if (gameKey.emitKey(code, assist, 'down', e)) { + e.preventDefault(); + } });