diff --git a/packages/keystrokes/package.json b/packages/keystrokes/package.json index 825f3ee..cc5ee29 100644 --- a/packages/keystrokes/package.json +++ b/packages/keystrokes/package.json @@ -1,6 +1,6 @@ { "name": "@rwh/keystrokes", - "version": "1.5.3", + "version": "1.5.4", "description": "Keystrokes is an easy to use library for binding functions to keys and key combos. It can be used with any TypeScript or JavaScript project, even in non-browser environments.", "type": "module", "main": "dist/keystrokes.umd.cjs", diff --git a/packages/keystrokes/src/keystrokes.ts b/packages/keystrokes/src/keystrokes.ts index 544d301..ea707a6 100644 --- a/packages/keystrokes/src/keystrokes.ts +++ b/packages/keystrokes/src/keystrokes.ts @@ -315,7 +315,11 @@ export class Keystrokes< private _handleKeyPress(event: KeyEvent) { if (!this._isActive) return - event = { ...event, key: event.key.toLowerCase() } + event = { + ...event, + key: event.key.toLowerCase(), + aliases: event.aliases?.map((a) => a.toLowerCase()) ?? [], + } const remappedKey = this._keyRemap[event.key] if (remappedKey) event.key = remappedKey @@ -351,7 +355,11 @@ export class Keystrokes< } private _handleKeyRelease(event: KeyEvent) { - event = { ...event, key: event.key.toLowerCase() } + event = { + ...event, + key: event.key.toLowerCase(), + aliases: event.aliases?.map((a) => a.toLowerCase()) ?? [], + } const remappedKey = this._keyRemap[event.key] if (remappedKey) event.key = remappedKey diff --git a/packages/keystrokes/src/tests/keystrokes.spec.ts b/packages/keystrokes/src/tests/keystrokes.spec.ts index d28e180..fa75f3e 100644 --- a/packages/keystrokes/src/tests/keystrokes.spec.ts +++ b/packages/keystrokes/src/tests/keystrokes.spec.ts @@ -392,7 +392,7 @@ describe('new Keystrokes(options)', () => { keystrokes.release({ key: 'd' }) const event = handler.onPressed.mock.calls[0][0] as KeyComboEvent< - KeyboardEvent, + keyboardEvent, BrowserKeyEventProps, BrowserKeyComboEventProps > @@ -425,7 +425,7 @@ describe('new Keystrokes(options)', () => { keystrokes.release({ key: 'd' }) const event = handler.onPressed.mock.calls[0][0] as KeyComboEvent< - KeyboardEvent, + keyboardEvent, BrowserKeyEventProps, BrowserKeyComboEventProps > @@ -496,6 +496,57 @@ describe('new Keystrokes(options)', () => { expect(handler.onPressed).toBeCalledTimes(1) }) + + it.only('accepts a key combo made up of aliases and when that combo is satisfied the given handler is executed', () => { + const keystrokes = createTestKeystrokes() + + const handler1 = { + onPressed: vi.fn(), + onPressedWithRepeat: vi.fn(), + onReleased: vi.fn(), + } + const handler2 = { + onPressed: vi.fn(), + onPressedWithRepeat: vi.fn(), + onReleased: vi.fn(), + } + keystrokes.bindKeyCombo('@keya,@keyb>@keyc+@keyd', handler1) + keystrokes.bindKeyCombo('@keya,@keyb>@keyc+@keyd', handler2) + + keystrokes.press({ key: 'a', aliases: ['@keya'] }) + keystrokes.press({ key: 'a', aliases: ['@keya'] }) + keystrokes.release({ key: 'a', aliases: ['@keya'] }) + + keystrokes.press({ key: 'b', aliases: ['@keyb'] }) + keystrokes.press({ key: 'b', aliases: ['@keyb'] }) + keystrokes.press({ key: 'd', aliases: ['@keyd'] }) + keystrokes.press({ key: 'd', aliases: ['@keyd'] }) + keystrokes.press({ key: 'c', aliases: ['@keyc'] }) + keystrokes.press({ key: 'c', aliases: ['@keyc'] }) + keystrokes.release({ key: 'b', aliases: ['@keyb'] }) + keystrokes.release({ key: 'c', aliases: ['@keyc'] }) + keystrokes.release({ key: 'd', aliases: ['@keyd'] }) + + expect(handler1.onPressed).toBeCalledTimes(1) + expect(handler1.onPressedWithRepeat).toBeCalledTimes(2) + expect(handler1.onReleased).toBeCalledTimes(1) + expect(handler2.onPressed).toBeCalledTimes(1) + expect(handler2.onPressedWithRepeat).toBeCalledTimes(2) + expect(handler2.onReleased).toBeCalledTimes(1) + + expect(handler1.onPressed).toBeCalledWith( + expect.objectContaining({ + finalKeyEvent: expect.objectContaining({ key: 'c' }), + keyCombo: '@keya,@keyb>@keyc+@keyd', + keyEvents: expect.arrayContaining([ + expect.objectContaining({ key: 'a' }), + expect.objectContaining({ key: 'b' }), + expect.objectContaining({ key: 'c' }), + expect.objectContaining({ key: 'd' }), + ]), + }), + ) + }) }) describe('#unbindKeyCombo(keyCombo, handler?)', () => { diff --git a/packages/react-keystrokes/package.json b/packages/react-keystrokes/package.json index 7b02ca0..d14a294 100644 --- a/packages/react-keystrokes/package.json +++ b/packages/react-keystrokes/package.json @@ -1,6 +1,6 @@ { "name": "@rwh/react-keystrokes", - "version": "1.5.3", + "version": "1.5.4", "description": "React bindings for Keystrokes, an easy to use library for binding functions to keys and key combos. It can be used with any TypeScript or JavaScript project, even in non-browser environments.", "type": "module", "main": "dist/react-keystrokes.umd.cjs", diff --git a/packages/vue-keystrokes/package.json b/packages/vue-keystrokes/package.json index f01def2..d66559e 100644 --- a/packages/vue-keystrokes/package.json +++ b/packages/vue-keystrokes/package.json @@ -1,6 +1,6 @@ { "name": "@rwh/vue-keystrokes", - "version": "1.5.3", + "version": "1.5.4", "description": "Vue 3 bindings for Keystrokes, an easy to use library for binding functions to keys and key combos. It can be used with any TypeScript or JavaScript project, even in non-browser environments.", "type": "module", "main": "./dist/vue-keystrokes.umd.cjs",