最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Keycode for Print Screen (44 is not working) - Stack Overflow

programmeradmin0浏览0评论

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
  • mac have different ways to take screenshot – Muhammad Umer Commented Aug 5, 2015 at 14:19
  • What does you get when you click the key? console.log(e.keyCode); Without the if statement ofcause. – Andreas Louv Commented Aug 5, 2015 at 14:19
  • i get 124 (Mac/italian keyboard) but there isn't seem to be a standard about this exact keyCode – Zibellino Commented Aug 5, 2015 at 14:22
Add a comment  | 

3 Answers 3

Reset to default 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.

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
发布评论

评论列表(0)

  1. 暂无评论