Skip to content

Commit 39e4052

Browse files
authored
fix: 修复 Windows 按键未正确记录的问题 (#575)
1 parent 61d0bd2 commit 39e4052

1 file changed

Lines changed: 4 additions & 21 deletions

File tree

src/composables/useDevice.ts

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type { CursorPoint } from '@/utils/monitor'
22

33
import { invoke } from '@tauri-apps/api/core'
4+
import { useDebounceFn } from '@vueuse/core'
45
import { isEqual, mapValues } from 'es-toolkit'
56
import { ref } from 'vue'
67

@@ -10,7 +11,6 @@ import { useModel } from './useModel'
1011
import { useTauriListen } from './useTauriListen'
1112

1213
import { useModelStore } from '@/stores/model'
13-
import { isWindows } from '@/utils/platform'
1414

1515
interface MouseButtonEvent {
1616
kind: 'MousePress' | 'MouseRelease'
@@ -32,13 +32,14 @@ type DeviceEvent = MouseButtonEvent | MouseMoveEvent | KeyboardEvent
3232
export function useDevice() {
3333
const modelStore = useModelStore()
3434
const lastCursorPoint = ref<CursorPoint>({ x: 0, y: 0 })
35-
const releaseTimers = new Map<string, NodeJS.Timeout>()
3635
const { handlePress, handleRelease, handleMouseChange, handleMouseMove } = useModel()
3736

3837
const startListening = () => {
3938
invoke(INVOKE_KEY.START_DEVICE_LISTENING)
4039
}
4140

41+
const debouncedRelease = useDebounceFn(handleRelease, 100)
42+
4243
const getSupportedKey = (key: string) => {
4344
let nextKey = key
4445

@@ -58,20 +59,6 @@ export function useDevice() {
5859
return nextKey
5960
}
6061

61-
const handleScheduleRelease = (key: string, delay = 500) => {
62-
if (releaseTimers.has(key)) {
63-
clearTimeout(releaseTimers.get(key))
64-
}
65-
66-
const timer = setTimeout(() => {
67-
handleRelease(key)
68-
69-
releaseTimers.delete(key)
70-
}, delay)
71-
72-
releaseTimers.set(key, timer)
73-
}
74-
7562
const processMouseMove = (point: CursorPoint) => {
7663
const roundedValue = mapValues(point, Math.round)
7764

@@ -93,14 +80,10 @@ export function useDevice() {
9380
if (nextValue === 'CapsLock') {
9481
handlePress(nextValue)
9582

96-
return handleScheduleRelease(nextValue, 100)
83+
return debouncedRelease(nextValue)
9784
}
9885

9986
if (kind === 'KeyboardPress') {
100-
if (isWindows) {
101-
handleScheduleRelease(nextValue)
102-
}
103-
10487
return handlePress(nextValue)
10588
}
10689

0 commit comments

Comments
 (0)