fix(core): clear key on keyup so combinations work during simultaneous keypress

This commit is contained in:
braks
2023-08-21 17:44:20 +02:00
committed by Braks
parent a57946a325
commit f095917e97
+14 -5
View File
@@ -20,26 +20,35 @@ function wasModifierPressed(event: KeyboardEvent) {
return event.ctrlKey || event.metaKey || event.shiftKey return event.ctrlKey || event.metaKey || event.shiftKey
} }
function isKeyMatch(pressedKey: string, keyToMatch: string, pressedKeys: Set<string>) { function isKeyMatch(pressedKey: string, keyToMatch: string, pressedKeys: Set<string>, isKeyUp: boolean) {
const keyCombination = keyToMatch.split('+').map((k) => k.trim().toLowerCase()) const keyCombination = keyToMatch.split('+').map((k) => k.trim().toLowerCase())
if (keyCombination.length === 1) { if (keyCombination.length === 1) {
return pressedKey === keyToMatch return pressedKey === keyToMatch
} else { } else {
pressedKeys.add(pressedKey.toLowerCase()) if (isKeyUp) {
return keyCombination.every((key) => pressedKeys.has(key)) pressedKeys.delete(pressedKey.toLowerCase())
} else {
pressedKeys.add(pressedKey.toLowerCase())
}
return keyCombination.every(
(key, index) => pressedKeys.has(key) && Array.from(pressedKeys.values())[index] === keyCombination[index],
)
} }
} }
function createKeyPredicate(keyFilter: string | string[], pressedKeys: Set<string>): KeyPredicate { function createKeyPredicate(keyFilter: string | string[], pressedKeys: Set<string>): KeyPredicate {
return (event: KeyboardEvent) => { return (event: KeyboardEvent) => {
console.log(event.type)
// if the keyFilter is an array of multiple keys, we need to check each possible key combination // if the keyFilter is an array of multiple keys, we need to check each possible key combination
if (Array.isArray(keyFilter)) { if (Array.isArray(keyFilter)) {
return keyFilter.some((key) => isKeyMatch(event.key, key, pressedKeys)) return keyFilter.some((key) => isKeyMatch(event.key, key, pressedKeys, event.type === 'keyup'))
} }
// if the keyFilter is a string, we need to check if the key matches the string // if the keyFilter is a string, we need to check if the key matches the string
return isKeyMatch(event.key, keyFilter, pressedKeys) return isKeyMatch(event.key, keyFilter, pressedKeys, event.type === 'keyup')
} }
} }