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.");
}
}
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.");
}
}
Share
Improve this question
edited Aug 5, 2015 at 14:18
heartyporridge
1,2019 silver badges25 bronze badges
asked Aug 5, 2015 at 14:16
sqarfsqarf
3471 gold badge3 silver badges13 bronze badges
3
|
3 Answers
Reset to default 10window.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.
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);
if it helps, in Visual Basic it works like that:
Private Sub PdfViewer1_KeyUp(sender As Object, e As KeyEventArgs) Handles PdfViewer1.KeyUp
If e.KeyCode = Keys.PrintScreen Then
e.SuppressKeyPress = True
MsgBox("Printscreen is prohibited!", MsgBoxStyle.Critical, "PRINTSCREEN DISABLED!")
Clipboard.Clear()
End If
End Sub
console.log(e.keyCode);
Without the if statement ofcause. – Andreas Louv Commented Aug 5, 2015 at 14:19