How can I detect a rightmouse button event on mousedown?

javascript mouse hold event
mousedown event javascript
onmousedown vs onclick
trigger mousedown event javascript
onmousedown right click javascript
mousedown event button
mousedown event w3schools
javascript button events

I am updating/debugging and expanding the capabilities of my draggable script and I need to be able to achieve the following result:

whatever.onRightMouseButtonMousedown = preventDefault();

I have done a lot of research to no avail, however, I know that it is possible to do this because when I use jquery-ui draggable, it prevents the ability to drag elements when you mousedown with the right mouse button. I want to mimic this ability, and learn how it works so that I can implement it now and in the future as needed.

Note: Please do not give me information about how to use jquery or jquery-ui draggable (I already am familiar with it), I want to learn how they implemented, or how it is possible to implement the detection of a mousedown with the right mouse button so that I can prevent elements from being draged with the right mouse button.

Normally when you have any kind of mouse event, you'll want to have it only operate on one type of mouse click. Therefore, the events passed to your callbacks have a property which allow you to distinguish between types of clicks.

This data is passed back through the button property of the event data. See MDN for finding out what values represent what data.

Therefore, you don't disable the right click, you instead only enable your functionality for the left click. Here's a [poor] example:

element.onmousedown = function(eventData) {
  if (eventData.button === 1) {
      alert("From JS: the (left?) button is down!")
  }
}  

the equivalent in jQuery is:

$("div").mousedown(function(eventData) {
    if (eventData.which === 1) {
        alert("From JQuery: which=" + de.which)
    }
});

Note that if you don't use jquery, the values returned will be different across browsers. jQuery unifies the values across browsers, using 1 for left, 2 for middle, 3 for right:

 element.onmousedown = function(eventData) {
   if (eventData.button === 0) {
     console.log("From JS: the (left?) button is down!")
   }
 }

 $("#element").ready(function() {
   $("div").mousedown(function(de) {
     console.log("From JQuery: which=" + de.which);
   });
 });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="element" style="width: 100px; height: 100px; background-color: blue" />

jQuery mousedown() Method, Which event among them is fired when the right mouse button is released? Detecting Ctrl+Left (mouse button) in MouseDown event handler. When I first press down control key (the left one) and then click the left mouse button, why does the following code gets executed. I am modifying existing code and the below code is already there.

The html:

<a href="#" onmousedown="mouseDown(event);">aaa</a>​​​​​​​​​​​​​​​​​​​​​​​​​​​

The javascript:

function mouseDown(e) {
  e = e || window.event;
  switch (e.which) {
    case 1: alert('left'); break;
    case 2: alert('middle'); break;
    case 3: alert('right'); break; 
  }
}​

The demo.

The MouseEvent, The event occurs when the user right-clicks on an element to open a context menu Detect right mouse click on cocoa. does not send right mouse down events up to the responder chain, How to catch right mouse button events with my SKScene.

It's not very simple. Quirksmode.org has an article about event properties.

Look under 'Which mouse button has been clicked?' / 'Right click'. It varies by browser.

VBScript - Events, The event occurs when the user double-clicks on an element As others have mentioned, the right mouse button can be detected through the usual mouse events (mousedown, mouseup, click).However, if you're looking for a firing event when the right-click menu is brought up, you're looking in the wrong place.

DOM event handlers - MDN, The event occurs when the user presses a mouse button over an element I have a method to detect the left click event that visual studio made by double clicking on the form. I want to have a right-click event by right-clicking on the same object. I read online that you can use this switch: The trouble is that when I do this e.Button has has a red line and error: "'System.EventArgs' does not contain a definition

How can I detect a rightmouse button event on mousedown?, Which event of the command button triggers when user clicks left mouse button? If you're just using the Button's Click event, then the only mouse button that will fire it is the primary mouse button. If you still need to know specifically whether it was the left or right button, then you can use the SystemInformation to obtain it.

Mouse events, in HTML or properties in Web APIs, such as <button onclick="alert(this)"> or window. 3 Answers 3. Normally when you have any kind of mouse event, you'll want to have it only operate on one type of mouse click. Therefore, the events passed to your callbacks have a property which allow you to distinguish between types of clicks. This data is passed back through the button property of the event data.

Comments
  • This is not a duplicate. The question is specifically asking how to do it without using jQuery.
  • Thank you, I actually already figured this out before coming back to check your comment, I would simply add that to make it work across browsers (at least modern browsers anyway) you just need to check for event.which and event.button, this is how I ended up resolving it: if (evt.which === 3 || evt.button === 2) {evt.preventDefault();
  • FYI, for the left mouse button event.button === 0, for the mousewheel event.button === 1, and for the right mouse button event.button === 2 as explained at developer.mozilla.org/en-US/docs/Web/API/MouseEvent/…
  • I know this is from a while ago, but I clicked that link and saw the line "Not even explorer 6 supports it yet" meaning this article is ANCIENT.