Search code examples
javascriptevent-listenerkeycode

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.");
    }
}

Solution

  • 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.