Detect both left and right mouse click at the same time?
Solution 1
Create a class boolean variable for the left and right button defaulted to false. When the mouse down event fires set the variable to true and check if both are true. When the mouse up fires set the variable to false.
public bool m_right = false;
public bool m_left = false;
private void MainForm_MouseDown(object sender, MouseEventArgs e)
{
m_objGraphics.Clear(SystemColors.Control);
if (e.Button == MouseButtons.Left)
m_left = true;
if (e.Button == MouseButtons.Right)
m_right = true;
if (m_left == false || m_right == false) return;
//do something here
}
private void MainForm_MouseUp(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
m_left = false;
if (e.Button == MouseButtons.Right)
m_right = false;
}
Solution 2
Complete Code:
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left) leftPressed = true;
else if (e.Button == MouseButtons.Right) rightPressed = true;
if (leftPressed && rightPressed)
{
MessageBox.Show("Hello");
// note:
// the following are needed if you show a modal window on mousedown,
// the modal window somehow "eats" the mouseup event,
// hence not triggering the MouseUp event below
leftPressed = false;
rightPressed = false;
}
}
private void Form1_MouseUp(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left) leftPressed = false;
else if (e.Button == MouseButtons.Right) rightPressed = false;
}
Solution 3
Another option is to use the static MouseButtons on the System.Windows.Forms.Control class
This will tell you which mouse buttons are currently pressed so that you can do something like the following:
((Control.MouseButtons & MouseButtons.Right) == MouseButtons.Right) &&
((Control.MouseButtons & MouseButtons.Left) == MouseButtons.Left)
You can also check out the MSDN example
Solution 4
I got the following code to work in my Click Event.
if ((Control.MouseButtons == MouseButtons.Right) || (Control.MouseButtons == MouseButtons.Left))
When only one mouse button is being pressed the "Control.MouseButton" assumes the value of "MouseButtons.None"
But when both the left and right mouse buttons are being pressed the "Control.MouseButton" assumes the value of either "MouseButtons.Right" or "MouseButtons.Left" according to which was pressed first / last (depending on how long time it was between the left and right button being pressed)
Related videos on Youtube
Corey Ogburn
Updated on April 27, 2022Comments
-
Corey Ogburn about 2 years
I'm remaking windows Minesweeper (from XP) and something they had included was that if you click a number with as many flags as it's number with the left and right mouse button at the same time, it reveals every other hidden tile around that number.
I'm having a hard time telling when both the Left and Right mouse buttons are pressed at the exact same time... I'm using a pair of bools, one for each button with the OnMouseDown and OnMouseUp events but if the 2 buttons are clicked at the exact same time (or really close), then only one MouseDown event goes off and the other does not... If you click and hold one of the buttons then click and hold the other, the code works though.
Is there a better way to detect this kind of "dual" click?
Edit:
Alright, small story for why I messed this up (it worked all along).
I have a macbook pro running Windows 7. For those of you who don't know, the macbook pro has a single bar for a mouse button that normally left clicks, but if you place 2 fingers down it right clicks, so you can't do both (and no way to middle click). So I was building my app and sending it to my friend, he was telling me it wasn't working, so I posted this question. I finally decided to try it on my other laptop, a Dell XPS with 2 mouse buttons... Once it worked there I passed it along to other friends and they confirmed it worked. I don't know how my first friend messed it up, but moral of the story is don't buy anything Apple. At least that's the moral I got.
-
cHao almost 14 yearsNot going to work. e.Button can't have two values at once. It's probably a bitmask or something, though, so
If e.Button And (MouseButtons.Left Or MouseButtons.Right) = (MouseButtons.Left Or MouseButtons.Right)
may work. -
Corey Ogburn almost 14 yearsI've tried both of those. They're not flags, they're separate entities and it can only be one at a time.
-
Corey Ogburn almost 14 yearsI marked you as the answer because in the end, that's the code that's in my app... even if it was the code I had all along. Stupid mac...
-
James over 7 yearsYou can also use the Control class to retrieve the current mouse position outside of any event handlers which can also come in pretty handy. Control.MousePosition
-
Larry over 4 yearsThis actually works. I believe the best is to use the very readable HasFlag() method this way: (Control.MouseButtons.HasFlag(MouseButtons.Left) && Control.MouseButtons.HasFlag(MouseButtons.Right))