From f095917e97efa739cb58d987ca2c23aa00689476 Mon Sep 17 00:00:00 2001 From: braks <78412429+bcakmakoglu@users.noreply.github.com> Date: Fri, 18 Aug 2023 17:37:46 +0200 Subject: [PATCH] fix(core): clear key on keyup so combinations work during simultaneous keypress --- packages/core/src/composables/useKeyPress.ts | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/packages/core/src/composables/useKeyPress.ts b/packages/core/src/composables/useKeyPress.ts index 28e396e3..71a727a1 100644 --- a/packages/core/src/composables/useKeyPress.ts +++ b/packages/core/src/composables/useKeyPress.ts @@ -20,26 +20,35 @@ function wasModifierPressed(event: KeyboardEvent) { return event.ctrlKey || event.metaKey || event.shiftKey } -function isKeyMatch(pressedKey: string, keyToMatch: string, pressedKeys: Set) { +function isKeyMatch(pressedKey: string, keyToMatch: string, pressedKeys: Set, isKeyUp: boolean) { const keyCombination = keyToMatch.split('+').map((k) => k.trim().toLowerCase()) if (keyCombination.length === 1) { return pressedKey === keyToMatch } else { - pressedKeys.add(pressedKey.toLowerCase()) - return keyCombination.every((key) => pressedKeys.has(key)) + if (isKeyUp) { + 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): KeyPredicate { 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 (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 - return isKeyMatch(event.key, keyFilter, pressedKeys) + return isKeyMatch(event.key, keyFilter, pressedKeys, event.type === 'keyup') } }