From 3fd339ffcb6a73817799e5d2ad1ac7c012921f19 Mon Sep 17 00:00:00 2001 From: unanmed <1319491857@qq.com> Date: Fri, 23 Oct 2026 00:45:40 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=8A=AC=E8=B5=B7=E6=8C=89=E9=94=AE?= =?UTF-8?q?=E7=9A=84=E4=BC=98=E5=85=88=E7=BA=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/system/src/hotkey/system.ts | 22 +++++++++++++++++++--- packages/system/src/hotkey/type.ts | 4 ++-- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/packages/system/src/hotkey/system.ts b/packages/system/src/hotkey/system.ts index e660d50..88a183f 100644 --- a/packages/system/src/hotkey/system.ts +++ b/packages/system/src/hotkey/system.ts @@ -11,8 +11,6 @@ import { IHotkeySystem } from './type'; -// TODO: 优先级阻断 - interface IHotkeyScope { /** 作用域唯一标识符 */ readonly symbol: symbol; @@ -346,13 +344,15 @@ export class HotkeySystem implements IHotkeySystem, IExcitable { const index = this.holding.indexOf(key); this.holding.splice(index, 1); + const toEmit: IHotkeyCommandPending[] = []; + // 需要快照 for (const [command, pending] of new Map(this.holdingCmds)) { // 对于 KeyUp,如果有可以触发的,那么按规则触发 if (command.mode === HotkeyEmitMode.KeyUp) { const emit = this.matchKeyUp(pending.keys, key); if (emit) { - this.emit(pending); + toEmit.push(pending); this.holdingCmds.delete(command); } } else { @@ -380,6 +380,22 @@ export class HotkeySystem implements IHotkeySystem, IExcitable { } } + // 抬起事件要看优先级 + toEmit.sort((a, b) => b.command.priority - a.command.priority); + + let blocking = -Infinity; + for (const pending of toEmit) { + if (pending.command.priority < blocking) break; + if (pending.command.block) { + blocking = pending.command.priority; + } + if (pending.command.timeout === 0) { + this.emit(pending); + } else { + this.waitTimeout.add(pending); + } + } + // 如果有按键可能触发,那么返回 true 来 preventDefault return this.holdingCmds.size > 0 || this.waitTimeout.size > 0; } diff --git a/packages/system/src/hotkey/type.ts b/packages/system/src/hotkey/type.ts index e782d5b..9dcb526 100644 --- a/packages/system/src/hotkey/type.ts +++ b/packages/system/src/hotkey/type.ts @@ -53,7 +53,7 @@ export interface IHotkeyEmitInfo { } export interface IHotkeyCommandInit { - /** 按键指令优先级 */ + /** 按键指令优先级,目前仅对抬起触发的按键生效 */ readonly priority: number; /** 默认按键组合,可设置多组 */ readonly defaults: KeyCode[][]; @@ -72,7 +72,7 @@ export interface IHotkeyCommandInit { export interface IHotkeyCommand { /** 按键指令的标识符 */ readonly command: string; - /** 按键指令优先级,越高越先执行,如果同优先级,则无法保证顺序 */ + /** 按键指令优先级,越高越先执行,如果同优先级,则无法保证顺序,目前仅对抬起触发的按键生效 */ readonly priority: number; /** 触发后,是否阻断优先级比当前优先级更低的按键的触发 */ readonly block: boolean;