How to disable Windows keys (logo key and menu key) using Javascript

前端 未结 2 1062
小蘑菇
小蘑菇 2020-12-07 05:07

I write this Javascript code but it doesn\'t disable 2 windows keys (I mean logo key and menu key), though:

document.onkeydown = function(e) {
    document.t         


        
相关标签:
2条回答
  • 2020-12-07 06:02

    Your code looks right, try to find out real keycodes with this simple script:

    document.onkeydown = checkKeycode
    function checkKeycode(e) {
      var keycode;
      if (window.event) keycode = window.event.keyCode;
      else if (e) keycode = e.which;
      alert("keycode: " + keycode);
    }
    

    And to disabel certain keys you modify function (example for 'Enter'):

    document.onkeydown = checkKeycode
    function checkKeycode(e) {
      var event = e || window.event;
      var keycode = event.which || event.keyCode;
    
      if (keycode == 13) {
        // return key was pressed
      }
    }
    
    0 讨论(0)
  • 2020-12-07 06:08

    JavaScript cannot stop the effect of the Windows logo key, which (when released) is supposed to bring up the Window's start menu. In combination with other keys, it has other system wide effects (like with M = minimise all windows). This is something that happens outside of the browser context, and thus cannot and should not be blocked by the code running in your browser.

    The Windows menu key can be somewhat disabled, as described in this answer:

    $(function(){
        var lastKey=0;
        $(window).on("keydown", document, function(event){
            lastKey = event.keyCode;            
        });
    
        $(window).on("contextmenu", document, function(event){
            if (lastKey === 93){
                lastKey=0;
                event.preventDefault();
                event.stopPropagation();
                return false;
            }
        });
    });
    
    0 讨论(0)
提交回复
热议问题