From 2008c9f62dd6172a2e4de7663e13a9b59b2ec084 Mon Sep 17 00:00:00 2001 From: Robert Hurst Date: Thu, 14 Mar 2024 02:34:37 -0700 Subject: [PATCH] Fix issue with key aliases not working with checkKey and bindKey --- packages/keystrokes/src/keystrokes.ts | 24 +++++++++++---- .../keystrokes/src/tests/keystrokes.spec.ts | 30 +++++++++++++++++++ 2 files changed, 48 insertions(+), 6 deletions(-) diff --git a/packages/keystrokes/src/keystrokes.ts b/packages/keystrokes/src/keystrokes.ts index ea707a6..2be325c 100644 --- a/packages/keystrokes/src/keystrokes.ts +++ b/packages/keystrokes/src/keystrokes.ts @@ -234,7 +234,9 @@ export class Keystrokes< } checkKey(key: string) { - return this._activeKeyMap.has(key.toLowerCase()) + return this._activeKeyPresses.some( + (p) => p.key === key || p.aliases.has(key), + ) } checkKeyCombo(keyCombo: string) { @@ -323,17 +325,21 @@ export class Keystrokes< const remappedKey = this._keyRemap[event.key] if (remappedKey) event.key = remappedKey - if (event.aliases) { - for (let i = 0; i < event.aliases.length; i += 1) { - const remappedAlias = this._keyRemap[event.aliases[i]] - if (remappedAlias) event.aliases[i] = remappedAlias - } + for (let i = 0; i < event.aliases!.length; i += 1) { + const remappedAlias = this._keyRemap[event.aliases![i]] + if (remappedAlias) event.aliases![i] = remappedAlias } const keyPressHandlerStates = this._handlerStates[event.key] if (keyPressHandlerStates) { for (const s of keyPressHandlerStates) s.executePressed(event) } + for (let i = 0; i < event.aliases!.length; i += 1) { + const keyPressHandlerStates = this._handlerStates[event.aliases![i]] + if (keyPressHandlerStates) { + for (const s of keyPressHandlerStates) s.executePressed(event) + } + } const existingKeypress = this._activeKeyMap.get(event.key) if (existingKeypress) { @@ -374,6 +380,12 @@ export class Keystrokes< if (keyPressHandlerStates) { for (const s of keyPressHandlerStates) s.executeReleased(event) } + for (let i = 0; i < event.aliases!.length; i += 1) { + const keyPressHandlerStates = this._handlerStates[event.aliases![i]] + if (keyPressHandlerStates) { + for (const s of keyPressHandlerStates) s.executeReleased(event) + } + } if (this._activeKeyMap.has(event.key)) { this._activeKeyMap.delete(event.key) diff --git a/packages/keystrokes/src/tests/keystrokes.spec.ts b/packages/keystrokes/src/tests/keystrokes.spec.ts index ff7c8b8..0bb9c10 100644 --- a/packages/keystrokes/src/tests/keystrokes.spec.ts +++ b/packages/keystrokes/src/tests/keystrokes.spec.ts @@ -173,6 +173,21 @@ describe('new Keystrokes(options)', () => { expect(handler1).toBeCalledTimes(2) expect(handler2).toBeCalledTimes(1) }) + + it('accepts a key and handler which is executed repeatedly while the key is pressed when using aliases', () => { + const keystrokes = createTestKeystrokes() + + const handler1 = vi.fn() + const handler2 = vi.fn() + keystrokes.bindKey('@keya', handler1) + keystrokes.bindKey('@keya', handler2) + + keystrokes.press({ key: 'a', aliases: ['@keya'] }) + keystrokes.press({ key: 'a', aliases: ['@keya'] }) + + expect(handler1).toBeCalledTimes(2) + expect(handler2).toBeCalledTimes(2) + }) }) describe('#unbindKey(keyCombo, handler?)', () => { @@ -586,6 +601,21 @@ describe('new Keystrokes(options)', () => { expect(keystrokes.checkKey('a')).toBe(false) }) + + it('will return a boolean indicating if a key is pressed when using aliases', () => { + const keystrokes = createTestKeystrokes() + + expect(keystrokes.checkKey('@keya')).toBe(false) + + keystrokes.press({ key: 'a', aliases: ['@keya'] }) + keystrokes.press({ key: 'a', aliases: ['@keya'] }) + + expect(keystrokes.checkKey('@keya')).toBe(true) + + keystrokes.release({ key: 'a', aliases: ['@keya'] }) + + expect(keystrokes.checkKey('@keya')).toBe(false) + }) }) describe('#checkKeyCombo(keyCombo)', () => {