javascript 为什么触发 F11 按下事件不起作用?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6428242/
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
Why trigger F11 press event doesn't work?
提问by wong2
I just read this question: Full Screen Page by pressing button instead of F11
The op asked to replace F11with other hot keys, so I'm wondering that maybe I can simulate press F11to get things work.
I learned that I can use trigger in JQuery to simulate key press event, so I do something like this:
我刚刚阅读了这个问题:Full Screen Page bypress button 而不是 F11
操作要求F11用其他热键替换,所以我想知道也许我可以模拟按下F11以使事情工作。
我了解到我可以在 JQuery 中使用触发器来模拟按键事件,所以我做了这样的事情:
$("body").keyup(function (e) {
alert(e.which);
});
var e = $.Event("keyup");
e.which = 122; // # Key code of F11
$("body").trigger(e);
When I run this, I got the alert says 122, but it seems that it doesn't give the hoped result. Is there a restriction there?
当我运行它时,我收到警报说 122,但它似乎没有给出预期的结果。那里有限制吗?
I made a fiddle here: http://jsfiddle.net/ap295/5/
我在这里做了一个小提琴:http: //jsfiddle.net/ap295/5/
采纳答案by Val
I think this is the one :) to detect it ...
我认为这是一个:) 检测它......
$(document).keyup(function(e){
if(e.which==122){
e.preventDefault();//kill anything that browser may have assigned to it by default
//do what ever you wish here :)
alert('F11 pressed');
return false;
}
});
but triggering it (NOT POSSIBLE)
但触发它(不可能)
But you will not prevent the browser from full screen :) ... Reson given is that , lets say I have full screened it somehow, and wish to toggle out of it using F11but u are preventing me, I would have to restart PC, [computer illiterates] which poses security risk as you are preventing a user from doing something he is expecting to do, and they may think PC is broken or something :) so ...there you are.
但是你不会阻止浏览器全屏:) ...给出的原因是,假设我以某种方式全屏了它,并希望使用它来切换它,F11但你阻止了我,我将不得不重新启动 PC, [计算机文盲] 这会带来安全风险,因为您正在阻止用户做他希望做的事情,他们可能认为 PC 坏了或什么:) 所以......你就是这样。
回答by Evert
You can not do this. The linked answer in that question provides a way with jQuery to simulate key-presses, within the jQuery event framework.
你不能做这个。该问题中的链接答案提供了一种在 jQuery 事件框架内使用 jQuery 模拟按键的方法。
You simply can not trigger or fake keypresses. So the answer of this question is:
您根本无法触发或伪造按键。所以这个问题的答案是:
No, this is impossible
不,这是不可能的
回答by Spudley
You won't be able to override the browser's built-in hotkeys from within a web page.
您将无法从网页中覆盖浏览器的内置热键。
You mightbe able to do it in a browser extension, but that's would surely be serious overkill just to change the application's hotkeys.
您也许可以在浏览器扩展程序中执行此操作,但仅更改应用程序的热键肯定是严重的矫枉过正。
In any case, why would you even want to override the standard keyboard shortcuts? I don't get that. They've been standard for a long time; most users will be familiar with them, and will find it very odd if they've been changed to something else.
无论如何,您为什么还要覆盖标准的键盘快捷键?我不明白。长期以来,它们一直是标准配置;大多数用户会熟悉它们,如果将它们更改为其他东西,会觉得很奇怪。
回答by nnnnnn
Don't look at is as a question of "How do I trigger F11?" - look at is as "How do I trigger or simulate full-screen?"
不要看是“如何触发F11?”的问题。- 看是“如何触发或模拟全屏?”
With older versions of IE you can open a new window straight into full-screen:
使用旧版本的 IE,您可以打开一个新窗口直接进入全屏模式:
window.open(someURLorOther, '', 'fullscreen=yes, scrollbars=auto');
Or you can use window.open
to open a new window of a specific size.
或者您可以使用window.open
来打开特定大小的新窗口。
Or you can try to resize the current window to fill the screen:
或者您可以尝试调整当前窗口的大小以填满屏幕:
moveTo(0,0);
resizeTo(screen.availWidth,screen.availHeight);
However just because you candoesn't mean you should.You should neverresize the current window - this annoys practically everyone. Opening a new window to a size you choose is more reasonable, though if it's too big it can be annoying, and on a normal web page (where by "normal" I probably mean not some kind of browser-based data-entry app) it is nicer not to open new windows.
然而,仅仅因为你可以并不意味着你应该。你永远不应该调整当前窗口的大小 - 这几乎让每个人都烦恼。打开一个您选择的大小的新窗口更合理,但如果它太大可能会很烦人,并且在普通网页上(这里的“正常”我可能不是指某种基于浏览器的数据输入应用程序)最好不要打开新窗口。