Detect both left and right mouse click at the same time?
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;
}
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;
}
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