@@ -20,26 +20,35 @@ function wasModifierPressed(event: KeyboardEvent) {
20
20
return event . ctrlKey || event . metaKey || event . shiftKey
21
21
}
22
22
23
- function isKeyMatch ( pressedKey : string , keyToMatch : string , pressedKeys : Set < string > ) {
23
+ function isKeyMatch ( pressedKey : string , keyToMatch : string , pressedKeys : Set < string > , isKeyUp : boolean ) {
24
24
const keyCombination = keyToMatch . split ( '+' ) . map ( ( k ) => k . trim ( ) . toLowerCase ( ) )
25
25
26
26
if ( keyCombination . length === 1 ) {
27
27
return pressedKey === keyToMatch
28
28
} 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
+ )
31
38
}
32
39
}
33
40
34
41
function createKeyPredicate ( keyFilter : string | string [ ] , pressedKeys : Set < string > ) : KeyPredicate {
35
42
return ( event : KeyboardEvent ) => {
43
+ console . log ( event . type )
44
+
36
45
// if the keyFilter is an array of multiple keys, we need to check each possible key combination
37
46
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' ) )
39
48
}
40
49
41
50
// 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' )
43
52
}
44
53
}
45
54
0 commit comments