2017-07-23 113 views
0

我用一個函數在瀏覽器中使用javascript切換全屏模式。全屏禁用所有鍵盤按鍵

function toggleFullScreen(elem) { 
    // ## The below if statement seems to work better ## if ((document.fullScreenElement && document.fullScreenElement !== null) || (document.msfullscreenElement && document.msfullscreenElement !== null) || (!document.mozFullScreen && !document.webkitIsFullScreen)) { 
    if ((document.fullScreenElement !== undefined && document.fullScreenElement === null) || (document.msFullscreenElement !== undefined && document.msFullscreenElement === null) || (document.mozFullScreen !== undefined && !document.mozFullScreen) || (document.webkitIsFullScreen !== undefined && !document.webkitIsFullScreen)) { 
     if (elem.requestFullScreen) { 
      elem.requestFullScreen(); 
     } else if (elem.mozRequestFullScreen) { 
      elem.mozRequestFullScreen(); 
     } else if (elem.webkitRequestFullScreen) { 
      elem.webkitRequestFullScreen(Element.ALLOW_KEYBOARD_INPUT); 
     } else if (elem.msRequestFullscreen) { 
      elem.msRequestFullscreen(); 
     } 
    } else { 
     if (document.cancelFullScreen) { 
      document.cancelFullScreen(); 
     } else if (document.mozCancelFullScreen) { 
      document.mozCancelFullScreen(); 
     } else if (document.webkitCancelFullScreen) { 
      document.webkitCancelFullScreen(); 
     } else if (document.msExitFullscreen) { 
      document.msExitFullscreen(); 
     } 
    } 
} 

並且創建了一個函數來在全屏模式下禁用鍵盤按鍵。 大多數鍵都被禁用,但仍有一些鍵仍在工作(F11,ESC,ALT + Tab)。

function disable() 
{ 

document.onkeydown = function (e) 
{ 
    return false; 
} 
} 

反正還有禁用它們嗎?因爲我正在開發一個在線考試,該考試仍應全屏顯示,直到考試結束。

+0

Alt-tab或CTRL-ALT-DELETE被操作系統接管,所以我懷疑是否有任何阻止這些操作的方法。 – apokryfos

回答

0

這裏是Disabling keys in Windows

答案您不能禁用CTRL + ALT + DELETE 是Windows專用鍵。

你不能禁止ALT + F4 ALT + TAB 作爲爲那些還特殊鍵。

你應該沒有任何理由通過Javascript禁用這些功能。

0
<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> 
+0

F11和ESC仍然在全屏模式下工作。 –