javascript 捕获浏览器快捷方式 (ctrl+t/n/w)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7295508/
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
javascript capture browser shortcuts (ctrl+t/n/w)
提问by Gergely Fehérvári
Is it possible to capture these shortcuts?
是否可以捕获这些快捷方式?
- Ctrl+N
- Ctrl+T
- Ctrl+W
- Ctrl+N
- Ctrl+T
- Ctrl+W
I tried this but it doesn't work:
我试过这个,但它不起作用:
$(window).keydown(function(event) {
console.log(event.keyCode);
event.preventDefault();
});
When I press Tit shows 84
in the console, but if I press Ctrl+Tit shows nothing, and opens a new tab.
当我按下T它时,它会显示84
在控制台中,但是如果我按下Ctrl+T它什么也不显示,并会打开一个新选项卡。
I would like to capture these shortcuts and prevent any browser action.
我想捕获这些快捷方式并阻止任何浏览器操作。
回答by Gergely Fehérvári
Capturing Ctrlkeyboard events in Javascript
Ctrl在 Javascript 中捕获键盘事件
Sample 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)
(6.0.1 测试)
In Firefox both event listener works. If you press CtrlTor CtrlSkeycombinations, you will get both message on the console, and the browser wont open a tab, nor ask for save.
在 Firefox 中,两个事件侦听器都可以工作。如果您按CtrlT或CtrlS组合键,您将在控制台上收到这两条消息,并且浏览器不会打开选项卡,也不会要求保存。
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.
这是intresting,如果你使用alert
的不是console.log
的event.preventDefault()
不是的作品,并打开一个新的标签,或者要求保存。也许这个错误需要修复。
Chrome3
铬3
In Chrome 3 it works like in Firefox.
在 Chrome 3 中它就像在 Firefox 中一样工作。
Chrome4
铬4
(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).
在 Chrome4 中,某些控制键组合已保留仅供浏览器使用,无法再被网页中的客户端 JavaScript 拦截。
这些限制在 Chrome3 中不存在,并且与 Firefox3/3.5 和 IE7/8(在 Windows 上)不一致。
In Chrome 4 it works similary to Firefox, except some keyboard combination:
在 Chrome 4 中,它的工作方式与 Firefox 类似,除了一些键盘组合:
CtrlN
CtrlShiftN
CtrlT
CtrlShiftT
CtrlW
CtrlShiftW
CtrlN
CtrlShiftN
CtrlT
CtrlShiftT
CtrlW
CtrlShiftW
These combinations cannot get captured by Javascript, butembed plugins can capture these. For example if you focus in a Youtube video and press CtrlT, the browser won't open a new tab.
这些组合无法被 Javascript 捕获,但嵌入插件可以捕获这些。例如,如果您专注于 Youtube 视频并按CtrlT,浏览器将不会打开新标签页。
IE7/8
IE7/8
It works like in Firefox or Chrome3.
它的工作原理类似于 Firefox 或 Chrome3。
IE9
IE9
(tested)
(已测试)
IE9 is a black sheep again, because it dosen't allow javascript to capture any Ctrl?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.
IE9 再次成为害群之马,因为它不允许 javascript 捕获任何Ctrl?键盘事件。我用许多键盘组合(R、T、P、S、N、T)进行了测试,但都没有奏效。嵌入应用程序也无法捕获事件。使用 Youtube 视频进行测试。
回答by JotaBe
As of March, 20th, 2012, there has been a Chrome fix that allows web applications to handle Control+NWT in Chrome in js keydown event (so it works in pure javascript, or any library like jQuery).
截至 2012 年 3 月 20 日,已经有一个 Chrome 修复程序,允许 web 应用程序在 js keydown 事件中处理 Chrome 中的 Control+NWT(因此它可以在纯 javascript 或任何库(如 jQuery)中工作)。
This fix allows javascript to handle this key combinations if Chrome is opened in Application mode, which can be done in this way:
如果 Chrome 在应用程序模式下打开,此修复程序允许 javascript 处理此组合键,这可以通过以下方式完成:
The fix is documented here:
修复记录在此处:
回答by flying sheep
To expand on the other answers:
要扩展其他答案:
The code to block certain combinations in Chrome/Chromium is defined here, im summary, F11to exit fullscreen mode and everything manipulating and navigating tabs or windows is blocked:
此处定义了在 Chrome/Chromium 中阻止某些组合的代码,我总结一下,F11退出全屏模式并阻止所有操作和导航选项卡或窗口的内容:
[Shift]Ctrl(Q|N|W|T|Tab ?)
[ Shift] Ctrl( Q| N| W| T| Tab ?)
回答by Robin Millette
Instead of trapping the Ctrl+Whotkey, detect when the window is closedmight be a valid option:
检测窗口何时关闭可能是一个有效的选项,而不是捕获Ctrl+W热键:
"The beforeunload event is fired when the window, the document and its resources are about to be unloaded.
“当窗口、文档及其资源即将被卸载时,beforeunload 事件被触发。
When a non-empty string is assigned to the returnValue Event property, a dialog box appears, asking the users for confirmation to leave the page (see example below). When no value is provided, the event is processed silently."
当将非空字符串分配给 returnValue 事件属性时,会出现一个对话框,要求用户确认离开页面(请参见下面的示例)。如果未提供任何值,则事件将被静默处理。”
回答by Robin
The answer is really simple: This is not possible directly without some tricks in javascript.
答案非常简单:如果没有 javascript 中的一些技巧,这是不可能直接实现的。
It depends on the browser. Mostly all browser catch this shortcuts and use it for their own events. (e.g open new tabs) The shortcut never reach the javascript engine.
这取决于浏览器。大多数浏览器都会捕获这个快捷方式并将其用于自己的事件。(例如,打开新标签)快捷方式永远不会到达 javascript 引擎。
What is easy possible however, to catch the shortcuts with flash. But this is wide away from a user friendly website.
然而,用闪光灯捕捉快捷方式是很容易的。但这与用户友好的网站相去甚远。
Update:
更新:
Here a short example. Mostly all browser will show the alert when Ctrl+yis pressed. (y = 89)
这里有一个简短的例子。大多数情况下,所有浏览器都会在按下Ctrl+时显示警报y。(y = 89)
document.onkeydown = keyDownEvent;
document.onkeyup = keyUpEvent;
var isCtrl = false;
function keyDownEvent() {
var keyid = event.keyCode;
if(keyid == 17) {
isCtrl = true;
}
}
function keyUpEvent() {
var keyid = event.keyCode;
if(keyid == 17) {
isCtrl = false;
}
if(keyid == 89 && isCtrl == true) {
alert('CTRL-Y pressed');
}
}
If you replace the 84 by 89, which represents a t, nothing will happen. You can try it on jsfiddle.net.
如果将 84 替换为 89(代表 a )t,则什么都不会发生。你可以在 jsfiddle.net 上试试。