update
This commit is contained in:
parent
44c3f6e149
commit
6f06337aee
@ -131,7 +131,6 @@ export class HotKeyManager<HotkeyConfig extends Record<string, HotkeyEntry<strin
|
|||||||
if (this.ignoreFormElements && HotKeyManager.isFormElement(e.target)) {
|
if (this.ignoreFormElements && HotKeyManager.isFormElement(e.target)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
this.keyPressedMap.set(e.key.toLowerCase(), true);
|
this.keyPressedMap.set(e.key.toLowerCase(), true);
|
||||||
this.checkHotkeys(e);
|
this.checkHotkeys(e);
|
||||||
});
|
});
|
||||||
@ -197,10 +196,21 @@ export class HotKeyManager<HotkeyConfig extends Record<string, HotkeyEntry<strin
|
|||||||
}, {} as HotkeyPressedMap<HotkeyConfig>);
|
}, {} as HotkeyPressedMap<HotkeyConfig>);
|
||||||
}
|
}
|
||||||
|
|
||||||
private triggerListenerFor(key: keyof HotkeyConfig, event: KeyboardEvent) {
|
private triggerListenerFor(
|
||||||
|
key: keyof HotkeyConfig,
|
||||||
|
event: KeyboardEvent,
|
||||||
|
oldKeyMap: HotkeyPressedMap<HotkeyConfig>[keyof HotkeyConfig],
|
||||||
|
isRepeated: boolean,
|
||||||
|
) {
|
||||||
const pressedEntry = this.hotKeysPressedMap[key];
|
const pressedEntry = this.hotKeysPressedMap[key];
|
||||||
this.hotKeys[key].callbacks.forEach((callback) =>
|
this.hotKeys[key].callbacks.forEach((callback) =>
|
||||||
callback({ event, subKeys: pressedEntry.subKeys, isPressed: pressedEntry.isPressed }),
|
callback({
|
||||||
|
event,
|
||||||
|
subKeys: pressedEntry.subKeys,
|
||||||
|
isPressed: pressedEntry.isPressed,
|
||||||
|
previous: oldKeyMap,
|
||||||
|
isRepeated,
|
||||||
|
}),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -223,19 +233,17 @@ export class HotKeyManager<HotkeyConfig extends Record<string, HotkeyEntry<strin
|
|||||||
|
|
||||||
// Check for repeated pressed. Nothing changed, but key is pressed again
|
// Check for repeated pressed. Nothing changed, but key is pressed again
|
||||||
const eventKey = event.key.toLowerCase();
|
const eventKey = event.key.toLowerCase();
|
||||||
hotkeysToCheckAffection.forEach(([key]) => {
|
const hotkeysToTroggerAgain = hotkeysToCheckAffection.filter(([key]) => {
|
||||||
const keyDefinition = this.hotKeys[key];
|
const keyDefinition = this.hotKeys[key];
|
||||||
if (HotKeyManager.checkAffections(keyDefinition.keys, eventKey)) {
|
if (HotKeyManager.checkAffections(keyDefinition.keys, eventKey)) {
|
||||||
hotkeysToTrigger.push(key);
|
return true;
|
||||||
} else {
|
|
||||||
ObjectHelper.values(keyDefinition.subKeys).forEach((subKeyDefinitions) => {
|
|
||||||
if (HotKeyManager.checkAffections(subKeyDefinitions, eventKey)) {
|
|
||||||
hotkeysToTrigger.push(key);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
return ObjectHelper.values(keyDefinition.subKeys).some((subKeyDefinitions) => {
|
||||||
|
return HotKeyManager.checkAffections(subKeyDefinitions, eventKey);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
hotkeysToTrigger.forEach((key) => this.triggerListenerFor(key, event));
|
hotkeysToTrigger.forEach((key) => this.triggerListenerFor(key, event, oldMap[key], false));
|
||||||
|
hotkeysToTroggerAgain.forEach(([key]) => this.triggerListenerFor(key, event, oldMap[key], true));
|
||||||
}
|
}
|
||||||
|
|
||||||
private clearMap(event: KeyboardEvent) {
|
private clearMap(event: KeyboardEvent) {
|
||||||
|
|||||||
@ -2,6 +2,11 @@ export type HotkeyListenerEvent<SubKeys extends string | symbol | number> = {
|
|||||||
event: KeyboardEvent;
|
event: KeyboardEvent;
|
||||||
subKeys: Record<SubKeys, boolean>;
|
subKeys: Record<SubKeys, boolean>;
|
||||||
isPressed: boolean;
|
isPressed: boolean;
|
||||||
|
previous: {
|
||||||
|
subKeys: Record<SubKeys, boolean>;
|
||||||
|
isPressed: boolean;
|
||||||
|
};
|
||||||
|
isRepeated: boolean;
|
||||||
};
|
};
|
||||||
|
|
||||||
export type HotkeyListener<SubKeys extends string | symbol | number> = (ev: HotkeyListenerEvent<SubKeys>) => unknown;
|
export type HotkeyListener<SubKeys extends string | symbol | number> = (ev: HotkeyListenerEvent<SubKeys>) => unknown;
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user