Reputation: 268
I'm searching for a way to start and stop an HTML 5 video in Chrome (I'm using impress.js
, so it only has to work in Chrome) by pressing the enter key.
I know that it has something to do with the .play()
function, but I'm rather new to JavaScript.
Can someone give me a hint?
Upvotes: 1
Views: 3096
Reputation: 115950
var myVideo = document.getElementById('vid-id');
document.documentElement.addEventListener("keyup", function(e) {
var ev = e || window.event; // window.event for IE fallback
if(ev.keyCode == 13) {
// toggle play/pause
if(myVideo.paused) { myVideo.play(); }
else { myVideo.pause(); }
}
});
Note: keyup
fires only once per press, when a key is released. keydown
and keypress
will fire repeatedly if the user holds down the key.
Upvotes: 1
Reputation: 111910
var video = document.getElementById('video-element-id-here');
document.onkeypress = function(e) {
if ( (e || window.event).keyCode === 13 /* enter key */ ) {
video.paused ? video.play() : video.pause();
}
};
OP: Please see comments too.
Upvotes: 4