Is right click a Javascript event?

后端 未结 18 2112
南笙
南笙 2020-11-22 10:02

Is right click a Javascript event? If so, how do I use it?

相关标签:
18条回答
  • 2020-11-22 10:38

    No, but you can detect what mouse button was used in the "onmousedown" event... and from there determine if it was a "right-click".

    0 讨论(0)
  • 2020-11-22 10:38

    Yes - it is!

    function doSomething(e) {
        var rightclick;
        if (!e) var e = window.event;
        if (e.which) rightclick = (e.which == 3);
        else if (e.button) rightclick = (e.button == 2);
        alert('Rightclick: ' + rightclick); // true or false
    }
    
    0 讨论(0)
  • 2020-11-22 10:39

    Yes, oncontextmenu is probably the best alternative but be aware that it triggers on mouse down whereas click will trigger on mouse up.

    Other related questions were asking about double right click - which apparently isn't supported except through manual timer checking. One reason you might want to be able to have right double click is if you need/want to support left-handed mouse input (button reversal). The browser implementations seem to make a lot of assumptions about how we should be using the available input devices.

    0 讨论(0)
  • 2020-11-22 10:40

    have a look at the following jQuery code:

    $("#myId").mousedown(function(ev){
          if(ev.which == 3)
          {
                alert("Right mouse button clicked on element with id myId");
          }
    });
    

    The value of which will be:

    • 1 for the left button
    • 2 for the middle button
    • 3 for the right button
    0 讨论(0)
  • 2020-11-22 10:41

    The following code is using jQuery to generate a custom rightclick event based on the default mousedown and mouseup events. It considers the following points:

    • trigger on mouseup
    • trigger only when pressed mousedown on the same element before
    • this code especially also works in JFX Webview (since the contextmenu event is not triggered there)
    • it does NOT trigger when the contextmenu key on the keyboard is pressed (like the solution with the on('contextmenu', ...) does

    $(function ()
    { // global rightclick handler - trigger custom event "rightclick"
    	var mouseDownElements = [];
    	$(document).on('mousedown', '*', function(event)
    	{
    		if (event.which == 3)
    		{
    			mouseDownElements.push(this);
    		}
    	});
    	$(document).on('mouseup', '*', function(event)
    	{
    		if (event.which == 3 && mouseDownElements.indexOf(this) >= 0)
    		{
    			$(this).trigger('rightclick');
    		}
    	});
    	$(document).on('mouseup', function()
    	{
    		 mouseDownElements.length = 0;
    	});
        // disable contextmenu
        $(document).on('contextmenu', function(event)
    	{
    		 event.preventDefault();
    	});
    });
    
    
    
    // Usage:
    $('#testButton').on('rightclick', function(event)
    {
      alert('this was a rightclick');
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <button id="testButton">Rightclick me</button>

    0 讨论(0)
  • 2020-11-22 10:42

    If you want to detect right mouse click, you shouldn't use MouseEvent.which property as it is non-standard and there's large incompatibility among browsers. (see MDN) You should instead use MouseEvent.button. It returns a number representing a given button:

    • 0: Main button pressed, usually the left button or the un-initialized state
    • 1: Auxiliary button pressed, usually the wheel button or the middle button (if present)
    • 2: Secondary button pressed, usually the right button
    • 3: Fourth button, typically the Browser Back button
    • 4: Fifth button, typically the Browser Forward button

    MouseEvent.button handles more input types than just standard mouse:

    Buttons may be configured differently to the standard "left to right" layout. A mouse configured for left-handed use may have the button actions reversed. Some pointing devices only have one button and use keyboard or other input mechanisms to indicate main, secondary, auxilary, etc. Others may have many buttons mapped to different functions and button values.

    Reference:

    1. https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent/which
    2. https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent/button
    0 讨论(0)
提交回复
热议问题