Skip to content

armgit / meet

Public Imported from GitHub
0

Open source video conferencing app powered by LiveKit. Built with Django and React.

✨(frontend) introduce long press keyboard shortcut

lebaudantoine1faae96ccd41
diff --git a/src/frontend/src/features/shortcuts/useLongPress.ts b/src/frontend/src/features/shortcuts/useLongPress.ts
new file mode 100644
index 00000000..775f9237
--- /dev/null
+++ b/src/frontend/src/features/shortcuts/useLongPress.ts
@@ -0,0 +1,47 @@
+import { useEffect, useRef } from 'react'
+
+export type useLongPressProps = {
+ keyCode?: string
+ onKeyDown: () => void
+ onKeyUp: () => void
+ longPressThreshold?: number
+}
+
+export const useLongPress = ({
+ keyCode,
+ onKeyDown,
+ onKeyUp,
+ longPressThreshold = 300,
+}: useLongPressProps) => {
+ const timeoutIdRef = useRef<ReturnType<typeof setTimeout> | null>(null)
+
+ useEffect(() => {
+ const handleKeyDown = (event: KeyboardEvent) => {
+ if (event.code != keyCode || timeoutIdRef.current) return
+ timeoutIdRef.current = setTimeout(() => {
+ onKeyDown()
+ }, longPressThreshold)
+ }
+
+ const handleKeyUp = (event: KeyboardEvent) => {
+ if (event.code != keyCode || !timeoutIdRef.current) return
+ clearTimeout(timeoutIdRef.current)
+ timeoutIdRef.current = null
+ onKeyUp()
+ }
+
+ if (!keyCode) return
+
+ window.addEventListener('keydown', handleKeyDown)
+ window.addEventListener('keyup', handleKeyUp)
+
+ return () => {
+ window.removeEventListener('keydown', handleKeyDown)
+ window.removeEventListener('keyup', handleKeyUp)
+ }
+ }, [keyCode, onKeyDown, onKeyUp, longPressThreshold])
+
+ return
+}
+
+export default useLongPress