Skip to content

Commit 8e1d18d

Browse files
authored
Move keyboard controls to video controls (#10617)
1 parent 4cf1945 commit 8e1d18d

File tree

2 files changed

+35
-52
lines changed

2 files changed

+35
-52
lines changed

web/src/components/player/HlsVideoPlayer.tsx

Lines changed: 0 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
import {
22
MutableRefObject,
33
ReactNode,
4-
useCallback,
54
useEffect,
65
useRef,
76
useState,
87
} from "react";
98
import Hls from "hls.js";
109
import { isDesktop, isMobile } from "react-device-detect";
11-
import useKeyboardListener from "@/hooks/use-keyboard-listener";
1210
import { TransformComponent, TransformWrapper } from "react-zoom-pan-pinch";
1311
import VideoControls from "./VideoControls";
1412

@@ -87,56 +85,6 @@ export default function HlsVideoPlayer({
8785
const [controls, setControls] = useState(isMobile);
8886
const [controlsOpen, setControlsOpen] = useState(false);
8987

90-
const onKeyboardShortcut = useCallback(
91-
(key: string, down: boolean, repeat: boolean) => {
92-
if (!videoRef.current) {
93-
return;
94-
}
95-
96-
switch (key) {
97-
case "ArrowLeft":
98-
if (down) {
99-
const currentTime = videoRef.current.currentTime;
100-
101-
if (currentTime) {
102-
videoRef.current.currentTime = Math.max(0, currentTime - 5);
103-
}
104-
}
105-
break;
106-
case "ArrowRight":
107-
if (down) {
108-
const currentTime = videoRef.current.currentTime;
109-
110-
if (currentTime) {
111-
videoRef.current.currentTime = currentTime + 5;
112-
}
113-
}
114-
break;
115-
case "m":
116-
if (down && !repeat && videoRef.current) {
117-
videoRef.current.muted = !videoRef.current.muted;
118-
}
119-
break;
120-
case " ":
121-
if (down && videoRef.current) {
122-
if (videoRef.current.paused) {
123-
videoRef.current.play();
124-
} else {
125-
videoRef.current.pause();
126-
}
127-
}
128-
break;
129-
}
130-
},
131-
// only update when preview only changes
132-
// eslint-disable-next-line react-hooks/exhaustive-deps
133-
[videoRef.current],
134-
);
135-
useKeyboardListener(
136-
["ArrowLeft", "ArrowRight", "m", " "],
137-
onKeyboardShortcut,
138-
);
139-
14088
return (
14189
<div
14290
className={`relative ${visible ? "visible" : "hidden"}`}

web/src/components/player/VideoControls.tsx

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import {
1717
MdVolumeUp,
1818
} from "react-icons/md";
1919
import { Slider } from "../ui/slider-volume";
20+
import useKeyboardListener from "@/hooks/use-keyboard-listener";
2021

2122
type VideoControls = {
2223
volume?: boolean;
@@ -100,6 +101,40 @@ export default function VideoControls({
100101
// eslint-disable-next-line react-hooks/exhaustive-deps
101102
}, [video?.volume, video?.muted]);
102103

104+
const onKeyboardShortcut = useCallback(
105+
(key: string, down: boolean, repeat: boolean) => {
106+
switch (key) {
107+
case "ArrowLeft":
108+
if (down) {
109+
onSeek(-10);
110+
}
111+
break;
112+
case "ArrowRight":
113+
if (down) {
114+
onSeek(10);
115+
}
116+
break;
117+
case "m":
118+
if (down && !repeat && video) {
119+
video.muted = !video.muted;
120+
}
121+
break;
122+
case " ":
123+
if (down) {
124+
onPlayPause(!isPlaying);
125+
}
126+
break;
127+
}
128+
},
129+
// only update when preview only changes
130+
// eslint-disable-next-line react-hooks/exhaustive-deps
131+
[video, isPlaying, onSeek],
132+
);
133+
useKeyboardListener(
134+
["ArrowLeft", "ArrowRight", "m", " "],
135+
onKeyboardShortcut,
136+
);
137+
103138
if (!show) {
104139
return;
105140
}

0 commit comments

Comments
 (0)