capturing simultaneous left and right mouse button clicks in javascript

javascript

Solution

You could track which mouse buttons are down with some boolean variables like this:

var leftButtonDown = false;
var rightButtonDown = false;

$(document).mousedown(function() {
    if(e.which == 1) {
        leftButtonDown = true;
    } else if (e.which == 3) {
        rightButtonDown = true;
    }
});

$(document).mouseup(function() {
    if(e.which == 1) {
        leftButtonDown = false;
    } else if (e.which == 3) {
        rightButtonDown = false;
    }
});

$(document).click(function() {
    if(leftButtonDown && rightButtonDown) {
        // left and right buttons are clicked at the same time
    }
});

If both booleans are true, the right and left mouse buttons are both being clicked.

Problem

What is the best way to capture a left AND right mouse click in javascript? I'm not using jQuery (it's next on my to-learn list), just javascript for now. Basically, I want to do something like ``` onClick()=javascript:rightClickFunction() // do right click function onContextMenu()=javascript:leftClickFunction() / onBoth() ??? ``` The only thing I could find on stackoverflow was: How to distinguish between left and right mouse click with jQuery How should I capture the double-button click? Can i check if the opposite button is also clicked during the R and L button routines?

Original source

Related problems