Get Mouse State without access to MouseEventArgs?

David picture David · May 30, 2012 · Viewed 20.9k times · Source

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;
}

Answer

LarsTech picture LarsTech · May 30, 2012

You can try checking with a timer:

private void timer1_Tick(object sender, EventArgs e) {
  this.Text = "Mouse Is " + (Control.MouseButtons == MouseButtons.Left);
}