Skip to content

Commit

Permalink
fix caase sensitivity on key codes
Browse files Browse the repository at this point in the history
  • Loading branch information
RobertWHurst committed Dec 20, 2023
1 parent 712d069 commit a5a81dd
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 4 deletions.
12 changes: 10 additions & 2 deletions packages/keystrokes/src/keystrokes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,11 @@ export class Keystrokes<
private _handleKeyPress(event: KeyEvent<OriginalEvent, KeyEventProps>) {
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
Expand Down Expand Up @@ -351,7 +355,11 @@ export class Keystrokes<
}

private _handleKeyRelease(event: KeyEvent<OriginalEvent, KeyEventProps>) {
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
Expand Down
55 changes: 53 additions & 2 deletions packages/keystrokes/src/tests/keystrokes.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
>
Expand Down Expand Up @@ -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
>
Expand Down Expand Up @@ -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?)', () => {
Expand Down

0 comments on commit a5a81dd

Please sign in to comment.