Javascript 覆盖浏览器的键盘快捷键

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

Overriding Browser's Keyboard Shortcuts

javascript

提问by Tom Tucker

I'd like to add support for keyboard shortcuts to a couple of pages in my web application by intercepting the keypress event handler of the document object, not the accesskey attribute.

我想通过拦截文档对象的 keypress 事件处理程序,而不是 accesskey 属性,为我的 Web 应用程序中的几个页面添加对键盘快捷键的支持。

The problem is that every browser has its own keyboard combinations, so it's impossible to come up with a set of keyboard combinations that work on all web browsers and yet consistent.(e.g. It'd be silly if the shortcut for save was Ctrl+ Shift+ Swhile one for delete was Alt+ D.)

问题是,每个浏览器都有自己的组合键,因此不可能拿出一套键盘组合可在所有网络浏览器的工作,但一致的。(例如,它会是愚蠢的,如果在保存快捷为Ctrl+ Shift+ S,而删除的一个是Alt+ D。)

So I figured it would be just simpler to override browser shortcuts altogether in a couple of pages with mine.

所以我认为在我的几个页面中完全覆盖浏览器快捷方式会更简单。

All downside aside, is it possible? If so, how do you do it?

撇开所有缺点不谈,这可能吗?如果是这样,你怎么做?

采纳答案by Brad Robinson

There's an excellent coverage of this here: http://unixpapa.com/js/key.html

这里有一个很好的报道:http: //unixpapa.com/js/key.html

As for whether this is something that should be done, stackoverflow's question editor override's quite a few keys without disrupting too much (hover over the toolbar buttons).

至于这是否应该完成,stackoverflow 的问题编辑器覆盖了相当多的键而不会造成太多干扰(将鼠标悬停在工具栏按钮上)。

回答by antimatter15

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

回答by Alesk

Here is my solution to this problem:

这是我对这个问题的解决方案:

Most (if not all) of the browser's shortcuts will be overriden. Only system ones, like Alt+ Tabor the Windowskey won't.

大多数(如果不是全部)浏览器的快捷方式将被覆盖。只有系统的,如Alt+TabWindows键不会。

document.onkeydown = overrideKeyboardEvent;
document.onkeyup = overrideKeyboardEvent;
var keyIsDown = {};

function overrideKeyboardEvent(e){
  switch(e.type){
    case "keydown":
      if(!keyIsDown[e.keyCode]){
        keyIsDown[e.keyCode] = true;
        // do key down stuff here
      }
    break;
    case "keyup":
      delete(keyIsDown[e.keyCode]);
      // do key up stuff here
    break;
  }
  disabledEventPropagation(e);
  e.preventDefault();
  return false;
}
function disabledEventPropagation(e){
  if(e){
    if(e.stopPropagation){
      e.stopPropagation();
    } else if(window.event){
      window.event.cancelBubble = true;
    }
  }
}

回答by deepeshb

Here is my Solution:

这是我的解决方案:

document.onkeydown = function () {
                       if ((window.event.keyCode == 121) && (window.event.ctrlKey))) {
               window.event.returnValue = false;
               window.event.cancelBubble = true;
               window.event.keyCode = 0;
               return false;
           }
       }