-
-
Notifications
You must be signed in to change notification settings - Fork 316
/
Copy pathuseKeyPress.ts
192 lines (145 loc) · 6.09 KB
/
useKeyPress.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
import type { MaybeRefOrGetter } from 'vue'
import { ref, toRef, toValue, watch } from 'vue'
import type { KeyFilter, KeyPredicate } from '@vueuse/core'
import { onKeyStroke, useEventListener } from '@vueuse/core'
type PressedKeys = Set<string>
type KeyOrCode = 'key' | 'code'
export interface UseKeyPressOptions {
target?: MaybeRefOrGetter<EventTarget | null | undefined>
actInsideInputWithModifier?: MaybeRefOrGetter<boolean>
preventDefault?: MaybeRefOrGetter<boolean>
}
const inputTags = ['INPUT', 'SELECT', 'TEXTAREA']
export function isInputDOMNode(event: KeyboardEvent): boolean {
const target = (event.composedPath?.()?.[0] || event.target) as HTMLElement
const hasAttribute = typeof target?.hasAttribute === 'function' ? target.hasAttribute('contenteditable') : false
const closest = typeof target?.closest === 'function' ? target.closest('.nokey') : null
// when an input field is focused we don't want to trigger deletion or movement of nodes
return inputTags.includes(target?.nodeName) || hasAttribute || !!closest
}
// we want to be able to do a multi selection event if we are in an input field
function wasModifierPressed(event: KeyboardEvent) {
return event.ctrlKey || event.metaKey || event.shiftKey || event.altKey
}
function isKeyMatch(pressedKey: string, keyToMatch: string, pressedKeys: Set<string>, isKeyUp: boolean) {
const keyCombination = keyToMatch
.replace('+', '\n')
.replace('\n\n', '\n+')
.split('\n')
.map((k) => k.trim().toLowerCase())
if (keyCombination.length === 1) {
return pressedKey.toLowerCase() === keyToMatch.toLowerCase()
}
// we need to remove the key *after* checking for a match otherwise a combination like 'shift+a' would never get unmatched/reset
if (!isKeyUp) {
pressedKeys.add(pressedKey.toLowerCase())
}
const isMatch = keyCombination.every(
(key, index) => pressedKeys.has(key) && Array.from(pressedKeys.values())[index] === keyCombination[index],
)
if (isKeyUp) {
pressedKeys.delete(pressedKey.toLowerCase())
}
return isMatch
}
function createKeyPredicate(keyFilter: string | string[], pressedKeys: PressedKeys): KeyPredicate {
return (event: KeyboardEvent) => {
if (!event.code && !event.key) {
return false
}
const keyOrCode = useKeyOrCode(event.code, keyFilter)
// 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[keyOrCode], key, pressedKeys, event.type === 'keyup'))
}
// if the keyFilter is a string, we need to check if the key matches the string
return isKeyMatch(event[keyOrCode], keyFilter, pressedKeys, event.type === 'keyup')
}
}
function useKeyOrCode(code: string, keysToWatch: string | string[]): KeyOrCode {
return keysToWatch.includes(code) ? 'code' : 'key'
}
/**
* Composable that returns a boolean value if a key is pressed
*
* @public
* @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'])
* @param options - Options object
*/
export function useKeyPress(keyFilter: MaybeRefOrGetter<KeyFilter | boolean | null>, options?: UseKeyPressOptions) {
const actInsideInputWithModifier = toRef(() => toValue(options?.actInsideInputWithModifier) ?? false)
const target = toRef(() => toValue(options?.target) ?? window)
const preventDefault = toRef(() => toValue(options?.preventDefault) ?? true)
const isPressed = ref(toValue(keyFilter) === true)
let modifierPressed = false
const pressedKeys = new Set<string>()
let currentFilter = createKeyFilterFn(toValue(keyFilter))
watch(
() => toValue(keyFilter),
(nextKeyFilter, previousKeyFilter) => {
// if the previous keyFilter was a boolean but is now something else, we need to reset the isPressed value
if (typeof previousKeyFilter === 'boolean' && typeof nextKeyFilter !== 'boolean') {
reset()
}
currentFilter = createKeyFilterFn(nextKeyFilter)
},
{
immediate: true,
},
)
useEventListener(['blur', 'contextmenu'], reset)
onKeyStroke(
(...args) => currentFilter(...args),
(e) => {
modifierPressed = wasModifierPressed(e)
const preventAction = (!modifierPressed || (modifierPressed && !actInsideInputWithModifier.value)) && isInputDOMNode(e)
if (preventAction) {
return
}
const target = (e.composedPath?.()?.[0] || e.target) as Element | null
const isInteractiveElement = target?.nodeName === 'BUTTON' || target?.nodeName === 'A'
if (!preventDefault.value && (modifierPressed || !isInteractiveElement)) {
e.preventDefault()
}
isPressed.value = true
},
{ eventName: 'keydown', target },
)
onKeyStroke(
(...args) => currentFilter(...args),
(e) => {
if (isPressed.value) {
const preventAction = (!modifierPressed || (modifierPressed && !actInsideInputWithModifier.value)) && isInputDOMNode(e)
if (preventAction) {
return
}
modifierPressed = false
isPressed.value = false
}
},
{ eventName: 'keyup', target },
)
function reset() {
modifierPressed = false
pressedKeys.clear()
isPressed.value = toValue(keyFilter) === true
}
function createKeyFilterFn(keyFilter: KeyFilter | boolean | null) {
// if the keyFilter is null, we just set the isPressed value to false
if (keyFilter === null) {
reset()
return () => false
}
// if the keyFilter is a boolean, we just set the isPressed value to that boolean
if (typeof keyFilter === 'boolean') {
reset()
isPressed.value = keyFilter
return () => false
}
if (Array.isArray(keyFilter) || typeof keyFilter === 'string') {
return createKeyPredicate(keyFilter, pressedKeys)
}
return keyFilter
}
return isPressed
}