最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Disable Internet Explorer shortcut keys - Stack Overflow

programmeradmin0浏览0评论

EDIT: After waited a while and didn't get anything yet, I've decided to do shortcut disable thingy only for IE now. Is there a possibility to disable IE shortcut keys to access menus/print etc. via vbscript?

Is it possible to disable browser shortkeys?

Because many of them are using in application. For instance, Ctrl+p is using and I don't want browser to popup the print window.

EDIT: After waited a while and didn't get anything yet, I've decided to do shortcut disable thingy only for IE now. Is there a possibility to disable IE shortcut keys to access menus/print etc. via vbscript?

Is it possible to disable browser shortkeys?

Because many of them are using in application. For instance, Ctrl+p is using and I don't want browser to popup the print window.

Share Improve this question edited Aug 1, 2012 at 10:59 Himanshu 32.6k32 gold badges115 silver badges136 bronze badges asked Jul 15, 2009 at 9:29 Ramiz UddinRamiz Uddin 4,2594 gold badges42 silver badges72 bronze badges 2
  • Did you manage to get this to work? I'm having the same problem and couldn't find a solution yet. – Diana Amza Commented Oct 6, 2014 at 14:14
  • @DianaAmza, I couldn't find a solution for this. IE will not allow you to override the default behavior. – Ramiz Uddin Commented Oct 10, 2014 at 7:21
Add a comment  | 

6 Answers 6

Reset to default 4

Yes, you can listen for the various key combinations with javascript and disable the default behaviors. There's even a library that you can use and test here. I just tested it using google chrome and firefox in their demo textarea, and it works as you want.

shortcut.add("Ctrl+P",function() {
    return;
});

This works in the browsers that I listed above, but IE will not allow you to override the default behavior in some cases.

Your only option in IE is to disable the Ctrl key entirely with something like:

document.onkeydown = function () { 
  if (event.keyCode == 17) alert('Ctrl Key is disabled'); 
};

Which is not ideal and probably not what you want, but it will work.

You can try creating an event handler for keydown event, check on the keyCode and prevent its default action if needed. However this will not work in all browsers.

An example for Firefox (canceling "Print" short key, verified):

document.addEventListener("keydown", function(oEvent) {
    if (oEvent.keyCode == 80 && oEvent.ctrlKey)
        oEvent.preventDefault();
}, false)

There is a nice trick to fight with IE10+, to avoid display browser menus on alt key combinations, like Alt + F, Alt + H ...

I recently used on IE11, just add an anchor with the attribute accesskey:[yourKey] on your body

<body>
   <a href="#" accesskey="f"></a>
   <script type="text/javascript">
    window.onkeydown = function(e){
          console.log(e.keyCode + " alt: " + e.altKey);
          e.preventDefault();
    };
    window.onkeyup = function(e){
          console.log(e.keyCode + " alt: " + e.altKey);
          e.preventDefault();
    };
   </script>
</body>

Now when you press Alt + f the browser will not display "File popup" as usual, and will let events keydown and keyup gets to you, and not only keydown.

I am working on similar problem, hooking keyboard event Below code works well to disable, except the flash object on the IE has not got the focus. Since I am trying to handle keyboard event on the flash object, this code does not work for me.

function hookKeyboardEvents(e) {
    // get key code
    var key_code = (window.event) ? event.keyCode : e.which;

    // case :if it is IE event
    if (window.event)
    {
        if (!event.shiftKey && !event.ctrlKey) {
            window.event.returnValue = null;
            event.keyCode = 0;
        }
    }
    // case: if it is firefox event
    else
        e.preventDefault();
}

window.document.onkeydown = hookKeyboardEvents;

This works for me in IE 8. The important part is IE requires ev.returnValue to be set to false. NOTE: this only works if you have focus on some element on the document...that is, if you just load the page and hit 'ctrl-p' you'll see the print dialog. But if you click somewhere on the page, then try it, it should suppress the print dialog.

document.onkeydown = function (e) { 

    var ev = e||window.event; 

    // Do what I want keys to do ...

    // Block browser short cuts
    if(ev.preventDefault) // non-IE browsers
        ev.preventDefault();
    else  // IE Only
        ev.returnValue = false;
};

From you application after calling the method on Ctrl+P just make the keycode as zero.I think that will solve your problem...

window.event.keyCode=0;

this will set the keycode as zero..So when explorer checks for the keyCode it will be zero...so default function will not execute...

Try this...just a suggestion

发布评论

评论列表(0)

  1. 暂无评论