feat: 自定义按键阻止默认行为

This commit is contained in:
unanmed 2023-11-24 21:02:53 +08:00
parent 168b59ccf3
commit 860f5c9fdb
2 changed files with 21 additions and 16 deletions

View File

@ -151,7 +151,7 @@ export class Hotkey extends EventEmitter<HotkeyEvent> {
* @param emit set事件fromJSON方法调用时为false * @param emit set事件fromJSON方法调用时为false
*/ */
set(id: string, key: KeyCode, assist: number, emit: boolean = true) { 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 data = this.data[id];
const before = this.keyMap.get(data.key)!; const before = this.keyMap.get(data.key)!;
deleteWith(before, data); deleteWith(before, data);
@ -175,13 +175,13 @@ export class Hotkey extends EventEmitter<HotkeyEvent> {
assist: number, assist: number,
type: KeyEmitType, type: KeyEmitType,
ev: KeyboardEvent ev: KeyboardEvent
) { ): boolean {
if (!this.enabled) return; if (!this.enabled) return false;
const when = this.conditionMap.get(this.scope)!; const when = this.conditionMap.get(this.scope)!;
if (!when()) return; if (!when()) return false;
const toEmit = this.keyMap.get(key); const toEmit = this.keyMap.get(key);
if (!toEmit) return; if (!toEmit) return false;
const { ctrl, shift, alt } = this.unwarpBinary(assist); const { ctrl, shift, alt } = unwarpBinary(assist);
toEmit.forEach(v => { toEmit.forEach(v => {
if (type !== v.type) return; if (type !== v.type) return;
if (ctrl === v.ctrl && shift === v.shift && alt === v.alt) { if (ctrl === v.ctrl && shift === v.shift && alt === v.alt) {
@ -193,6 +193,7 @@ export class Hotkey extends EventEmitter<HotkeyEvent> {
} }
}); });
this.emit('emit', key, assist, type); this.emit('emit', key, assist, type);
return toEmit.length > 0;
} }
/** /**
@ -248,14 +249,6 @@ export class Hotkey extends EventEmitter<HotkeyEvent> {
} }
} }
private unwarpBinary(bin: number): AssistHoykey {
return {
ctrl: !!(bin & (1 << 0)),
shift: !!(bin & (1 << 1)),
alt: !!(bin & (1 << 2))
};
}
private ensureMap(key: KeyCode) { private ensureMap(key: KeyCode) {
if (!this.keyMap.has(key)) { if (!this.keyMap.has(key)) {
this.keyMap.set(key, []); this.keyMap.set(key, []);
@ -270,3 +263,11 @@ export class Hotkey extends EventEmitter<HotkeyEvent> {
return this.list.find(v => v.id === id); 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))
};
}

View File

@ -507,10 +507,14 @@ gameKey.fromJSON(keyStorage.toJSON());
document.addEventListener('keyup', e => { document.addEventListener('keyup', e => {
const assist = generateBinary([e.ctrlKey, e.shiftKey, e.altKey]); const assist = generateBinary([e.ctrlKey, e.shiftKey, e.altKey]);
const code = keycode(e.keyCode); const code = keycode(e.keyCode);
gameKey.emitKey(code, assist, 'up', e); if (gameKey.emitKey(code, assist, 'up', e)) {
e.preventDefault();
}
}); });
document.addEventListener('keydown', e => { document.addEventListener('keydown', e => {
const assist = generateBinary([e.ctrlKey, e.shiftKey, e.altKey]); const assist = generateBinary([e.ctrlKey, e.shiftKey, e.altKey]);
const code = keycode(e.keyCode); const code = keycode(e.keyCode);
gameKey.emitKey(code, assist, 'down', e); if (gameKey.emitKey(code, assist, 'down', e)) {
e.preventDefault();
}
}); });