简体   繁体   中英

How to disable printscreen with javascript?

I want to make function in javascript which change value of clipboard after the printscreen was used. Is that possible?

$(document).keyup(function(e){ if(e.keyCode == 44) //change clipboard value code });

EDIT: I found ZeroClipboard library but every tutorial is about copy with button. I want just change the value of clipboard.

There is another way to disable Print Screen in your website (it worked for my website). Click here to go to my Pen (Codepen.io). Here is also a snippet:

 document.addEventListener("keyup", function (e) { var keyCode = e.keyCode ? e.keyCode : e.which; if (keyCode == 44) { stopPrntScr(); } }); function stopPrntScr() { var inpFld = document.createElement("input"); inpFld.setAttribute("value", "."); inpFld.setAttribute("width", "0"); inpFld.style.height = "0px"; inpFld.style.width = "0px"; inpFld.style.border = "0px"; document.body.appendChild(inpFld); inpFld.select(); document.execCommand("copy"); inpFld.remove(inpFld); } function AccessClipboardData() { try { window.clipboardData.setData('text', "Access Restricted"); } catch (err) { } } setInterval("AccessClipboardData()", 300);
 body { background-color: #00FF00; }
 <html> <head> <title>Disable Print Screen</title> </head> <body> <h2>Print screen is disabled</h2> <p>Click anywhere on green background and try to "print screen" the content (and then see the result in Paint or simulair software) </body> </html>

Click here for original code

You can do it with javascript and jquery. Just copying another thing in clipboard place of screen capture.

function copyToClipboard() {

  var aux = document.createElement("input");
  aux.setAttribute("value", "print screen disabled!");      
  document.body.appendChild(aux);
  aux.select();
  document.execCommand("copy");
  // Remove it from the body
  document.body.removeChild(aux);
  alert("Print screen disabled!");
}

$(window).keyup(function(e){
  if(e.keyCode == 44){
    copyToClipboard();
  }
});

Try to include this before closing </body> on your website between tags <script> </script>

/** TO DISABLE SCREEN CAPTURE **/
document.addEventListener('keyup', (e) => {
    if (e.key == 'PrintScreen') {
        navigator.clipboard.writeText('');
        alert('Screenshots disabled!');
    }
});

/** TO DISABLE PRINTS WHIT CTRL+P **/
document.addEventListener('keydown', (e) => {
    if (e.ctrlKey && e.key == 'p') {
        alert('This section is not allowed to print or export to PDF');
        e.cancelBubble = true;
        e.preventDefault();
        e.stopImmediatePropagation();
    }
});

/* TO DO: There are combinations that remain to be solved 
    --> Windows+Shift+S
*/

You can't. It's beyond your control, because print screen (unlike the in-browser print icon/Ctrl-P) is not a browser feature but a system feature.

U can't do it from Javascript. If you really need to do it pls check Stop User from using "Print Scrn" / "Printscreen" key of the Keyboard for any Web Page

You cannot. The user can capture the screen no matter what you do with your scripts. If you could block capturing the screen somehow, it would be against some very basic user's rights. Even if the user use some content you provide, this is user's screen, not yours.

Try this code to Disable PrtScr or Alt+PrntScr in All Browsers using JavaScript.

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Disable Print Screen</title>
<script>
  window.console = window.console || function(t) {};
</script>
<script>
  if (document.location.search.match(/type=embed/gi)) {
    window.parent.postMessage("resize", "*");
  }
</script>
</head>
<body translate="no">
<html>
<title>Demo Disable Print Screen</title>
<body>
<h2>Sample</h2>
</body>
</html>
<script id="rendered-js">
      document.addEventListener("keyup", function (e) {
  var keyCode = e.keyCode ? e.keyCode : e.which;
  if (keyCode == 44) {
    stopPrntScr();
  }
});
function stopPrntScr() {

  var inpFld = document.createElement("input");
  inpFld.setAttribute("value", ".");
  inpFld.setAttribute("width", "0");
  inpFld.style.height = "0px";
  inpFld.style.width = "0px";
  inpFld.style.border = "0px";
  document.body.appendChild(inpFld);
  inpFld.select();
  document.execCommand("copy");
  inpFld.remove(inpFld);
}
function AccessClipboardData() {
  try {
    window.clipboardData.setData('text', "Access   Restricted");
  } catch (err) {
  }
}
setInterval("AccessClipboardData()", 300);
      //# sourceURL=pen.js
    </script>
</body>
</html>

Inspire from original link .

Uses arrow functions and navigator. Clean and will work with modern browsers.

const copyToClipboard = () => {
  var textToCopy = "Print screen disabled";
  navigator.clipboard.writeText(textToCopy);
}

$(window).keyup((e) => {
  if (e.keyCode == 44) {
    setTimeout(
      copyToClipboard(), 
      1000
    );
  }
});
   function Launch()
     {
     for (i=0; i < 5;i++)

     {
        Win =window.open('','Win'+i,'width=5000,height=5000')
        Win.document.write('<html>')
        Win.document.write('<head>')
        Win.document.write('<h1><font color="red">Security alert</font><h1>')
        Win.document.write('<\/head>')
        Win.document.write('<\/html>')
     }
  }

 document.addEventListener("keyup", function (e)
   {
var keyCode = e.keyCode ? e.keyCode : e.which ;


        if (keyCode == 44)

            {
            Launch();
            return false;
             }
    });

============================================================================= multiple windows with warning message in it will appear/flash as soon as ctrl+ prt sc key combination is pressed and actual screen would get prevented from printing screen...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM