如何在 JavaScript 中监听 Ctrl-P 按键?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12517819/
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-23 10:35:59  来源:igfitidea点击:

How to listen for Ctrl-P key press in JavaScript?

javascriptjquerybrowsercross-browser

提问by John

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?

我想禁用某些网页的打印功能。如何将跨浏览器热键 (Cntrl + P) 连接到一个 javascript 中,该 javascript 将在按下热键时触发?

回答by Anoop

You can override by capturing the event.

您可以通过捕获事件来覆盖。

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

回答by Charlie G

Also a great library is Mousetrap

Mousetrap也是一个很棒的图书馆

回答by nana

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

你可以在这里看到