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 1ea1e92
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 7 deletions.
2 changes: 1 addition & 1 deletion packages/keystrokes/package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
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
2 changes: 1 addition & 1 deletion packages/react-keystrokes/package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
2 changes: 1 addition & 1 deletion packages/vue-keystrokes/package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down

0 comments on commit 1ea1e92

Please sign in to comment.