Skip to content

Commit 13775d5

Browse files
committed
feat: global hotkey support
1 parent ecef37c commit 13775d5

File tree

2 files changed

+72
-0
lines changed

2 files changed

+72
-0
lines changed

src/js/player.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,8 @@ class Player extends Component {
370370

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

373+
this.boundGlobalKeydown_ = (e) => this.handleGlobalKeydown_(e);
374+
373375
// default isFullscreen_ to false
374376
this.isFullscreen_ = false;
375377

@@ -611,6 +613,10 @@ class Player extends Component {
611613
this.on('keydown', (e) => this.handleKeyDown(e));
612614
this.on('languagechange', (e) => this.handleLanguagechange(e));
613615

616+
if (this.isGlobalHotKeysEnabled()) {
617+
Events.on(document.body, 'keydown', this.boundGlobalKeydown_);
618+
}
619+
614620
this.breakpoints(this.options_.breakpoints);
615621
this.responsive(this.options_.responsive);
616622

@@ -646,6 +652,7 @@ class Player extends Component {
646652
// Make sure all player-specific document listeners are unbound. This is
647653
Events.off(document, this.fsApi_.fullscreenchange, this.boundDocumentFullscreenChange_);
648654
Events.off(document, 'keydown', this.boundFullWindowOnEscKey_);
655+
Events.off(document.body, 'keydown', this.boundGlobalKeydown_);
649656

650657
if (this.styleEl_ && this.styleEl_.parentNode) {
651658
this.styleEl_.parentNode.removeChild(this.styleEl_);
@@ -2247,6 +2254,12 @@ class Player extends Component {
22472254
this.trigger('textdata', data);
22482255
}
22492256

2257+
handleGlobalKeydown_(event) {
2258+
if (event.target === document.body) {
2259+
this.handleKeyDown(event);
2260+
}
2261+
}
2262+
22502263
/**
22512264
* Get object for cached values.
22522265
*
@@ -4570,6 +4583,10 @@ class Player extends Component {
45704583
this.height(this.audioOnlyCache_.controlBarHeight);
45714584
}
45724585

4586+
isGlobalHotKeysEnabled() {
4587+
return !!(this.options_ && this.options_.userActions && this.options_.userActions.globalHotkeys);
4588+
}
4589+
45734590
enableAudioOnlyUI_() {
45744591
// Update styling immediately to show the control bar so we can get its height
45754592
this.addClass('vjs-audio-only-mode');

test/unit/player-user-actions.test.js

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -639,3 +639,58 @@ QUnit.test('hotkeys are NOT ignored when focus is on a button input', function(a
639639
defaultKeyTests.mute(this.player, assert, true);
640640
defaultKeyTests.playPause(this.player, assert, true);
641641
});
642+
643+
QUnit.module('Player: User Actions: Global Hotkeys', {
644+
645+
beforeEach() {
646+
this.clock = sinon.useFakeTimers();
647+
this.player = TestHelpers.makePlayer();
648+
},
649+
650+
afterEach() {
651+
this.player.dispose();
652+
this.clock.restore();
653+
}
654+
});
655+
656+
QUnit.test('when userActions.globalHotkeys is true, hotkeys are enabled at document.body level', function(assert) {
657+
this.player.dispose();
658+
this.player = TestHelpers.makePlayer({
659+
controls: true,
660+
userActions: {
661+
globalHotkeys: true,
662+
hotkeys: true
663+
}
664+
});
665+
666+
this.player.requestFullscreen = sinon.spy();
667+
668+
const event = new KeyboardEvent('keydown', { // eslint-disable-line no-undef
669+
key: 'f'
670+
});
671+
672+
document.body.dispatchEvent(event);
673+
674+
assert.strictEqual(this.player.requestFullscreen.callCount, 1, 'has gone fullscreen');
675+
});
676+
677+
QUnit.test('when userActions.globalHotkeys is NOT true, hotkeys are NOT enabled at document.body level', function(assert) {
678+
this.player.dispose();
679+
this.player = TestHelpers.makePlayer({
680+
controls: true,
681+
userActions: {
682+
globalHotkeys: false,
683+
hotkeys: true
684+
}
685+
});
686+
687+
this.player.requestFullscreen = sinon.spy();
688+
689+
const event = new KeyboardEvent('keydown', { // eslint-disable-line no-undef
690+
key: 'f'
691+
});
692+
693+
document.body.dispatchEvent(event);
694+
695+
assert.strictEqual(this.player.requestFullscreen.callCount, 0, 'has not gone fullscreen');
696+
});

0 commit comments

Comments
 (0)