2017-07-25 86 views
0

我正在开发一个web应用程序,我想覆盖CTRL + N(windows)CMD + N(mac)快捷方式,以便它们不会打开新窗口。我想让自定义事件触发。在javascript/jquery中覆盖CMD + N或CTRL + N快捷键?

$(window).bind('keydown', function(e) { 
    if(e.ctrlKey && e.keyCode === 'N'.charCodeAt(0)){ 
     e.preventDefault(); 
     // custom trigger 
    } 
}); 

感谢您的帮助!

+0

FYI:并非所有的操作系​​统都使用Ctrl键。 – epascarello

+0

如果我在不关注文档时按'cmd + n'会怎么样? – evolutionxbox

+0

[如何在Firefox中重写Ctrl + N来启动AJAX](https://stackoverflow.com/questions/16021413/how-to-override-ctrl-n-in-firefox-to-launch-ajax) – William

回答

1

PLZ试试这个

<script> 
window.addEventListener('keyup', function(e) { 
if (e.keyCode === KeyCode.KEY_RETURN) { 
    console.log('It was the Return key.'); 
} else { 
    console.log('It was any other key.'); 
} 
}); 
<script> 

OR

<!DOCTYPE html> 
<html lang="en"> 
<head> 
<title>Bootstrap Example</title> 
<meta charset="utf-8"> 
<meta name="viewport" content="width=device-width, initial-scale=1"> 
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"> 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> 
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> 

<script> 
    var pKey; 
    $(function() { 
     $(window).keydown(function (e) { 
      if (e.which == 17) { 
       pKey = e.keyCode; 
      } 
      else { 
       if (pKey == 17 && e.keyCode == 78) { 
        e.preventDefault(); 
        console.log(e); 
       } 
      } 
     }); 
    }); 
</script> 
</head> 
<body> 

<div class="container"> 
    <h2>Well</h2> 

    <div class="well">Hello G...</div> 
</div> 

</body> 
</html> 
+0

第二个脚本只有在你按住Ctrl + N的同时才能正常工作 –