1

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? "

Scott Chu
  • 972
  • 14
  • 26
  • `01` seems odd. Does it work for one key or the other? – Cfreak May 15 '13 at 03:40
  • would you like to tell what key r u going to trap? – Surendra Jnawali May 15 '13 at 03:42
  • You might toss an `e.stopPropagation();` in there right before you return for good measure. Also, as mentioned the 01 is kinda weird. Here's a [list of keycodes](http://www.cambiaresearch.com/articles/15/javascript-char-codes-key-codes) – dinjas May 15 '13 at 03:47
  • @ SJnawali:It's those 2 Windows keys (Window logo key & Windows context-menu key) I want to trap. Wiki calls them "Windows key". To be more clear, I'll modify the title to be "start key" and "menu key". – Scott Chu May 15 '13 at 03:51
  • @ dinjas: It's a typo and already correct it to 91. Thanks for pointing out the typo. I tried the e.stopPropagation() but it doesn't help. – Scott Chu May 15 '13 at 03:54
  • @Cfreak: 01 is typo. Correction is done. Thanks for pointing out. – Scott Chu May 15 '13 at 04:13
  • I'm not sure this is possible in a cross-browser sort of manner. Was just looking at [this](http://unixpapa.com/js/key.html) (search page for "Windows keyboards"). – dinjas May 15 '13 at 06:25

2 Answers2

1

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
  }
}
RobG
  • 142,382
  • 31
  • 172
  • 209
zolter
  • 7,070
  • 3
  • 37
  • 51
  • I'm pretty sure logo key is code 91 & menu key is 93. The previous 01 is a typo. – Scott Chu May 15 '13 at 03:58
  • 3
    My code does exactly funtion as yours. However, Windows keys are special case. I also tried your codes (change 13 to 91 or 93) and it doesn't block Windows keys, either. – Scott Chu May 15 '13 at 04:17
  • Do you find solution to achieve this? – Kishore Indraganti Oct 15 '16 at 11:21
  • @Zolter I tried your code . 91 is for windows. But it is not stopping the windows key, Could you please help me out how to disable all key board key when my application page is open. here is the code i am trying and not able to stop windows key stroke.document.onkeydown = checkKeycode function checkKeycode(e) { var event = e || window.event; var keycode = event.which || event.keyCode; if (keycode == 91) { window.event.keyCode = 0; window.event.returnValue = false; return false; // return key was pressed } } – sashikanta Mar 16 '17 at 14:34
  • You can checkout http://keycode.info/ to easily find your key codes (as opposed to using an alert which is slightly cumbersome) – Mirage Jun 15 '17 at 09:45
1

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;
        }
    });
});
Community
  • 1
  • 1
trincot
  • 317,000
  • 35
  • 244
  • 286