javascript Jquery,禁用浏览器快捷键
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23492793/
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
Jquery, Disable Browser Shortcut keys
提问by Shivam Pandya
I am trying to block some of browser shortcut keys for my projects like If user press F1key then it should open a new page from project, but browse open its Help page. If I press Ctrl+ Nthen it should open specific page but browser open new window.
我试图阻止我的项目的一些浏览器快捷键,例如如果用户按下F1键,则它应该从项目中打开一个新页面,但浏览打开其帮助页面。如果我按Ctrl+N那么它应该打开特定页面但浏览器打开新窗口。
Here Is my code. (FIDDLE)
这是我的代码。(小提琴)
$('body').keypress(function(event){
if (event.keyCode == 17 && event.keyCode == 110)
{
alert("New");
}
else
{
event.preventDefault();
}
});
回答by collapsar
It seems that you cannot interfere with ctrl+key combinations that have special semantics for the browser (at least that seems to be the situation on chrome 34).
似乎您无法干扰对浏览器具有特殊语义的 ctrl+key 组合(至少在 chrome 34 上似乎是这种情况)。
have a look at this fiddledemonstrating some ways to query keys and key combinations. note that upon pressing ctrl+n
, the keydown handler still triggers the state transition (evidenced by inspecting the alerts upon the keystroke sequence ctrl+n
, shift+n
, shift+n
).
看看这个小提琴演示了一些查询键和键组合的方法。请注意,按下 时ctrl+n
,keydown 处理程序仍会触发状态转换(通过检查击键序列ctrl+n
, shift+n
, 上的警报来证明shift+n
)。
However, I have not found a way to prevent the browser from claiming the keystrokes with meanings in the ui (which I believe is good, taking the user's perspective).
但是,我还没有找到一种方法来阻止浏览器在 ui 中声明具有含义的击键(从用户的角度来看,我认为这很好)。
EDIT:
编辑:
I found this SO answeradressing the issue on chrome (caveat: I haven't tested the solution wrt the current version of chrome).
我发现这个 SO 答案解决了 chrome 上的问题(警告:我没有测试过当前版本的 chrome 的解决方案)。
回答by Moshtaf
If you have no limitation to use jQuery, this is a neat snippet that exactly do what you want:
如果您对使用 jQuery 没有任何限制,那么这是一个简洁的片段,可以完全满足您的需求:
var isCtrlHold = false;
var isShiftHold = false;
$(document).keyup(function (e) {
if (e.which == 17) //17 is the code of Ctrl button
isCtrlHold = false;
if (e.which == 16) //16 is the code of Shift button
isShiftHold = false;
});
$(document).keydown(function (e) {
if (e.which == 17)
isCtrlHold = true;
if (e.which == 16)
isShiftHold = true;
ShortcutManager(e);
});
function ShortcutManager(e){
//F1:
if (e.which == 112) { //112 is the code of F1 button
e.preventDefault(); //prevent browser from the default behavior
alert("F1 is pressed");
}
//Ctrl+K:
if (isCtrlHold && e.which == 75) { //75 is the code of K button
e.preventDefault(); //prevent browser from the default behavior
alert("Ctrl+K is pressed");
}
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Press F1 or press Ctrl+K
Test it on JSFiddle.
在JSFiddle上测试它。
You can find the javascript codes of all keys here: https://www.cambiaresearch.com/articles/15/javascript-char-codes-key-codes
您可以在此处找到所有键的 javascript 代码:https: //www.cambiaresearch.com/articles/15/javascript-char-codes-key-codes
回答by SunUser
<html>
<head>
<script src="http://www.openjs.com/scripts/events/keyboard_shortcuts/shortcut.js"></script>
<script>
shortcut.add("F1",function() {
alert("F1");
});
</script>
</head>
<body>
Please Key press F1
</body>
</html>
You can try this shortcut script its easy to use.
您可以试试这个快捷脚本,它易于使用。
回答by Sajitha Rathnayake
in firefox I tried this
在 Firefox 中我试过这个
$(document).ready(function(){
$(document).keyup(function (e) {
var code = (e.keyCode ? e.keyCode : e.which);
if(code==112)
{
alert("F1 pressed");
}
});
});
f1 - f12: 112-123 workin on main browsers
f1 - f12: 112-123 在主要浏览器上工作
Explorer 5-7: almost
Firefox 2.0 Windows : yes
Firefox 2.0 Mac: yes
Safari 1.3:almost
Opera 9 Windows:yes
Opera 9 Mac: incorrect
The source I have refereed http://www.quirksmode.org/js/keys.html
回答by Rohit
Try this
试试这个
$(window).keydown(function(event) {
if(event.ctrlKey && event.keyCode == 78) {
$('body').html("Hey! Ctrl+N event captured!");
event.preventDefault();
}
});
});