0

I want to prevent the default event on key #93 (select, between alt gr and ctrl right on AZERTY keyboard).

This key open context menu like right click.

I tried :

$(document).off('keydown');
$(document).off('keyup');
$(document).off('keypress');

$(document).on('keypress', function(e){
  if(e.keyCode == 93)
  {
     e.preventDefault();
     return false;
  }
});

$(document).on('keyup', function(e){
  if(e.keyCode == 93)
  {
     e.preventDefault();
     return false;
  }
});

$(document).on('keydown', function(e){
  if(e.keyCode == 93)
  {
     e.preventDefault();
     return false;
  }
});

Nothing works... I have always the contextmenu.

pagid
  • 13,559
  • 11
  • 78
  • 104
Matrix
  • 3,458
  • 6
  • 40
  • 76
  • http://jsfiddle.net/0kkm1vq0/1/ not sure, it is working here to me, unless you move out of the render (hence you go to the url bar or the css and html and js sections of jsfiddle, which are NOT considered as renders). In a nutshell, it seems to be working inside the dom itself, while you (luckily) can't disable it out of the DOM. Oh, as a side note, it was working with both .which and .keyCode, but you should use .which because.. this: http://stackoverflow.com/questions/4471582/javascript-keycode-vs-which – briosheje Jun 09 '15 at 16:40
  • this works on chrome, but I have context menu with FF 38.0.5 on your jsfiddle or on this too http://jsfiddle.net/0kkm1vq0/2/ – Matrix Jun 09 '15 at 16:49
  • we actually are looking for another event, not the keyboard events, see my answer below, it should be working. – briosheje Jun 09 '15 at 17:07

1 Answers1

1

After checking for a while, I've been headed to another question similar to this one, but with a very different matter.

In any case, since the problem is the context menu, you don't even need jQuery for such, and the solution (despite it WON'T always work in firefox because the user may set it to disable such) is this one:

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

fiddle:

http://jsfiddle.net/0kkm1vq0/3/

Works on chrome as well, and you won't need to use the keyboard listeners.

Reference: How to disable right-click context-menu in javascript

(which is really the same as key #93).

** note that this will disable the right click too **.

EDIT:

Not sure if this is cross-browser (the UPDATED code below seems to be working for both chrome and firefox, didn't try IE and others though), but the event fired by key #97 seems to be identified as 1, while the click seems to be identified as key 3, so you can just:

(function($){
    if (navigator.userAgent.toLowerCase().indexOf('chrome') > -1) {
        $(document).on('keyup', function(e) {
           e.which == 93 && e.preventDefault(); 
        });
    }
    else {
        document.oncontextmenu = function (e) {
         e.which == 1 && e.preventDefault();
        }   
    }
})(jQuery);

http://jsfiddle.net/0kkm1vq0/10/

To disable JUST the key and not the right click.

Community
  • 1
  • 1
briosheje
  • 7,356
  • 2
  • 32
  • 54
  • @Matrix: Yes, but be aware that it will disable the right click aswell, I'm trying to check if we can in someway make it to work in all modern browsers (IE, Firefox, Safari, Opera, Chrome). – briosheje Jun 09 '15 at 17:21