how to unbind/prevent context menu by keyboard (key #93) with FF?

1.1k views Asked by At

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.

1

There are 1 answers

1
briosheje On BEST ANSWER

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.