I need a timer that checks every x ammount of seconds if the mousebutton is down.

How would I do this?

Oh, and it must be global. Not just on the form.

Recommended Answers

All 5 Replies

Implement a MouseDown and MouseUp event handler to set a global boolean flag, to be checked by your timer handler method.

Make yourself a little mouse value structure:

public struct MouseInfo
  {
    private int _ctrlStatus;
    private bool _left;
    private bool _middle;
    private bool _right;
    private bool _leftAndRight;

    public int ControlStatus { get { return _ctrlStatus; } }
    public bool Left { get { return _left; } }
    public bool Right { get { return _right; } }
    public bool Middle { get { return _middle; } }
    public bool LeftAndRight { get { return _leftAndRight; } }


    public MouseInfo(int ctrlStatus)
    {
      _ctrlStatus = ctrlStatus;
      _left = ((ctrlStatus & (int)MouseButtons.Left) == (int)MouseButtons.Left);
      _right = ((ctrlStatus & (int)MouseButtons.Right) == (int)MouseButtons.Right);
      _middle = ((ctrlStatus & (int)MouseButtons.Middle) == (int)MouseButtons.Middle);

      //This is duplicate information, just showing how to bitwise compare two values
      _leftAndRight = ((ctrlStatus & (int)(MouseButtons.Left | MouseButtons.Right)) == (int)(MouseButtons.Left | MouseButtons.Right));
      //_leftAndRight == (_left && _right);
    }
    public MouseInfo(MouseButtons btns)
      : this((int)btns)
    {
    }

    public override string ToString()
    {
      return string.Format(
        "Left: {0} | " +
        "Right: {1} | " +
        "Middle: {2} | " +
        "LeftAndRight: {3} | {4:F0}",
        BoolToString(Left), BoolToString(Right), BoolToString(Middle), BoolToString(LeftAndRight), this.ControlStatus);
    }

    private static string BoolToString(bool val)
    {
      return val ? "Yes" : "No ";
    }
  }

Then pass it the control's value:

public partial class frmMouse : Form
  {
    //So we can reference from a static method
    //to ensure this is not related to events
    private static frmMouse _frm; 
    public frmMouse()
    {
      InitializeComponent();
      _frm = this;
    }

    private void frmMouse_MouseMove(object sender, MouseEventArgs e)
    {
      DoMouseStuff();
    }

    private static void DoMouseStuff()
    {
      MouseInfo mi = new MouseInfo(Control.MouseButtons);
      _frm.Text = mi.ToString();
    }
  }

That's great code scott, but if you just want to know all the time the condition of the mouse buttons. even when the form is not focused, you will need to implement a low level Mouse hook.

here is a link
http://www.codeproject.com/KB/cs/globalhook.aspx
I recommend version 1, not 2.

if you need this information when your application is the active window I do recommend Scott's solution, but Global to the system, you will need hooks.

best of luck.

Thats my problem, the mouse hook isn't fast enough to recognise when the mouse button is released, so I'm intending to create a failsafe for it.

Is there maybe a medium level mouse hook? :)

Can you post a sample project that demonstrates the behavior?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.