Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add global hotkey support #8906

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions src/js/player.js
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,8 @@

this.boundUpdatePlayerHeightOnAudioOnlyMode_ = (e) => this.updatePlayerHeightOnAudioOnlyMode_(e);

this.boundGlobalKeydown_ = (e) => this.handleGlobalKeydown_(e);

// default isFullscreen_ to false
this.isFullscreen_ = false;

Expand Down Expand Up @@ -611,6 +613,10 @@
this.on('keydown', (e) => this.handleKeyDown(e));
this.on('languagechange', (e) => this.handleLanguagechange(e));

if (this.isGlobalHotKeysEnabled()) {
Events.on(document.body, 'keydown', this.boundGlobalKeydown_);

Check warning on line 617 in src/js/player.js

View check run for this annotation

Codecov / codecov/patch

src/js/player.js#L617

Added line #L617 was not covered by tests
}

this.breakpoints(this.options_.breakpoints);
this.responsive(this.options_.responsive);

Expand Down Expand Up @@ -646,6 +652,7 @@
// Make sure all player-specific document listeners are unbound. This is
Events.off(document, this.fsApi_.fullscreenchange, this.boundDocumentFullscreenChange_);
Events.off(document, 'keydown', this.boundFullWindowOnEscKey_);
Events.off(document.body, 'keydown', this.boundGlobalKeydown_);

if (this.styleEl_ && this.styleEl_.parentNode) {
this.styleEl_.parentNode.removeChild(this.styleEl_);
Expand Down Expand Up @@ -2247,6 +2254,12 @@
this.trigger('textdata', data);
}

handleGlobalKeydown_(event) {
if (event.target === document.body) {
this.handleKeyDown(event);

Check warning on line 2259 in src/js/player.js

View check run for this annotation

Codecov / codecov/patch

src/js/player.js#L2257-L2259

Added lines #L2257 - L2259 were not covered by tests
}
}

/**
* Get object for cached values.
*
Expand Down Expand Up @@ -4570,6 +4583,10 @@
this.height(this.audioOnlyCache_.controlBarHeight);
}

isGlobalHotKeysEnabled() {
return !!(this.options_ && this.options_.userActions && this.options_.userActions.globalHotkeys);
}

enableAudioOnlyUI_() {
// Update styling immediately to show the control bar so we can get its height
this.addClass('vjs-audio-only-mode');
Expand Down