Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Keycode for Print Screen (44 is not working)

So I want to test if visitors of my site have pressed Print Screen button.

As much as I was looking for, there were no information to be found how to do it. All I found was, that ir is supposed to be keyCode == 44.

With all the other buttons I tried there was no problem.

Where is my mistake?

Here's similar, working code for enter button:

window.addEventListener("keydown", checkKeyPressed, false);

function checkKeyPressed(e) {
    if (e.keyCode == "13") {
        alert("The 'enter' key is pressed.");
    }
}
like image 473
sqarf Avatar asked Aug 05 '15 14:08

sqarf


People also ask

What is the keyCode for Print Screen?

Depending on your hardware, you may use the Windows Logo Key + PrtScn button as a shortcut for print screen. If your device does not have the PrtScn button, you may use Fn + Windows logo key + Space Bar to take a screenshot, which can then be printed.

What is e keyCode === 13?

key 13 keycode is for ENTER key.

How do I know if my Print Screen key is working?

Try pressing the Fn and Print Screen keys at the same time to see if a screenshot is successfully taken with this shortcut. You may also try the Fn + Windows key + Print Screen combination. Check if your Print Screen key is working when you use this key combination.


2 Answers

According to the comment on this page: javascripter

In most browsers, pressing the PrntScrn key fires keyup events only.

So you need:

function checkKeyPressed(e) {
    if (e.keyCode == "44") {
        alert("The print screen button was pressed.");
    }
}

window.addEventListener("keyup", checkKeyPressed, false);
like image 199
Chris Charles Avatar answered Oct 06 '22 01:10

Chris Charles


window.addEventListener("keyup", function(e) {
  if (e.keyCode == 44) {
    alert("The 'print screen' key is pressed");
  }
});

Note keyup and not keydown.

Honestly, I have no idea why this works and not the other, but I think it may have something to do with the OS intercepting it on press and (somehow?) blocking the event.

like image 34
ᔕᖺᘎᕊ Avatar answered Oct 06 '22 01:10

ᔕᖺᘎᕊ