Charles Munene
Charles Munene

Reputation: 1

C# Determine interval as mouse moves

Developing an application that will simulate mouse left drag when user stop moving the mouse for one second.

How can I make the mouse left up simulation wait untill the user stops moving the mouse and it remains in same position for one second?



namespace AutoClicker

{

  public partial class frmClicker : Form

  {

    [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]

    public static extern void mouse_event(uint dwFlags, uint dx, uint dy, uint cButtons, uint dwExtraInfo);

    //Mouse actions

    private const int MOUSEEVENTF_LEFTDOWN = 0x02;

    private const int MOUSEEVENTF_LEFTUP = 0x04;

    public frmClicker()

    {

      InitializeComponent();

    }

    private void LeftDrag()

    {

       mouse_event(MOUSEEVENTF_LEFTDOWN, X, Y, 0, 0);

       Thread.Sleep(100); //How can I make the thread sleep while mouse is moving. The ms should be determined by mouse movement

        mouse_event(MOUSEEVENTF_LEFTUP, X, Y, 0, 0);

    }

  }  

}

Upvotes: 0

Views: 77

Answers (1)

Mike Nakis
Mike Nakis

Reputation: 61931

The general strategy for waiting for some thing X to not happen for a certain period of time T is as follows:

Each time X happens, set up a timer to fire after time T. (If the timer already exists, destroy it and start a new one, or restart it with a new full duration of T.)

When the timer fires, you know that time T has elapsed since the last time that X happened.

It is kind of difficult for me to understand what you are using, because you seem to be mixing Windows Forms with Win32 API (why on earth are you doing this? why are you using Win32 API instead of Windows Forms mouse events?) but under Windows Forms, the documentation for timers (together with example code) is here: https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.timer?view=windowsdesktop-7.0

Upvotes: 0

Related Questions