简体   繁体   English

Jquery,禁用浏览器快捷键

[英]Jquery, Disable Browser Shortcut keys

I am trying to block some of browser shortcut keys for my projects like If user press F1 key then it should open a new page from project, but browse open its Help page. 我试图阻止我的项目的一些浏览器快捷键,如果用户按F1键然后它应该从项目打开一个新页面,但浏览打开其帮助页面。 If I press Ctrl + N then it should open specific page but browser open new window. 如果我按Ctrl + N然后它应该打开特定页面但浏览器打开新窗口。

Here Is my code. 这是我的代码。 ( FIDDLE ) FIDDLE

$('body').keypress(function(event){

            if (event.keyCode == 17 && event.keyCode == 110)
            {
                alert("New");   
            }
            else
            {
                event.preventDefault(); 
            }


          });

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 +键组合(至少看起来像是chrome 34上的情况)。

have a look at this fiddle demonstrating 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+nshift+nshift+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 answer adressing the issue on chrome (caveat: I haven't tested the solution wrt the current version of chrome). 我发现这个SO答案解决了关于chrome的问题(警告:我还没有测试当前版本的chrome的解决方案)。

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

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 我已经参考了http://www.quirksmode.org/js/keys.html

<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. 您可以尝试这个易于使用的快捷方式脚本。

Have used this to my project to Use " Enter " button, i guess this might help you, to use for different controls 已经使用这个到我的项目使用“ 输入 ”按钮,我想这可能会帮助你,用于不同的控件

 $('body').keypress(function (e) {
        //debugger;
        if (e.which === 13) {
            $("input[value='Login']").trigger('click');
        }
    });

where 13 is for "Enter" button 其中13表示“Enter”按钮

Try this 尝试这个

$(window).keydown(function(event) {
    if(event.ctrlKey && event.keyCode == 78) { 
        $('body').html("Hey! Ctrl+N event captured!");
            event.preventDefault(); 
        }
    });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM