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

javascript - Overriding shortcut keys in Firefox and Chrome - Stack Overflow

programmeradmin3浏览0评论

I have a script that is supposed to open a section of a web page, and save changes on Ctrl + n and Ctrl + s respectively. I got it working in IE, but it doesn't seem to work in Firefox and Chrome. Any ideas?

My override function.

function prevent(e)
{
try{e.stopPropagation();}catch(ex){}
try{e.preventDefault()}catch(ex){}
try{if (e.preventDefault)
        e.preventDefault();
    else {
        e.cancelBubble = true;
        e.returnValue = false;
        e.keyCode = 0;
    }}  catch(ex){}
}

I have a script that is supposed to open a section of a web page, and save changes on Ctrl + n and Ctrl + s respectively. I got it working in IE, but it doesn't seem to work in Firefox and Chrome. Any ideas?

My override function.

function prevent(e)
{
try{e.stopPropagation();}catch(ex){}
try{e.preventDefault()}catch(ex){}
try{if (e.preventDefault)
        e.preventDefault();
    else {
        e.cancelBubble = true;
        e.returnValue = false;
        e.keyCode = 0;
    }}  catch(ex){}
}
Share Improve this question edited Aug 27, 2020 at 8:06 jonasfh 4,5692 gold badges23 silver badges39 bronze badges asked Apr 9, 2013 at 20:24 wolfcallwolfcall 8861 gold badge11 silver badges19 bronze badges 3
  • this might help: stackoverflow.com/q/7295508/212869 (or not as the case may be) – NickSlash Commented Apr 9, 2013 at 20:36
  • I am trying to avoid JQuery I already have looked at that one. – wolfcall Commented Apr 9, 2013 at 20:47
  • I was thinking more of the answers stating that some browser shortcuts cant really be overridden, also anything done in jQuery can be done in plain JavaScript – NickSlash Commented Apr 9, 2013 at 20:48
Add a comment  | 

2 Answers 2

Reset to default 18

I have seen the same issue. Some browsers will not allow you to capture certain shortcuts. Look at this https://stackoverflow.com/a/7296303/1366887

Some key combinations are resticted in Chrome 4, but not in Chrome 3. Look here: https://groups.google.com/a/chromium.org/forum/?fromgroups=#!topic/chromium-bugs/Ntc1byZXHfU

Here is the Javascript:

$(window).keydown(function(event) {
  if(event.ctrlKey && event.keyCode == 84) { 
    console.log("Hey! Ctrl+T event captured!");
    event.preventDefault(); 
  }
  if(event.ctrlKey && event.keyCode == 83) { 
    console.log("Hey! Ctrl+S event captured!");
    event.preventDefault(); 
  }
});

I have used this numerous times, and it has worked greatly.

Here is another rescource you can take a look at: http://unixpapa.com/js/key.html

Without Jquery:

onkeydown = function(e){
  if(e.ctrlKey && e.keyCode == 'S'.charCodeAt(0)){
    e.preventDefault();
    //your saving code
  }
}

Here is a JSFIDDLE of it working.

For anyone looking for this in the future, the answer for current browsers is the following:

if (event.ctrlKey && event.key === 'k') event.preventDefault()
发布评论

评论列表(0)

  1. 暂无评论