如何使用 javascript 或 Jquery 禁用 CTRL+P?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18843936/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 22:36:28  来源:igfitidea点击:

How to Disable the CTRL+P using javascript or Jquery?

javascriptjquery

提问by Vignesh Pichamani

Here I tried to disable the Ctrl+Pbut it doesn't get me alert and also it shows the print options

在这里,我尝试禁用Ctrl+,P但它没有提醒我,并且还显示了打印选项

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

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

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

I am not sure how can I disable the Ctrl+Pcombination itself using jQuery or JavaScript.

我不确定如何使用 jQuery 或 JavaScript禁用Ctrl+P组合本身。

Thanks

谢谢

回答by Shadow Wizard is Ear For You

You can't prevent the user from printing, but you can hide everything when the user prints the document by using simple CSS:

您无法阻止用户打印,但可以在用户打印文档时使用简单的 CSS 隐藏所有内容:

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

Updated fiddle.

更新了小提琴

If you would like to show the visitor a custom message when he/she try to print rather then just a blank page, it's possible with client side code but first wrap all your existing contents like this:

如果您想在访问者尝试打印时向他/她显示自定义消息而不是空白页,则可以使用客户端代码,但首先要像这样包装所有现有内容:

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

And add such a container with the 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 would be:

现在摆脱<style type="text/css" media="print">块,代码将是:

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 is adopted from this excellent answer.

代码来自这个优秀的答案

Updated fiddle. (showing message when printing)

更新了小提琴。(打印时显示信息)

回答by Peter

After much testings on various browsers, it is easier to intercept the keys when they are down (not pressed) because some of this "App integrated keys" are difficult to intercept with the "keypress" event.

经过在各种浏览器上的大量测试,在按键按下(未按下)时更容易拦截,因为其中一些“应用程序集成键”很难通过“keypress”事件拦截。

I came up with this script that is sort of cross browser compatible (I didn't test for Microsoft's IE). Notice that the browsers return different codes for some keys. In my case I wanted to prevent Ctrl+P.

我想出了这个有点跨浏览器兼容的脚本(我没有测试微软的 IE)。请注意,浏览器会为某些键返回不同的代码。就我而言,我想阻止 Ctrl+P。

The key "P" on chrome is seen as e.keyCode == 80, on opera, it is e.charCode == 16, while on firefox it is e.charCode == 112

chrome 上的“P”键被视为e.keyCode == 80,在opera 上是e.charCode == 16,而在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.

我使用了 jQuery。

回答by Peter

had a journy finding this, should be canceled on the keydownevent

有一个旅程发现这个,应该在keydown活动中取消

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

further simplified to :

进一步简化为:

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

given you have only one keydown event

鉴于您只有一个 keydown 事件

回答by Morne

Your code works in the jsfiddle example? What browser are you using? Itested it with the latest chrome and it worked fine.

您的代码在 jsfiddle 示例中有效?你使用的是什么浏览器?用最新的 chrome 对其进行了测试,效果很好。

You can also add:

您还可以添加:

e.preventDefault();

回答by Hyman Franzen

This Actually worked for me in chrome. I was pretty suprised.

这实际上在 chrome 中对我有用。我很惊讶。

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

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

其中 Print 是我编写的一个函数,它调用 window.print(); 如果您禁用 Print(),它也可用作纯拦截器;

As noted here: https://stackoverflow.com/a/20121038/2102085

如此处所述:https: //stackoverflow.com/a/20121038/2102085

window.print() will pause so you can add an onPrintFinish or onPrintBegin like this

window.print() 将暂停,因此您可以像这样添加 onPrintFinish 或 onPrintBegin

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

(Again this is just chrome, but Peter has a downvoted solution below that claims the keycodes are different for ff and ie)

(同样,这只是 chrome,但 Peter 在下面有一个被低估的解决方案,声称 ff 和 ie 的键码不同)

回答by schwar_01

This is basically Peters answer from above. The difference is I added the accountability for mac when pressing the cmd+p button combo to print a page.

这基本上是彼得斯从上面的回答。不同之处在于我在按下 cmd+p 按钮组合打印页面时为 mac 添加了责任。

$(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();
    }  
});

回答by Dheeraj Guleria

Try this

尝试这个

    //hide body on Ctrl + P
    jQuery(document).bind("keyup keydown", function (e) {
        if (e.ctrlKey && e.keyCode == 80) {
            $("body").hide();
            return false;
        }
    });

回答by lordvlad

there are some shortcuts you simply can't override with javascript, i learned it the hard way. I suppose CTRL+P is one of them.

有一些快捷方式你根本无法用 javascript 覆盖,我是通过艰难的方式学会的。我想 CTRL+P 是其中之一。

one way to override them would be to deploy a chrome pacakged app.

覆盖它们的一种方法是部署一个 chrome pacakged 应用程序。

回答by Pranit More

To disable Ctrl+Pprinting by using javascript use below code:

要使用 javascript禁用Ctrl+P打印,请使用以下代码:

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);

回答by chandni sharma

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