-1

I'm trying to work on a, you can say a practice project, In which an audio file will be played in background, Which would be be Paused if SPACEBAR key is pressed and Play when it's pressed again. so, is there anything like onkeypress, keydown etc which can be added to body of html. Thanks, in advance.

2 Answers2

0
var isplaying = false;
window.addEventListener("keydown", function (e) {
   if (e.keyCode === 32) {
      if (isplaying) {
        //Code for pause
      } else {
        //Code for play
      }
   }
});
Abi
  • 165
  • 1
  • 12
-1

You can use a variable to store current state and modify that on key press event as

var state=0; // 0 for play, 1 for pause
document.body.onkeyup = function(e){
if(e.keyCode == 32){
    if(state==0)
       {
        state=1;
        //Run your code to pause
        }
     else{
        state=0;
        //Run your code to play
      }
}
}
Mesar ali
  • 1,832
  • 2
  • 16
  • 18