sun
sun

Reputation: 1678

How to disable mouse right click menu when shift is being pressed in firefox?

I'm trying to disable the mouse right click option. So i used contextmenu bind function to prevent it. This works fine but when shift is pressed along with the mosue right click the contextmenu bind function is not triggering but it shows the contextmenu. Means am not getting the alert but it shows the menu.

Here is the code i tried.

$(document).ready(function(){
    $(document).bind("contextmenu",function(e){
   alert('Context Menu event has fired!');
   return false;
}); 
});

In order to capture the shift button press and mouse right click am doing the below code but this doesn't help. May be i am doing something wrong.

$(document).ready(function(){

    $(document).bind("contextmenu",function(e){
   alert('Context Menu event has fired!');
   return false;
}); 

    var shift = false;
jQuery(document).on("keydown", function(event) {
            //check for shift key is pressed
        if (event.which === 16) { 
        shift = true;
                        }
});
jQuery(document).mousedown(function(e) {
     // e.which === 3 is for mouse right click
   if (e.which === 3 && shift === true) {
console.log("both action are triggered");
       return false; // how to stop the contextmenu action here
                           }
});
});

I tried giving the e.preventDefault instead of return false. I think the context menu event itself is not triggering in firefox when shift is clicked.

How to disable the mouse right click in this situation for firefox? Any help or clue will be much helpful

JSFIDDLE NOTE This is not happening in chrome. This is happening in firefox only. Is this a bug?

Upvotes: 8

Views: 3756

Answers (2)

Srinivas Ramakrishna
Srinivas Ramakrishna

Reputation: 1335

Javascript code to disable mouse right click

 <script language="javascript">
    document.onmousedown=disableRightclick;
    status="Disabled";
    function disableRightclick(event)
    {
      if(event.button==2)
       {
         alert(status);
         return false;    
       }
    }
    </script>

On the HTML Body tag set the oncontextmenu property to false.

<body oncontextmenu="return false">
...
</body>

Disclaimer: The link provided is the blog which i have written. Hope this solves the problem.

Upvotes: 0

LaGoutte
LaGoutte

Reputation: 468

It's not a bug, it's a feature!

http://www.whatwg.org/specs/web-apps/current-work/multipage/interactive-elements.html#context-menus

User agents may provide means for bypassing the context menu processing model, ensuring that the user can always access the UA's default context menus. For example, the user agent could handle right-clicks that have the Shift key depressed in such a way that it does not fire the contextmenu event and instead always shows the default context menu.

You will not be able to do this in Firefox, by design. It's annoying, especially for complex web apps and games, but it's hard-coded into the browser and there's not way to disable it in javascript (that I know of).

Blame the standards, not Mozilla.

Upvotes: 5

Related Questions