From 860f5c9fdb7add690957d4595ecddaaa8c0c78a2 Mon Sep 17 00:00:00 2001 From: unanmed <1319491857@qq.com> Date: Fri, 24 Nov 2023 21:02:53 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E8=87=AA=E5=AE=9A=E4=B9=89=E6=8C=89?= =?UTF-8?q?=E9=94=AE=E9=98=BB=E6=AD=A2=E9=BB=98=E8=AE=A4=E8=A1=8C=E4=B8=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/core/main/custom/hotkey.ts | 29 +++++++++++++++-------------- src/core/main/init/hotkey.ts | 8 ++++++-- 2 files changed, 21 insertions(+), 16 deletions(-) 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(); + } });