0

I want when user enables full screen mode then user can't use any keyboard key, which means I will disable all keys when user enter in full screen mode. Please help me to solve this problem.

<pre><!DOCTYPE html>
<html>
 <head>
  <title>Page Title</title>
   <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>

    <script type="text/javascript">

      $(document).ready(function ()
       {
          $(document).keydown(function (event) {
          return false;
       });
    });
      document.oncontextmenu = function (e)        //check for the right click
     {
       return false;
     }
    document.ondragstart = function (e)
   {
     return false;
   }

   function toggleFullScreen() {

       var docElm = document.documentElement;
       if ((document.fullScreenElement && document.fullScreenElement !== null) ||(!document.mozFullScreen && !document.webkitIsFullScreen)) {
                                    if (docElm.requestFullscreen) {
                                        docElm.requestFullscreen();
                                    }
                                    else if (docElm.mozRequestFullScreen) {
                                        docElm.mozRequestFullScreen();
                                    }
                                    else if (docElm.webkitRequestFullScreen) {
                                        docElm.webkitRequestFullScreen();
                                    }
                                }
                                else {


                                    if (document.exitFullscreen) {
                                        document.exitFullscreen();
                                    }
                                    else if (document.mozCancelFullScreen) {
                                        document.mozCancelFullScreen();
                                    }
                                    else if (document.webkitCancelFullScreen) {
                                        document.webkitCancelFullScreen();
                                    }



                                }
                            }

    </script>
    </head>
    <body>

    <h1>This is a Heading</h1>
     <p>This is a paragraph.</p>
     <input type=text id="text"/>
     <button onclick="toggleFullScreen()" value="toggleFullScreen" 
      name="toggleFullScreen">toggleFullScreen</button>

  </body>
</html>

 </pre>

In above code I will add a toggler button to enable full screen mode and also place code for disable keyboard key full screen mode will be work and keyboard key is also working but ESC and Window key can't be disabled. Please help me to solve this problem.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129

2 Answers2

0

Update your keydown code as follows

   $(document).ready(function () {
      $(document).keydown(function (event) {
        var charCode = event.charCode || event.keyCode || event.which;
        if (charCode == 27 || charCode == 91 || charCode == 92) {
          alert("Escape and window keys are not allowed");
          return false;
        }
      });
    });
Aman
  • 828
  • 1
  • 10
  • 15
0

Well, if-else always looks a good idea in this situation but I will prefer this way. More readable and better approach

document.attachEvent("onkeydown", win_onkeydown_handler);

function win_onkeydown_handler() {
    switch (event.keyCode) {

    case 91: // 'Left Window'
         event.returnValue = false;
         event.keyCode = 0;
         break;  

    case 27: // 'Esc'
        event.returnValue = false;
        event.keyCode = 0;
        break;

    case 92: // 'Right Windows'
        event.returnValue = false;
         event.keyCode = 0;
         break;
        break;

    }
}

Modify it the way you want and you can see full list of key codes here

Kunal Vohra
  • 2,703
  • 2
  • 15
  • 33