Skip to content

Commit f095917

Browse files
committed
fix(core): clear key on keyup so combinations work during simultaneous keypress
1 parent a57946a commit f095917

File tree

1 file changed

+14
-5
lines changed

1 file changed

+14
-5
lines changed

packages/core/src/composables/useKeyPress.ts

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,26 +20,35 @@ function wasModifierPressed(event: KeyboardEvent) {
2020
return event.ctrlKey || event.metaKey || event.shiftKey
2121
}
2222

23-
function isKeyMatch(pressedKey: string, keyToMatch: string, pressedKeys: Set<string>) {
23+
function isKeyMatch(pressedKey: string, keyToMatch: string, pressedKeys: Set<string>, isKeyUp: boolean) {
2424
const keyCombination = keyToMatch.split('+').map((k) => k.trim().toLowerCase())
2525

2626
if (keyCombination.length === 1) {
2727
return pressedKey === keyToMatch
2828
} else {
29-
pressedKeys.add(pressedKey.toLowerCase())
30-
return keyCombination.every((key) => pressedKeys.has(key))
29+
if (isKeyUp) {
30+
pressedKeys.delete(pressedKey.toLowerCase())
31+
} else {
32+
pressedKeys.add(pressedKey.toLowerCase())
33+
}
34+
35+
return keyCombination.every(
36+
(key, index) => pressedKeys.has(key) && Array.from(pressedKeys.values())[index] === keyCombination[index],
37+
)
3138
}
3239
}
3340

3441
function createKeyPredicate(keyFilter: string | string[], pressedKeys: Set<string>): KeyPredicate {
3542
return (event: KeyboardEvent) => {
43+
console.log(event.type)
44+
3645
// if the keyFilter is an array of multiple keys, we need to check each possible key combination
3746
if (Array.isArray(keyFilter)) {
38-
return keyFilter.some((key) => isKeyMatch(event.key, key, pressedKeys))
47+
return keyFilter.some((key) => isKeyMatch(event.key, key, pressedKeys, event.type === 'keyup'))
3948
}
4049

4150
// if the keyFilter is a string, we need to check if the key matches the string
42-
return isKeyMatch(event.key, keyFilter, pressedKeys)
51+
return isKeyMatch(event.key, keyFilter, pressedKeys, event.type === 'keyup')
4352
}
4453
}
4554

0 commit comments

Comments
 (0)