Javascript 如何使用javascript或jquery触发键盘的f11事件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8253245/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 05:20:27  来源:igfitidea点击:

How to trigger f11 event of keyboard using javascript or jquery?

javascriptjqueryevents

提问by Ravinder Singh

I want to trigger f11 event of keyboard using javascript or jquery, can anyone tell me how to do this ?

我想使用 javascript 或 jquery 触发键盘的 f11 事件,谁能告诉我怎么做?

回答by jacoor

回答by mrtsherman

You can't make people's computers type things through javascript. It violates the sandboxing environment of browsers. I assume that you are trying to make a window go full screen though. This SO question covers that.

你不能让人们的电脑通过 javascript 输入东西。它违反了浏览器的沙盒环境。我假设您正在尝试使窗口全屏显示。这个 SO 问题涵盖了这一点。

How to make the window full screen with Javascript (stretching all over the screen)

如何使用 Javascript 使窗口全屏显示(在整个屏幕上拉伸)

回答by Santos L. Victor

I'm use this code:

我正在使用此代码:

function go_full_screen(){
  var elem = document.documentElement;
  if (elem.requestFullscreen) {
    elem.requestFullscreen();
  } else if (elem.msRequestFullscreen) {
    elem.msRequestFullscreen();
  } else if (elem.mozRequestFullScreen) {
    elem.mozRequestFullScreen();
  } else if (elem.webkitRequestFullscreen) {
    elem.webkitRequestFullscreen();
  }
}
<a href="#" onClick="go_full_screen();">Full Screen / Compress Screen</a>

回答by MarmiK

To trigger event Keypress of F11key like from keyboard, we need this..

F11像从键盘一样触发按键的事件 Keypress ,我们需要这个..

Call this function on any event..

在任何事件上调用此函数..

function mKeyF11(){
    var e = new Event('keypress');
    e.which = 122; // Character F11 equivalent.
    e.altKey=false;
    e.ctrlKey=false;
    e.shiftKey=false;
    e.metaKey=false;
    e.bubbles=true;
    document.dispatchEvent(e);
}

This is pure Javascript function and don't need jQuery to support it..

这是纯 Javascript 函数,不需要 jQuery 来支持它..

Update: Chrome prevents JavaScript key-press. Source

更新:Chrome 会阻止 JavaScript 按键操作。来源

For chrome we can use the following.

对于 chrome,我们可以使用以下内容。

if (document.documentElement.webkitRequestFullscreen) {
    document.documentElement.webkitRequestFullscreen();
}

回答by alexcm1987

Working code sample:

工作代码示例:

document.addEventListener("keyup", keyUpTextField, false);
function keyUpTextField (e) 
{
    var keyCode = e.keyCode;
    if(keyCode==122){/*ejecutaAlgo*/}
}