1
1
import type { MaybeRefOrGetter } from 'vue'
2
- import { onMounted , ref , toValue , watch } from 'vue'
2
+ import { onMounted , ref , toRef , toValue , watch } from 'vue'
3
3
import type { KeyFilter , KeyPredicate } from '@vueuse/core'
4
4
import { onKeyStroke , useEventListener } from '@vueuse/core'
5
- import { useWindow } from './useWindow'
6
5
7
6
export interface UseKeyPressOptions {
8
7
actInsideInputWithModifier ?: MaybeRefOrGetter < boolean >
9
- target ?: MaybeRefOrGetter < EventTarget >
8
+ target ?: MaybeRefOrGetter < EventTarget | null | undefined >
10
9
}
11
10
12
11
export function isInputDOMNode ( event : KeyboardEvent ) : boolean {
@@ -69,19 +68,18 @@ function useKeyOrCode(code: string, keysToWatch: string | string[]) {
69
68
return keysToWatch . includes ( code ) ? 'code' : 'key'
70
69
}
71
70
72
- const window = useWindow ( )
73
-
74
71
/**
75
72
* Composable that returns a boolean value if a key is pressed
76
73
*
77
74
* @public
78
75
* @param keyFilter - Can be a boolean, a string, an array of strings or a function that returns a boolean. If it's a boolean, it will act as if the key is always pressed. If it's a string, it will return true if a key matching that string is pressed. If it's an array of strings, it will return true if any of the strings match a key being pressed, or a combination (e.g. ['ctrl+a', 'ctrl+b'])
79
76
* @param options - Options object
80
77
*/
81
- export function useKeyPress (
82
- keyFilter : MaybeRefOrGetter < KeyFilter | null > ,
83
- options : UseKeyPressOptions = { actInsideInputWithModifier : true , target : window } ,
84
- ) {
78
+ export function useKeyPress ( keyFilter : MaybeRefOrGetter < KeyFilter | null > , options ?: UseKeyPressOptions ) {
79
+ const actInsideInputWithModifier = toRef ( ( ) => toValue ( options ?. actInsideInputWithModifier ) ?? false )
80
+
81
+ const target = toRef ( ( ) => toValue ( options ?. target ) ?? window )
82
+
85
83
const isPressed = ref ( toValue ( keyFilter ) === true )
86
84
87
85
let modifierPressed = false
@@ -114,7 +112,7 @@ export function useKeyPress(
114
112
( e ) => {
115
113
modifierPressed = wasModifierPressed ( e )
116
114
117
- const preventAction = ( ! modifierPressed || ( modifierPressed && ! options . actInsideInputWithModifier ) ) && isInputDOMNode ( e )
115
+ const preventAction = ( ! modifierPressed || ( modifierPressed && ! actInsideInputWithModifier . value ) ) && isInputDOMNode ( e )
118
116
119
117
if ( preventAction ) {
120
118
return
@@ -124,14 +122,14 @@ export function useKeyPress(
124
122
125
123
isPressed . value = true
126
124
} ,
127
- { eventName : 'keydown' , target : options . target } ,
125
+ { eventName : 'keydown' , target } ,
128
126
)
129
127
130
128
onKeyStroke (
131
129
( ...args ) => currentFilter ( ...args ) ,
132
130
( e ) => {
133
131
if ( isPressed . value ) {
134
- const preventAction = ( ! modifierPressed || ( modifierPressed && ! options . actInsideInputWithModifier ) ) && isInputDOMNode ( e )
132
+ const preventAction = ( ! modifierPressed || ( modifierPressed && ! actInsideInputWithModifier . value ) ) && isInputDOMNode ( e )
135
133
136
134
if ( preventAction ) {
137
135
return
@@ -140,7 +138,7 @@ export function useKeyPress(
140
138
reset ( )
141
139
}
142
140
} ,
143
- { eventName : 'keyup' , target : options . target } ,
141
+ { eventName : 'keyup' , target } ,
144
142
)
145
143
146
144
function reset ( ) {
0 commit comments