mirror of
https://github.com/unanmed/HumanBreak.git
synced 2025-01-19 12:49:25 +08:00
feat: 自定义按键阻止默认行为
This commit is contained in:
parent
168b59ccf3
commit
860f5c9fdb
@ -151,7 +151,7 @@ export class Hotkey extends EventEmitter<HotkeyEvent> {
|
||||
* @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<HotkeyEvent> {
|
||||
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<HotkeyEvent> {
|
||||
}
|
||||
});
|
||||
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) {
|
||||
if (!this.keyMap.has(key)) {
|
||||
this.keyMap.set(key, []);
|
||||
@ -270,3 +263,11 @@ export class Hotkey extends EventEmitter<HotkeyEvent> {
|
||||
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))
|
||||
};
|
||||
}
|
||||
|
@ -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();
|
||||
}
|
||||
});
|
||||
|
Loading…
Reference in New Issue
Block a user