Get Mouse State without access to MouseEventArgs?

24,250

Solution 1

You can try checking with a timer:

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

Solution 2

ChecK Control.MouseButtons static property:

if (Control.MouseButtons == MouseButtons.Left)
{
}
Share:
24,250
David
Author by

David

Hey! I'm David, co-founder at a design & dev startup called All Front. I love to build single page apps with the amazing design & dev team! I'm enthusiastic about Angular, React and progressive web apps.

Updated on June 01, 2020

Comments

  • David
    David almost 4 years

    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;
    }
    
    • banging
      banging almost 12 years
      Handle mouse events on the form rather than the individual controls.
    • paparazzo
      paparazzo almost 12 years
      @banging My experience is Forms does not bubble events up like WPF. Events on the form they only fire directly on the form. Mousedown on a control does not bubble to the form. If you have made this work at the form level then please show how.
    • RenniePet
      RenniePet about 9 years
      "I need to detect if the mouse is down or if it's up." Like, if it's feeling depressed or in a good mood? Or if it's not working today? (Sorry, I couldn't resist.)
  • David
    David almost 12 years
    MouseButtons < Just what I was looking for. Thanks!
  • user1703401
    user1703401 almost 12 years
    Implementing IMessageFilter is a way to turn that into events.
  • dhakim
    dhakim over 10 years
    Since it took me a while to figure out what that meant without name spaces, maybe this will help people: System.Windows.Forms.MouseButtons pressedButtons = System.Windows.Forms.Control.MouseButtons;
  • Admin
    Admin over 7 years
    Just curious, is there a way to achieve the exact same thing WITHOUT System.Windows.Forms? When developing with WPF, it would seem unnecessary to reference the additional library just to accomplish this specific task, especially if you know you won't need it for anything else.
  • Admin
    Admin over 7 years
    It appears you can use if (Mouse.LeftButton == MouseButtonState.Pressed) if developing with WPF, using System.Windows.Input.
  • bwall
    bwall over 4 years
    @JamesM, Good find. It's important to note that the System.Windows.Forms method can be run on any thread, while the System.Windows.Input one needs to be run on an STA thread (typically the GUI thread.)