为什么触发F11按下事件不起作用?
我刚刚读过这个问题: 全屏页面按按钮而不是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);
当我运行这个时,我收到警报说 122,但似乎它没有给出希望的结果。那里有限制吗?
我在这里做了一个小提琴: http://jsfiddle.net/ap295/5/
I just read this question: Full Screen Page by pressing button instead of F11
The op asked to replace F11 with other hot keys, so I'm wondering that maybe I can simulate press F11 to get things work.
I learned that I can use trigger in JQuery to simulate key press event, so I do something like this:
$("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?
I made a fiddle here: http://jsfiddle.net/ap295/5/
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我认为这是一个:)检测它......
但触发它(不可能)
但你不会阻止浏览器全屏:)......
给出的原因是,假设我已经以某种方式全屏了它,并希望使用 F11 切换它,但你阻止了我,我必须重新启动 PC,[计算机文盲] 这会带来安全问题风险是因为你阻止用户做他期望做的事情,他们可能会认为电脑坏了或者什么:)所以......你就在这里。
I think this is the one :) to detect it ...
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 F11 but 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.
你不能这样做。该问题中的链接答案提供了一种在 jQuery 事件框架内使用 jQuery 模拟按键的方法。
您根本无法触发或伪造按键。所以这个问题的答案是:
不,这是不可能的
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.
You simply can not trigger or fake keypresses. So the answer of this question is:
No, this is impossible
您将无法从网页内覆盖浏览器的内置热键。
您也许能够在浏览器扩展中做到这一点,但是仅仅更改应用程序的热键肯定是严重的矫枉过正。
无论如何,您为什么要覆盖标准键盘快捷键?我不明白。它们已经成为标准很长时间了;大多数用户都会熟悉它们,并且如果将它们更改为其他内容,会发现很奇怪。
You won't be able to override the browser's built-in hotkeys from within a web page.
You might be 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.
不要把它看成“我如何触发F11?”的问题。 - 看看“如何触发或模拟全屏?”
使用旧版本的 IE,您可以直接全屏打开一个新窗口:
或者您可以使用
window.open
打开特定大小的新窗口。或者您可以尝试调整当前窗口的大小以填满屏幕:
但是仅仅因为您可以并不意味着您应该。您应该< em>从不调整当前窗口的大小——这几乎让每个人都烦恼。打开一个新窗口到您选择的大小是更合理的,尽管如果它太大它可能会很烦人,并且在正常的网页上(其中“正常”我可能意味着不是某种基于浏览器的数据输入应用程序)最好不要打开新窗口。
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?"
With older versions of IE you can open a new window straight into full-screen:
Or you can use
window.open
to open a new window of a specific size.Or you can try to resize the current window to fill the screen:
However just because you can doesn't mean you should. You should never resize 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.