How to disable CTRL + P using javascript or jQuery?

Here I tried to disable Ctrl + P , but it does not receive a warning, and also shows print options

jQuery(document).bind("keyup keydown", function(e){ if(e.ctrlKey && e.keyCode == 80){ alert('fine'); return false; } }); 

http://jsfiddle.net/qaapD/10/

I'm not sure how I can turn off the Ctrl + P combination using jQuery or JavaScript.

thanks

+7
source share
10 answers

You cannot prevent the user from printing, but you can hide everything when the user prints a document using simple CSS:

 <style type="text/css" media="print"> * { display: none; } </style> 

Updated violin .

If you want to show the visitor a custom message when he tries to print, and not just a blank page, this is possible using client-side code, but first wrap all existing content as follows:

 <div id="AllContent"> <!-- all content here --> </div> 

And add a container like this with a custom message:

 <div class="PrintMessage">You are not authorized to print this document</div> 

Now get rid of the <style type="text/css" media="print"> block and the code will be like this:

 if ('matchMedia' in window) { // Chrome, Firefox, and IE 10 support mediaMatch listeners window.matchMedia('print').addListener(function(media) { if (media.matches) { beforePrint(); } else { // Fires immediately, so wait for the first mouse movement $(document).one('mouseover', afterPrint); } }); } else { // IE and Firefox fire before/after events $(window).on('beforeprint', beforePrint); $(window).on('afterprint', afterPrint); } function beforePrint() { $("#AllContent").hide(); $(".PrintMessage").show(); } function afterPrint() { $(".PrintMessage").hide(); $("#AllContent").show(); } 

Code adopted from this excellent answer .

Updated violin . (shows a message when printing)

+19
source

After many checks in different browsers, it is easier to intercept keys when they are omitted (not pressed), because some of these "closed applications" are difficult to intercept with the "keypress" event.

I came up with this script, which is kind of browser compatible (I have not tested for Microsoft IE). Note that browsers return different codes for some keys. In my case, I wanted to prevent Ctrl + P.

The key "P" on chrome is considered as e.keyCode == 80 , in the opera e.charCode == 16 , and on firefox - e.charCode == 112

 $(document).on('keydown', function(e) { if(e.ctrlKey && (e.key == "p" || e.charCode == 16 || e.charCode == 112 || e.keyCode == 80) ){ alert("Please use the Print PDF button below for a better rendering on the document"); e.cancelBubble = true; e.preventDefault(); e.stopImmediatePropagation(); } }); 

I used jQuery.

+5
source

Does your code work in jsfiddle example? Which browser are you using? I tested it with the last chrome, and it worked fine.

You can also add:

 e.preventDefault(); 
+1
source

It really worked for me in chrome. I was very surprised.

 jQuery(document).bind("keyup keydown", function(e){ if(e.ctrlKey && e.keyCode == 80){ Print(); e.preventDefault(); } }); 

Where Print is the function I wrote that calls window.print (); It also works as a clean blocker if you disable Print ();

As noted here: fooobar.com/questions/160457 / ...

window.print () will be paused, so you can add onPrintFinish or onPrintBegin like this.

 function Print(){ onPrintBegin window.print(); onPrintFinish(); } 

(Again, it's just chrome, but Peter has a lower solution below that claims that the key codes are different for ff, etc.)

+1
source

a log was found that should be canceled in the keydown event

 document.addEventListener('keydown',function(e){ e.preventDefault(); return false; }); 

further simplified:

 document.onkeydown = function(e){ e.preventDefault(); } 

if you have only one keydown event

+1
source

there are a few shortcuts that you simply cannot override with javascript, I found this out with difficulty. I believe CTRL + P is one of them.

one way to override them is to deploy the chrome pacakged application.

0
source

To disable Ctrl + P printing using javascript, use the code below:

 window.addEventListener('keydown', function(event) { if (event.keyCode === 80 && (event.ctrlKey || event.metaKey) && !event.altKey && (!event.shiftKey || window.chrome || window.opera)) { event.preventDefault(); if (event.stopImmediatePropagation) { event.stopImmediatePropagation(); } else { event.stopPropagation(); } return; } }, true); 
0
source

This is basically Peters answer from above. The difference is that I added an account for mac when I pressed the cmd + p key combination to print the page.

 $(document).on('keydown', function(e) { if((e.ctrlKey || e.metaKey) && (e.key == "p" || e.charCode == 16 || e.charCode == 112 || e.keyCode == 80) ){ alert("Please use the Print PDF button below for a better rendering on the document"); e.cancelBubble = true; e.preventDefault(); e.stopImmediatePropagation(); } }); 
0
source
 <script> function isKeyPressed(event) { if(event.ctrlKey == 1) { alert("Please Submit exam form befor printing"); } } </script> <body onkeydown="isKeyPressed(event)"> <p>this is the solution</p> </body> 
-1
source

If you want to disable the printing of your web page, you are wasting time : this cannot be done. Even if you learn to capture CTRL-P, users can still use the browser menu bar to find a print command or take a screenshot of the browser.

Stop trying to control the user, invest your energy in making your website / application more useful , not less useful.

edit 2016: for 3 years, it was up to 3 votes. I still do not delete it. I think it is important to inform fellow developers when they are given impossible tasks or tasks that do not make sense.

edit 2018: I still think it's important that people who have this question read this answer.

-2
source

Source: https://habr.com/ru/post/953968/


All Articles