diff --git a/ui/src/hooks/useKeyboard.ts b/ui/src/hooks/useKeyboard.ts index c2ce9ab3..d2d289cf 100644 --- a/ui/src/hooks/useKeyboard.ts +++ b/ui/src/hooks/useKeyboard.ts @@ -272,15 +272,13 @@ export default function useKeyboard() { }, []); const scheduleKeepAlive = useCallback(() => { - // Clear existing timer if it exists - if (keepAliveTimerRef.current) { - clearInterval(keepAliveTimerRef.current); - } + // Clears existing keepalive timer + cancelKeepAlive(); keepAliveTimerRef.current = setInterval(() => { sendKeypressKeepAliveHidRpc(); }, KEEPALIVE_INTERVAL); - }, [sendKeypressKeepAliveHidRpc]); + }, [cancelKeepAlive, sendKeypressKeepAliveHidRpc]); // resetKeyboardState is used to reset the keyboard state to no keys pressed and no modifiers. // This is useful for macros and when the browser loses focus to ensure that the keyboard state