javascriptwindowswindows-key

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


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.title = e.keyCode;
    if (e.keyCode == 91 || e.keyCode == 93) {
        window.event.keyCode = 0;
        window.event.returnValue = false;
        return false;
    }
};

the 2 window.xxx statements are actually not necessary but I add them in to buy an insurance (Just doubt that e doesn't totally equal to window.event).

So I'd like to ask this question: " Is there a feasible way, directly or indirectly, to do this job in Javascript? "


Solution

  • 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
      }
    }