fix: 修复 Windows 按键未正确记录的问题 (#575)

This commit is contained in:
ayangweb
2025-07-22 15:19:36 +08:00
committed by GitHub
parent 61d0bd28ad
commit 39e4052b1b

View File

@@ -1,6 +1,7 @@
import type { CursorPoint } from '@/utils/monitor'
import { invoke } from '@tauri-apps/api/core'
import { useDebounceFn } from '@vueuse/core'
import { isEqual, mapValues } from 'es-toolkit'
import { ref } from 'vue'
@@ -10,7 +11,6 @@ import { useModel } from './useModel'
import { useTauriListen } from './useTauriListen'
import { useModelStore } from '@/stores/model'
import { isWindows } from '@/utils/platform'
interface MouseButtonEvent {
kind: 'MousePress' | 'MouseRelease'
@@ -32,13 +32,14 @@ type DeviceEvent = MouseButtonEvent | MouseMoveEvent | KeyboardEvent
export function useDevice() {
const modelStore = useModelStore()
const lastCursorPoint = ref<CursorPoint>({ x: 0, y: 0 })
const releaseTimers = new Map<string, NodeJS.Timeout>()
const { handlePress, handleRelease, handleMouseChange, handleMouseMove } = useModel()
const startListening = () => {
invoke(INVOKE_KEY.START_DEVICE_LISTENING)
}
const debouncedRelease = useDebounceFn(handleRelease, 100)
const getSupportedKey = (key: string) => {
let nextKey = key
@@ -58,20 +59,6 @@ export function useDevice() {
return nextKey
}
const handleScheduleRelease = (key: string, delay = 500) => {
if (releaseTimers.has(key)) {
clearTimeout(releaseTimers.get(key))
}
const timer = setTimeout(() => {
handleRelease(key)
releaseTimers.delete(key)
}, delay)
releaseTimers.set(key, timer)
}
const processMouseMove = (point: CursorPoint) => {
const roundedValue = mapValues(point, Math.round)
@@ -93,14 +80,10 @@ export function useDevice() {
if (nextValue === 'CapsLock') {
handlePress(nextValue)
return handleScheduleRelease(nextValue, 100)
return debouncedRelease(nextValue)
}
if (kind === 'KeyboardPress') {
if (isWindows) {
handleScheduleRelease(nextValue)
}
return handlePress(nextValue)
}