Search code examples
javascriptfirefoxcontextmenupreventdefaultstoppropagation

Shift-F10, prevent context-menu in Firefox


In my application I have a SHIFT-F10 keyboard handler like below:

<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
</head>
<body>
<script src="https://code.jquery.com/jquery-1.11.3.js"></script>
<script>
$(document).on('keyup keydown', function(e) {
  if (e.keyCode === 121 && e.shiftKey === true) {
    console.log('shift-f10 detected');
    e.preventDefault();
    e.stopPropagation();
    e.stopImmediatePropagation();
  }
});
</script>
</body>
</html>

But, in recent versions of Firefox (at least FF56), a context-menu is appearing in the top-right of the page:

context-menu appears when user presses shift-10

I've been unsuccessful in disabling the context-menu with:

e.preventDefault();
e.stopPropagation();
e.stopImmediatePropagation();

Is there a way to prevent this context-menu from appearing?


Solution

  • Try next code:

     $document.on('contextmenu',  (ev) => {
            return false;
     });
    

    or:

    $(document).on('keydown', function(e) {
      if (e.keyCode === 121 && e.shiftKey === true) {
        return false;
      }
    });
    

    Maybe you can use :

     document.body.addEventListener('keydown',function(ev){
          if (ev.keyCode === 121 && ev.shiftKey === true)
                   {
                     ev.preventDefault();
                   }
        });
    

    If this code don't work - focus don't locate in browser, or you have other errors. Try it code.