Skip to content

Commit

Permalink
Fix issue with key aliases not working with checkKey and bindKey
Browse files Browse the repository at this point in the history
  • Loading branch information
RobertWHurst committed Mar 14, 2024
1 parent fa2ff45 commit 2008c9f
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 6 deletions.
24 changes: 18 additions & 6 deletions packages/keystrokes/src/keystrokes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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)
Expand Down
30 changes: 30 additions & 0 deletions packages/keystrokes/src/tests/keystrokes.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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?)', () => {
Expand Down Expand Up @@ -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)', () => {
Expand Down

0 comments on commit 2008c9f

Please sign in to comment.