Keycode for Print Screen (44 is not working)
Asked Answered
M

3

10

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.");
    }
}
Mclyman answered 5/8, 2015 at 14:16 Comment(3)
mac have different ways to take screenshotChaperone
What does you get when you click the key? console.log(e.keyCode); Without the if statement ofcause.Disparagement
i get 124 (Mac/italian keyboard) but there isn't seem to be a standard about this exact keyCodeElenoraelenore
E
10
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.

Enshroud answered 5/8, 2015 at 14:20 Comment(1)
i believe its because keydown would catch it early enough to prevent print screen.Lingle
F
4

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);
Faria answered 5/8, 2015 at 14:23 Comment(1)
Looks like i was too slow. I'll leave this up as it has the linkFaria
P
0

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
Pentagram answered 15/9, 2023 at 12:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.