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.");
}
}
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.
key 13 keycode is for ENTER key.
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.
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);
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With