Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Mouse State without access to MouseEventArgs?

I have a form with many, many controls. I need to detect if the mouse is down or if it's up. Most of the time, I don't have MouseEventArgs.

Is there a quick and easy way to tell if the mouse is down without mouseEventArgs?

Is there an alternative, or is something like this the only way?:

foreach (Control c in this.Controls)
{
    c.MouseUp += new MouseEventHandler(globalMouseUp);
    c.MouseDown += new MouseEventHandler(globalMouseDown);
}


bool isMouseUp = true;


private void globalMouseDown(object sender, MouseEventArgs e)
{
    isMouseUp = false;
}

private void globalMouseUp(object sender, MouseEventArgs e)
{
    isMouseUp = true;
}
like image 582
David Avatar asked May 30 '12 16:05

David


2 Answers

You can try checking with a timer:

private void timer1_Tick(object sender, EventArgs e) {
  this.Text = "Mouse Is " + (Control.MouseButtons == MouseButtons.Left);
}
like image 61
LarsTech Avatar answered Nov 09 '22 18:11

LarsTech


ChecK Control.MouseButtons static property:

if (Control.MouseButtons == MouseButtons.Left)
{
}
like image 20
Eng. M.Hamdy Avatar answered Nov 09 '22 19:11

Eng. M.Hamdy