Print Screen key code
Print Screen sends event.key "PrintScreen" and keyCode 44, and on Windows only on keyup.
Every value at a glance
| Key | event.code | event.key | keyCode | keyCode on a Mac | event.location |
|---|---|---|---|---|---|
| Print Screen / F13 | PrintScreen | PrintScreen | 44 | 124 | 0 (Standard) |
keyCode as Chrome and Edge send it on Windows. The Mac column (Chrome and Safari on a Mac) and the Firefox column show only numbers that differ; some keys also differ by system, as the text below says. event.which is always the same number as keyCode. event.key is for a US layout without Shift.
Press it here
Click here, then press Print Screen
Using only a keyboard? Press Esc, then Tab, to leave the key box.
Code to react to Print Screen
document.addEventListener('keyup', (event) => {
if (event.key === 'PrintScreen') {
// Your code here
}
});document.addEventListener('keyup', (event) => {
if (event.code === 'PrintScreen') {
// Your code here
}
});document.addEventListener('keyup', (event) => {
if (event.keyCode === 44) {
// Your code here
}
});Which value to use
Check event.key === 'PrintScreen' (or event.code, which is the same). keyCode is 44 on Windows. On a Mac, a PC keyboard’s Print Screen key works as F13: Chrome and Safari report key and code "F13" with keyCode 124, so a PrintScreen check doesn’t match it there.
Listen for keyup, not keydown. Windows takes the key press for itself and passes only the release to the browser.
What trips people up
My keydown handler never sees Print Screen
On Windows the browser gets only a keyup. Move the check to keyup:
window.addEventListener('keyup', (event) => {
if (event.key === 'PrintScreen') { /* ... */ }
});
On Linux the desktop’s screenshot tool often takes the key, and the page may get nothing at all.
I want to block screenshots
A web page can’t. Print Screen saves the image before your page hears the key, and many screenshots don’t use the key at all: Windows + Shift + S, the Snipping Tool, Cmd + Shift + 3, 4 or 5 on a Mac, phone buttons, or another camera. Clearing the clipboard on keyup doesn’t help either: by then the screenshot may already be saved as a file, and recent Windows versions open the Snipping Tool on Print Screen.
Protected video (DRM) can show black in some screenshots, but only the video, and not on every system.
I want to know when someone took a screenshot
You can’t, reliably. The keyup tells you Print Screen was pressed while your page had focus on Windows. It can’t tell you whether a screenshot was saved, and every other way of taking one stays invisible to the page.
Print Screen needs Fn on my laptop
Many laptops put Print Screen on another key with Fn. The browser still gets "PrintScreen", since the keyboard handles Fn itself. Some laptops have no Print Screen key at all.
Note: A Mac has no Print Screen key. Its screenshot shortcuts are Cmd + Shift + 3 (whole screen), Cmd + Shift + 4 (an area) and Cmd + Shift + 5 (the toolbar). macOS takes them, so the page doesn’t get them as a shortcut.
Alt+Print Screen
On Windows, Alt + Print Screen captures only the active window. The page may see the Alt keydown and then the Print Screen keyup, with event.altKey true.