Skip to content

Instantly share code, notes, and snippets.

@luislobo14rap
Last active January 8, 2021 01:10
Show Gist options
  • Save luislobo14rap/6f1d3bc1802b7586f57ac02cd9421aab to your computer and use it in GitHub Desktop.
Save luislobo14rap/6f1d3bc1802b7586f57ac02cd9421aab to your computer and use it in GitHub Desktop.
addAudioAndVideoShortcuts.js
// addAudioAndVideoShortcuts.js v1.1
function addAudioAndVideoShortcuts(videos) {
if (typeof videos == 'undefined') {
// if defined
videos = document.querySelectorAll('audio:not(.key-shortcuts), video:not(.key-shortcuts)');
} else if (typeof videos.length == 'undefined') {
// if node
videos = [videos];
};
videos.forEach(function(element) {
document.body.addEventListener('keydown', function(event) {
if (event.code == 'ArrowLeft' || event.keyCode == 37) {
element.currentTime = element.currentTime - 5;
};
}, false);
document.body.addEventListener('keydown', function(event) {
if (event.code == 'ArrowRight' || event.keyCode == 39) {
element.currentTime = element.currentTime + 5;
};
}, false);
document.body.addEventListener('keydown', function(event) {
if (event.code == 'Space' || event.keyCode == 32) {
if( element.paused ){
element.play();
}else{
element.pause();
};
};
}, false);
element.classList.add('key-shortcuts');
});
setTimeout(function() {
addAudioAndVideoShortcuts();
}, 1000);
};
addAudioAndVideoShortcuts();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment