From 79e64c4cc2741035749dac52789d9039021dd0fa Mon Sep 17 00:00:00 2001 From: Philipp Kunz Date: Thu, 21 Apr 2022 23:52:45 +0200 Subject: [PATCH] fix(core): update --- ts/domtools.classes.keyboard.ts | 41 ++++++++------------------------- 1 file changed, 10 insertions(+), 31 deletions(-) diff --git a/ts/domtools.classes.keyboard.ts b/ts/domtools.classes.keyboard.ts index 144d7c8..9c87f45 100644 --- a/ts/domtools.classes.keyboard.ts +++ b/ts/domtools.classes.keyboard.ts @@ -140,12 +140,9 @@ export class Keyboard { public keyEnum = Key; - public on(keys: Key[] | KeyCombo[]) { - const combos = this.toCombos(keys); + public on(keys: Key[]) { const subject = new plugins.smartrx.rxjs.Subject(); - combos.forEach((combo) => { - this.registerComboCallback(combo, subject); - }); + this.registerKeys(keys, subject); return subject; } @@ -168,8 +165,8 @@ export class Keyboard { private handleKeyDown = (event: KeyboardEvent) => { this.pressedKeys.add(event.keyCode); - this.mapCombosToHandlers.forEach((subjectArg, comboArg) => { - if (this.isComboPressed(comboArg)) { + this.mapCombosToHandlers.forEach((subjectArg, keysArg) => { + if (this.areAllKeysPressed(keysArg)) { subjectArg.next(event); } }); @@ -179,7 +176,7 @@ export class Keyboard { this.pressedKeys.delete(event.keyCode); }; - private isComboPressed(combo: number[]) { + private areAllKeysPressed(combo: number[]) { let result = true; combo.forEach((key) => { @@ -191,33 +188,15 @@ export class Keyboard { return result; } - private registerComboCallback( - comboArg: Array, + private registerKeys( + keysArg: Array, subjectArg: plugins.smartrx.rxjs.Subject ) { - if (!this.mapCombosToHandlers.has(comboArg)) { - this.mapCombosToHandlers.set(comboArg, subjectArg); + if (!this.mapCombosToHandlers.has(keysArg)) { + this.mapCombosToHandlers.set(keysArg, subjectArg); } else { - const subject = this.mapCombosToHandlers.get(comboArg); + const subject = this.mapCombosToHandlers.get(keysArg); return subject; } } - - private toCombos(keys: KeyCombo[] | Key[]) { - if (keys.length === 0) { - return []; - } - - const isKeys = !Array.isArray(keys[0]); - let combos: KeyCombo[] = []; - - if (isKeys) { - combos = (keys as Key[]).map((key) => [key]); - } else { - combos = keys as KeyCombo[]; - combos = combos.filter((combo) => combo.length > 0); - } - - return combos; - } }