@@ -2,7 +2,7 @@ import { ref, watch } from 'vue'
2
2
import type { KeyFilter , KeyPredicate , MaybeRefOrGetter } from '@vueuse/core'
3
3
import { onKeyStroke , toValue , useEventListener } from '@vueuse/core'
4
4
import { useWindow } from './useWindow'
5
- import { isBoolean , isFunction } from '~/utils'
5
+ import { isBoolean , isFunction , isString } from '~/utils'
6
6
7
7
export function isInputDOMNode ( event : KeyboardEvent ) : boolean {
8
8
const target = ( event . composedPath ?.( ) ?. [ 0 ] || event . target ) as HTMLElement
@@ -20,18 +20,27 @@ function wasModifierPressed(event: KeyboardEvent) {
20
20
return event . ctrlKey || event . metaKey || event . shiftKey
21
21
}
22
22
23
- function createKeyPredicate ( keyFilter : string [ ] , pressedKeys : Set < string > ) : KeyPredicate {
24
- return ( event : KeyboardEvent ) =>
25
- keyFilter . some ( ( key ) => {
26
- const keyCombination = key . split ( '+' ) . map ( ( k ) => k . trim ( ) . toLowerCase ( ) )
23
+ function isKeyMatch ( pressedKey : string , keyToMatch : string , pressedKeys : Set < string > ) {
24
+ const keyCombination = keyToMatch . split ( '+' ) . map ( ( k ) => k . trim ( ) . toLowerCase ( ) )
27
25
28
- if ( keyCombination . length === 1 ) {
29
- return event . key === key
30
- } else {
31
- pressedKeys . add ( event . key . toLowerCase ( ) )
32
- return keyCombination . every ( ( key ) => pressedKeys . has ( key ) )
33
- }
34
- } )
26
+ if ( keyCombination . length === 1 ) {
27
+ return pressedKey === keyToMatch
28
+ } else {
29
+ pressedKeys . add ( pressedKey . toLowerCase ( ) )
30
+ return keyCombination . every ( ( key ) => pressedKeys . has ( key ) )
31
+ }
32
+ }
33
+
34
+ function createKeyPredicate ( keyFilter : string | string [ ] , pressedKeys : Set < string > ) : KeyPredicate {
35
+ return ( event : KeyboardEvent ) => {
36
+ // if the keyFilter is an array of multiple keys, we need to check each possible key combination
37
+ if ( Array . isArray ( keyFilter ) ) {
38
+ return keyFilter . some ( ( key ) => isKeyMatch ( event . key , key , pressedKeys ) )
39
+ }
40
+
41
+ // if the keyFilter is a string, we need to check if the key matches the string
42
+ return isKeyMatch ( event . key , keyFilter , pressedKeys )
43
+ }
35
44
}
36
45
37
46
/**
@@ -67,7 +76,7 @@ export function useKeyPress(keyFilter: MaybeRefOrGetter<KeyFilter | null>, onCha
67
76
return
68
77
}
69
78
70
- if ( Array . isArray ( unrefKeyFilter ) ) {
79
+ if ( Array . isArray ( unrefKeyFilter ) || ( isString ( unrefKeyFilter ) && unrefKeyFilter . includes ( '+' ) ) ) {
71
80
unrefKeyFilter = createKeyPredicate ( unrefKeyFilter , pressedKeys )
72
81
}
73
82
0 commit comments