Last active
March 25, 2020 20:06
-
-
Save dominikkaegi/1f487cd070f378c63fd216f80a7d90ed to your computer and use it in GitHub Desktop.
Adds keyboard events to interact with videos.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const TIME_JUMP_SECONDS = 15 | |
const VOLUME_JUMP = 0.1 | |
function moveInVideoTime(seconds) { | |
document.querySelector('video').currentTime += seconds | |
} | |
function changeVolume(change) { | |
document.querySelector('video').volume += change | |
} | |
function togglePause() { | |
const video = document.querySelector('video') | |
if(video.paused) { | |
console.log('playing video') | |
video.play() | |
} else { | |
console.log('pausing video') | |
video.pause() | |
} | |
} | |
function handleKeypresses(event) { | |
switch (event.code) { | |
case "ArrowLeft": | |
console.log('jump backwards') | |
moveInVideoTime(-TIME_JUMP_SECONDS) | |
break; | |
case "ArrowRight": | |
console.log('jump forwards') | |
moveInVideoTime(+TIME_JUMP_SECONDS) | |
break; | |
case "ArrowUp": | |
console.log('volume up') | |
changeVolume(0.1) | |
break; | |
case "ArrowDown": | |
console.log('volume down') | |
changeVolume(-0.1) | |
break; | |
case "Space": | |
console.group('togglePause') | |
togglePause() | |
console.groupEnd() | |
break; | |
} | |
} | |
document.onkeydown = handleKeypresses | |
window.playbackRate = document.querySelector('video').playbackRate |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment