本文介绍了javascript抓取浏览器快捷键(ctrl + t / n / w)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 可以捕获这些快捷方式吗? + + + W 我尝试过但不起作用: $ b $ ($) event.preventDefault(); $ b($)$ {code $ c $ $ b}); 当我按显示 84 ,但是如果我按 + 它不显示任何内容,并打开一个新的标签。 我想捕获这些快捷方式,并阻止任何浏览器操作。解决方案 捕获 Javascript中的键盘事件 示例代码: $ (window).keydown(function(event){ if(event.ctrlKey&& event.keyCode == 84){ console.log(嘿!Ctrl + T事件被捕获! ); event.preventDefault(); } if(event.ctrlKey&& event.keyCode == 83){ console.log(嘿!Ctrl + S事件捕获!); event.preventDefault(); } }); Firefox (6.0.1测试) 在Firefox中,事件监听器都可以正常工作。如果按 或 键组合,则您将在控制台和浏览器中同时收到这两个消息不要打开选项卡,也不要求保存。 如果您使用 alert 的 console.log event.preventDefault()无效,并打开一个新的选项卡或要求保存。也许这个bug需要修复。 Chrome3 在Chrome 3中,它的工作方式与Firefox相同。 Chrome4 在Chrome4中,某些控制键组合已被保留给浏览器用法,不能再被客户端JavaScript 拦截在网页中。 这些限制在Chrome3中不存在,并且与不兼容Firefox3 / 3.5和IE7 / 8(在Windows上)。 在Chrome 4中,它与Firefox类似,除了一些键盘组合: 组合不能被JavaScript捕获,但是嵌入式插件可以捕获这些。例如,如果您专注于Youtube视频,然后按 ,浏览器将不会打开新标签。 IE7 / 8 它的工作原理就像在Firefox或Chrome3中。 IE9 (测试) IE9是一只黑羊,因为它不允许javascript捕获任何 键盘事件。我用许多键盘组合(R,T,P,S,N,T)进行了测试,没有工作。还嵌入应用程序无法捕获事件。用Youtube视频测试。 感谢 @ Lime 为伟大的链接。 Is it possible to capture these shortcuts?+++I tried this but it doesn't work:$(window).keydown(function(event) { console.log(event.keyCode); event.preventDefault();});When I press it shows 84 in the console, but if I press + it shows nothing, and opens a new tab.I would like to capture these shortcuts and prevent any browser action. 解决方案 Capturing keyboard events in JavascriptSample code:$(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(); }});Firefox(6.0.1 tested)In Firefox both event listener works. If you press or keycombinations, you will get both message on the console, and the browser wont open a tab, nor ask for save.It is intresting that if you use alert instead of console.log the event.preventDefault() not works, and opens a new tab or asks for save. Maybe this bug needs to get fixed.Chrome3In Chrome 3 it works like in Firefox.Chrome4(tested) In Chrome4, certain control key combinations have been reserved for browser usage only and can no longer be intercepted by the client side JavaScript in the web page. These restrictions did not exist in Chrome3 and are inconsistent with both Firefox3/3.5 and IE7/8 (on Windows).In Chrome 4 it works similary to Firefox, except some keyboard combination:These combinations cannot get captured by Javascript, but embed plugins can capture these. For example if you focus in a Youtube video and press , the browser won't open a new tab.IE7/8It works like in Firefox or Chrome3.IE9(tested)IE9 is a black sheep again, because it dosen't allow javascript to capture any keyboard event. I tested with many keyboard combination (R,T,P,S,N,T) and neither worked. Also embed applications can't capture the event. Tested with Youtube videos.Thanks to @Lime for the great link. 这篇关于javascript抓取浏览器快捷键(ctrl + t / n / w)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-14 21:09