Skip to content

Commit

Permalink
ensure combos can end with aliases
Browse files Browse the repository at this point in the history
  • Loading branch information
RobertWHurst committed Dec 20, 2023
1 parent a238c37 commit c998f6a
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
12 changes: 10 additions & 2 deletions packages/keystrokes/src/key-combo-state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,11 @@ export class KeyComboState<OriginalEvent, KeyEventProps, KeyComboEventProps> {
}

executePressed(event: KeyEvent<OriginalEvent, KeyEventProps>) {
if (!this._isPressedWithFinalUnit?.has(event.key)) return
if (
!this._isPressedWithFinalUnit?.has(event.key) &&
!event.aliases?.some((a) => this._isPressedWithFinalUnit?.has(a))
)
return
this._handlerState.executePressed(
this._wrapEvent(this._lastActiveKeyPresses, {
key: event.key,
Expand All @@ -189,7 +193,11 @@ export class KeyComboState<OriginalEvent, KeyEventProps, KeyComboEventProps> {
}

executeReleased(event: KeyEvent<OriginalEvent, KeyEventProps>) {
if (!this._isPressedWithFinalUnit?.has(event.key)) return
if (
!this._isPressedWithFinalUnit?.has(event.key) &&
!event.aliases?.some((a) => this._isPressedWithFinalUnit?.has(a))
)
return
this._handlerState.executeReleased(
this._wrapEvent(this._lastActiveKeyPresses, {
key: event.key,
Expand Down
13 changes: 13 additions & 0 deletions packages/keystrokes/src/tests/keystrokes.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,19 @@ describe('new Keystrokes(options)', () => {
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: 'a,b>c+d',
keyEvents: expect.arrayContaining([
expect.objectContaining({ key: 'a' }),
expect.objectContaining({ key: 'b' }),
expect.objectContaining({ key: 'c' }),
expect.objectContaining({ key: 'd' }),
]),
}),
)
})

it('will not trigger a key combo handler if the keys are pressed in the wrong order', () => {
Expand Down

0 comments on commit c998f6a

Please sign in to comment.