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

后端 未结 5 1240
既然无缘
既然无缘 2020-12-16 05:18

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

相关标签:
5条回答
  • 2020-12-16 05:19

    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.

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

    0 讨论(0)
  • 2020-12-16 05:23

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

    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..

    Update: Chrome prevents JavaScript key-press. Source

    For chrome we can use the following.

    if (document.documentElement.webkitRequestFullscreen) {
        document.documentElement.webkitRequestFullscreen();
    }
    
    0 讨论(0)
  • 2020-12-16 05:28

    You can try using this one: http://andrew.hedges.name/experiments/fullscreen/

    0 讨论(0)
  • 2020-12-16 05:39

    Working code sample:

    document.addEventListener("keyup", keyUpTextField, false);
    function keyUpTextField (e) 
    {
        var keyCode = e.keyCode;
        if(keyCode==122){/*ejecutaAlgo*/}
    }   
    
    0 讨论(0)
  • 2020-12-16 05:40

    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>

    0 讨论(0)
提交回复
热议问题