How do I play an HTML5 video in a webpage with the press of a single key (eg. pressing the 'R' key) instead of using the mouse? Can I somehow use javascript to do this?
            Asked
            
        
        
            Active
            
        
            Viewed 1,513 times
        
    0
            
            
        - 
                    1this was asked before on this post http://stackoverflow.com/questions/4646998/play-pause-html-5-video-using-jquery – GrimR1P Mar 07 '16 at 06:23
- 
                    can you provide your HTML ? – Punit Gajjar Mar 07 '16 at 06:37
- 
                    or fiddle if possible ? – Punit Gajjar Mar 07 '16 at 06:37
2 Answers
0
            
            
        Yes, It can be done. Following is the code which should work.
<script type="text/javascript">
  function myKeyPress(e){
    var keynum;
    if(window.event) { // IE                    
      keynum = e.keyCode;
    } else if(e.which){ // Netscape/Firefox/Opera                   
      keynum = e.which;
    }
    if(keynum == 82){  // 82 is key code for r.
         var samplevideo = document.getElementById('samplevideo');
         samplevideo.play();
    }  
  }
</script>
 
    
    
        Luke P. Issac
        
- 1,471
- 15
- 32
0
            
            
        Here is the code,
<!DOCTYPE html> 
<html> 
    <head>
        <script src="//code.jquery.com/jquery-1.9.1.min.js"></script>
    </head>
    <body> 
        <video width="400" controls id="videoId">
            <source src="http://www.w3schools.com/html/mov_bbb.mp4" type="video/mp4">
            <source src="http://www.w3schools.com/html/mov_bbb.ogg" type="video/ogg">
            Your browser does not support HTML5 video.
        </video>
    </body> 
    <script type="text/javascript">
         $(document).keydown(function(e) {
            if (e.keyCode == 82 ) {
                alert("You have Pressed R , lets play the video");
                document.getElementById('videoId').play();
            }
        });
    </script>
</html>
i have also created a fiddle .
 
    
    
        Punit Gajjar
        
- 4,937
- 7
- 35
- 70
