2

I am currently working on a website and I have a video player I am trying to program basically I have a regular old basic html5 video player...

HTML

<div class="videoPlayerSkin" style="background: black;">
    <video id="videoPlayer" controls>
        <source src="http://localhost/C++Source/video/introSlideshow.mp4" />
        <source src="http://localhost/C++Source/video/introSlideshow.ogg" />
    </video>
</div>

Then I have the JS file

var videoPlayer = document.getElementById("videoPlayer");
var playVideoArray = [16, 17, 80]; //keycodes, ctrl shift and p

document.addEventListener("keydown", function(e) {
    if (e.keyCode === playVideoArray[0]&&playVideoArray[1] + playVideoArray[2])
    {
        videoPlayer.play();
    }
}, false);

But it wont play I went into my errors and could not find anything wrong could someone explain what is wrong with this code and how I can fix this? I am merely trying to play the video when the user presses ctrl+shift+p

Ruan Mendes
  • 90,375
  • 31
  • 153
  • 217
user6031759
  • 118
  • 1
  • 15
  • Nice try, however this: `e.keyCode === playVideoArray[0]&&playVideoArray[1] + playVideoArray[2]` is not exactly what you are looking for.. This condition, afaik, is either invalid or senseless. Because keydown listen to **one key at a time**, you need to store the current key in an array (or an object, even better) and check if all the desired keys are pressed, please check this for reference: http://stackoverflow.com/questions/5203407/javascript-multiple-keys-pressed-at-once (or you can check shiftKey and ctrlKey). Side note: ctrl + shift + p prompts the print window anyway (in chrome) – briosheje May 31 '16 at 17:46

1 Answers1

3

To know if shift and ctrl are down, use shiftKey and ctrlKey from the event object

The keyCode is the code that caused the event, in this case, it's 80 for p as you mentioned. In the following example, you'll get an error because videoPlayer doesn't exist, but it proves that it works

var videoPlayer = document.getElementById("videoPlayer");

document.addEventListener("keydown", function(e) {
  if (e.keyCode === 80 && e.shiftKey && e.ctrlKey) {
    videoPlayer.play();
  }
}, false);
 Just something so you can click and give focus to this frame    <input />
Ruan Mendes
  • 90,375
  • 31
  • 153
  • 217