Hotkey
Register a keyboard shortcut that invokes a player action
Import
import { Hotkey } from '@videojs/react';import '@videojs/html/ui/hotkey';Anatomy
<Hotkey keys="Space" action="togglePaused" /><media-hotkey keys="Space" action="togglePaused"></media-hotkey>Behavior
Hotkey is a behavior component: it renders no visible interface. Place it inside a Container to register one key pattern and player action. By default, the shortcut responds while the player container has focus. Set target="document" only when the shortcut should apply across the entire document.
Key patterns are case-insensitive and may use:
- Named keys such as
Space,ArrowLeft, andArrowRight. - Exact modifiers such as
Ctrl+k,Shift+f,Alt+m, andMeta+k. ModforMetaon macOS andCtrlelsewhere.- The special
0-9range for digit-based seeking.
Choose an action from the API reference below. Pass value as seconds for seekStep, a volume delta such as 0.05 or -0.05 for volumeStep, or a percentage from 0 to 100 for seekToPercent. When value is omitted from seekToPercent with keys="0-9", the pressed digit supplies the percentage.
Toggle actions ignore held-key repeats. Step, rate, and percentage actions may repeat. A matched shortcut prevents the browser’s default behavior. Unmodified shortcuts are ignored in editable fields, and Space or Enter still activates focused buttons, links, and sliders normally.
Accessibility
Hotkeys supplement operable controls; they should never be the only way to perform an action. Built-in buttons use matching hotkey registrations to expose aria-keyshortcuts, and their tooltips can display the registered shortcut.
Examples
Basic Usage
This player uses Space to play or pause, M to mute, and the arrow keys to seek by five seconds. Click the player first to focus its container.
Space: play/pause · M: mute · ←/→: seek
import { Container, createPlayer, Hotkey, PlayButton } from '@videojs/react';
import { Video, videoFeatures } from '@videojs/react/video';
const { Player } = createPlayer({ features: videoFeatures });
export default function BasicUsage() {
return (
<Player>
<Container className="react-hotkey-basic">
<Video src="https://stream.mux.com/BV3YZtogl89mg9VcNBhhnHm02Y34zI1nlMuMQfAbl3dM/highest.mp4" muted playsInline />
<p className="react-hotkey-basic__instructions">Space: play/pause · M: mute · ←/→: seek</p>
<PlayButton
className="react-hotkey-basic__button"
render={(props, state) => (
<button {...props}>{state.ended ? 'Replay' : state.paused ? 'Play' : 'Pause'}</button>
)}
/>
<Hotkey keys="Space" action="togglePaused" />
<Hotkey keys="m" action="toggleMuted" />
<Hotkey keys="ArrowLeft" action="seekStep" value={-5} />
<Hotkey keys="ArrowRight" action="seekStep" value={5} />
</Container>
</Player>
);
}
.react-hotkey-basic {
position: relative;
}
.react-hotkey-basic video {
width: 100%;
}
.react-hotkey-basic__instructions {
position: absolute;
top: 10px;
left: 10px;
padding: 6px 10px;
margin: 0;
color: white;
background: rgb(0 0 0 / 70%);
border-radius: 4px;
}
.react-hotkey-basic__button {
position: absolute;
bottom: 10px;
left: 10px;
padding: 8px 20px;
color: black;
cursor: pointer;
background: rgb(255 255 255 / 80%);
border: 1px solid rgb(255 255 255 / 30%);
border-radius: 9999px;
}
Space: play/pause · M: mute · ←/→: seek
<video-player>
<media-container class="html-hotkey-basic">
<video src="https://stream.mux.com/BV3YZtogl89mg9VcNBhhnHm02Y34zI1nlMuMQfAbl3dM/highest.mp4" muted playsinline></video>
<p class="html-hotkey-basic__instructions">Space: play/pause · M: mute · ←/→: seek</p>
<media-play-button class="html-hotkey-basic__button">
<span class="show-when-paused">Play</span>
<span class="show-when-playing">Pause</span>
<span class="show-when-ended">Replay</span>
</media-play-button>
<media-hotkey keys="Space" action="togglePaused"></media-hotkey>
<media-hotkey keys="m" action="toggleMuted"></media-hotkey>
<media-hotkey keys="ArrowLeft" action="seekStep" value="-5"></media-hotkey>
<media-hotkey keys="ArrowRight" action="seekStep" value="5"></media-hotkey>
</media-container>
</video-player>
.html-hotkey-basic {
position: relative;
}
.html-hotkey-basic video {
width: 100%;
}
.html-hotkey-basic__instructions {
position: absolute;
top: 10px;
left: 10px;
padding: 6px 10px;
margin: 0;
color: white;
background: rgb(0 0 0 / 70%);
border-radius: 4px;
}
.html-hotkey-basic__button {
position: absolute;
bottom: 10px;
left: 10px;
padding: 8px 20px;
color: black;
cursor: pointer;
background: rgb(255 255 255 / 80%);
border: 1px solid rgb(255 255 255 / 30%);
border-radius: 9999px;
}
.html-hotkey-basic__button .show-when-paused,
.html-hotkey-basic__button .show-when-playing,
.html-hotkey-basic__button .show-when-ended {
display: none;
}
.html-hotkey-basic__button[data-paused]:not([data-ended]) .show-when-paused,
.html-hotkey-basic__button:not([data-paused]) .show-when-playing,
.html-hotkey-basic__button[data-ended] .show-when-ended {
display: inline;
}
import '@videojs/html/video/player';
import '@videojs/html/ui/hotkey';
import '@videojs/html/ui/play-button';
API Reference
Props
| Prop | Type | Default | Details |
|---|---|---|---|
action* | 'togglePaused' | 'toggleMuted' | 'tog... | — | |
| |||
keys* | string | — | |
| |||
disabled | boolean | — | |
| |||
target | 'player' | 'document' | — | |
| |||
value | number | — | |
| |||