Skip to content

Commit f5dc568

Browse files
authored
fix: 修复了在 Windows 平台上按键无法释放的问题 (#401)
1 parent 9f0662c commit f5dc568

File tree

1 file changed

+21
-7
lines changed

1 file changed

+21
-7
lines changed

src/composables/useDevice.ts

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import type { Ref } from 'vue'
22

33
import { readDir } from '@tauri-apps/plugin-fs'
4-
import { useDebounceFn } from '@vueuse/core'
54
import { uniq } from 'es-toolkit'
65
import { reactive, ref, watch } from 'vue'
76

@@ -13,6 +12,7 @@ import { useCatStore } from '@/stores/cat'
1312
import { useModelStore } from '@/stores/model'
1413
import { isImage } from '@/utils/is'
1514
import { join } from '@/utils/path'
15+
import { isWindows } from '@/utils/platform'
1616

1717
interface MouseButtonEvent {
1818
kind: 'MousePress' | 'MouseRelease'
@@ -39,13 +39,13 @@ type DeviceEvent = MouseButtonEvent | MouseMoveEvent | KeyboardEvent
3939
export function useDevice() {
4040
const supportLeftKeys = ref<string[]>([])
4141
const supportRightKeys = ref<string[]>([])
42-
4342
const pressedMouses = ref<string[]>([])
4443
const mousePosition = reactive<MouseMoveValue>({ x: 0, y: 0 })
4544
const pressedLeftKeys = ref<string[]>([])
4645
const pressedRightKeys = ref<string[]>([])
4746
const catStore = useCatStore()
4847
const modelStore = useModelStore()
48+
const releaseTimers = new Map<string, NodeJS.Timeout>()
4949

5050
watch(() => modelStore.currentModel, async (model) => {
5151
if (!model) return
@@ -85,10 +85,6 @@ export function useDevice() {
8585
}
8686
}, { deep: true, immediate: true })
8787

88-
const debouncedHandleRelease = useDebounceFn((array: Ref<string[]>, key) => {
89-
handleRelease(array, key)
90-
}, 100)
91-
9288
const handlePress = (array: Ref<string[]>, value?: string) => {
9389
if (!value) return
9490

@@ -130,6 +126,20 @@ export function useDevice() {
130126
}
131127
}
132128

129+
const handleScheduleRelease = (keys: Ref<string[]>, key: string, delay = 500) => {
130+
if (releaseTimers.has(key)) {
131+
clearTimeout(releaseTimers.get(key))
132+
}
133+
134+
const timer = setTimeout(() => {
135+
handleRelease(keys, key)
136+
137+
releaseTimers.delete(key)
138+
}, delay)
139+
140+
releaseTimers.set(key, timer)
141+
}
142+
133143
useTauriListen<DeviceEvent>(LISTEN_KEY.DEVICE_CHANGED, ({ payload }) => {
134144
const { kind, value } = payload
135145

@@ -145,10 +155,14 @@ export function useDevice() {
145155
if (nextValue === 'CapsLock') {
146156
handlePress(pressedKeys, nextValue)
147157

148-
return debouncedHandleRelease(pressedKeys, nextValue)
158+
return handleScheduleRelease(pressedKeys, nextValue, 100)
149159
}
150160

151161
if (kind === 'KeyboardPress') {
162+
if (isWindows) {
163+
handleScheduleRelease(pressedKeys, nextValue)
164+
}
165+
152166
return handlePress(pressedKeys, nextValue)
153167
}
154168

0 commit comments

Comments
 (0)