Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions packages/keystrokes/src/key-combo-state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,10 @@ export class KeyComboState<OriginalEvent, KeyEventProps, KeyComboEventProps> {
return this._sequenceIndex
}

get sequenceLength() {
return this._parsedKeyCombo.length;
}

private _normalizedKeyCombo: string
private _parsedKeyCombo: string[][][]
private _handlerState: HandlerState<
Expand Down
42 changes: 38 additions & 4 deletions packages/keystrokes/src/keystrokes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,11 @@ export class Keystrokes<
KeyComboState<OriginalEvent, KeyEventProps, KeyComboEventProps>
>

private _keyCombosPressedByKey: Map<
string,
KeyComboState<OriginalEvent, KeyEventProps, KeyComboEventProps>[]
>

constructor(
options: KeystrokesOptions<
OriginalEvent,
Expand All @@ -125,6 +130,7 @@ export class Keystrokes<
this._activeKeyMap = new Map()

this._watchedKeyComboStates = {}
this._keyCombosPressedByKey = new Map()

this.bindEnvironment(options)
}
Expand Down Expand Up @@ -357,8 +363,31 @@ export class Keystrokes<

this._updateKeyComboStates()

for (const keyComboState of this._keyComboStatesArray)
keyComboState.executePressed(event)
const pressedCombos = this._keyComboStatesArray.filter(
(combo) => combo.isPressed,
)

if (pressedCombos.length > 0) {
const maxSequenceLength = Math.max(
...pressedCombos.map((combo) => combo.sequenceLength),
)

const activatedCombos: KeyComboState<
OriginalEvent,
KeyEventProps,
KeyComboEventProps
>[] = []
for (const combo of pressedCombos) {
if (combo.sequenceLength === maxSequenceLength) {
combo.executePressed(event)
activatedCombos.push(combo)
}
}

if (activatedCombos.length > 0) {
this._keyCombosPressedByKey.set(event.key, activatedCombos)
}
}
}

private _handleKeyRelease(event: KeyEvent<OriginalEvent, KeyEventProps>) {
Expand Down Expand Up @@ -402,8 +431,13 @@ export class Keystrokes<
this._tryReleaseSelfReleasingKeys()
this._updateKeyComboStates()

for (const keyComboState of this._keyComboStatesArray)
keyComboState.executeReleased(event)
const activatedCombos = this._keyCombosPressedByKey.get(event.key)
if (activatedCombos) {
for (const combo of activatedCombos) {
combo.executeReleased(event)
}
this._keyCombosPressedByKey.delete(event.key)
}
}

private _updateKeyComboStates() {
Expand Down