I hate this mess with the mouse buttons created by W3C an MS! I want to know if the left mouse button is pressed when I get a mousedown event.
I use this code
// 0 left, 2 right, 1 middle, other.. extra buttons, gaming mouses
var buttonsArray = [false, false, false, false, false, false, false, false, false];
var mousePressed = false;
document.onmousedown = function(e) {
buttonsArray[e.button] = true;
mousePressed = true;
};
document.onmouseup = function(e) {
buttonsArray[e.button] = false;
mousePressed = false;
};
document.oncontextmenu = function() {
return false;
}
Explanation: When mouse is down, we change to true the pressed button in our buttons array. When mouse is up, we change to false the pressed button to false.
Now we can establish which button is pressed more accurately, but we have a right click problem.. because with that button we open a contextmenu at the browser, and that escapes our control... so, we disable the context menu in order to properly detect right click. If we don't do that, we must resolve left click too... and its a complication that escapes this response.
In order to simplify things, we can add another variable mousePressed
and flag if mouse is down or up.
Works perfect on chrome, I didn't test it in other browser but I guess its ok in firefox and opera too... IE??? I don't care IE.
Enjoy it!