I want to disable print for some webpages. How to wireup cross browser hotkeys (Cntrl + P) to a javascript that will be fired whenever hotkeys are pressed?
Asked
Active
Viewed 3.5k times
32
-
6What if the user does file > print? – ScottE Sep 20 '12 at 18:01
-
1@ScottE OP never mentions that as his intention – Charlie G Sep 20 '12 at 18:06
-
@charlieg OP states "I want to disable print for some webpages." – Glen Pierce Dec 05 '16 at 22:28
-
This seems to be an XY problem. Although you _can_ catch `ctrl+p`, it is not possible to prevent a user from printing your page., Whether that's by disabling JS on the page, using their browser's console to override your binding, using a browser plugin that prints it regardless of what you catch, by stitching together a bunch of screenshots, or a physical camera, **if the user's eyes can see your content, the user can create a physical copy of your content. Period.** If you're actually looking at how to keep someone from stealing your content, look into DRM and how to secure your IP rights. – AmphotericLewisAcid Aug 05 '23 at 20:12
3 Answers
55
You can override by capturing the event.
jQuery(document).bind("keyup keydown", function(e){
if(e.ctrlKey && e.keyCode == 80){
return false;
}
});
-
1This works. FYI, we're trying to override default print keys and learned that Mac's Command key has different `keyCode` values in each browser which get handled by the the libraries below. – Dylan Valade Jun 23 '15 at 14:44
-
3should be `$(document).bind("keyup keydown", function (e) { if (e.ctrlKey && e.keyCode === 80) { return false; } return true; });` – Nguyễn Xuân Hoàng Jul 19 '17 at 01:46
2
Try Keyboard Shortcuts Library.
Instead of just copy pasting it let's havea look at the source and understand how it works.
You can see the source here

nana
- 4,426
- 1
- 34
- 48