C# Simulate mouse inputs

41 Views Asked by At

I'm developing an application that will help a handicapped person interact with the PC by simulating mouse inputs.

I've already achieved some progress but I have a challenge on Mouse Left Drag. I'd like the application to simulate Mouse Left Drag after 2 seconds on selecting "Mouse Left Drag" option and stop the drag 2 seconds after mouse stops moving. Here are some code that I'm working with:

  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;
    private const int MOUSEEVENTF_RIGHTDOWN = 0x08;
    private const int MOUSEEVENTF_RIGHTUP = 0x10;
    private const int MOUSEEVENTF_MIDDLEDOWN = 0x20;
    private const int MOUSEEVENTF_MIDDLEUP = 0x40;
    private const int MOUSEEVENTF_MOVE = 0x01;
    private const int MOUSEEVENTF_WHEEL = 0x0800;

    public frmClicker()
    {
    ``InitializeComponent();
    }

    public void DoMouseLeftClick()
    {
      Thread.Sleep(defaultDelay);
      uint X = (uint)Cursor.Position.X;
      uint Y = (uint)Cursor.Position.Y;
      Thread.Sleep(defaultDelay);
      mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, X, Y, 0, 0);
    }

    public void DoMouseLeftDoubleClick()
    {
      Thread.Sleep(defaultDelay);
      uint X = (uint)Cursor.Position.X;
      uint Y = (uint)Cursor.Position.Y;
      Thread.Sleep(defaultDelay);
      mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, X, Y, 0, 0);
      Thread.Sleep(50);
      mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, X, Y, 0, 0);
    }

    public void DoMouseLeftDrag()
    {
      Thread.Sleep(defaultDelay);
      uint X = (uint)Cursor.Position.X;
      uint Y = (uint)Cursor.Position.Y;
      Thread.Sleep(defaultDelay);
      mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTDOWN, X, Y, 0, 0);
      Thread.Sleep(dragRelease);
      mouse_event(MOUSEEVENTF_LEFTUP, X, Y, 0, 0);
    }

    public void DoMouseRightClick()
    {
      Thread.Sleep(defaultDelay);
      uint X = (uint)Cursor.Position.X;
      uint Y = (uint)Cursor.Position.Y;
      Thread.Sleep(defaultDelay);
      mouse_event(MOUSEEVENTF_RIGHTDOWN | MOUSEEVENTF_RIGHTUP, X, Y, 0, 0);
    }

    public void DoMouseRightDoubleClick()
    {
      Thread.Sleep(defaultDelay);
      uint X = (uint)Cursor.Position.X;
      uint Y = (uint)Cursor.Position.Y;
      Thread.Sleep(defaultDelay);
      mouse_event(MOUSEEVENTF_RIGHTDOWN | MOUSEEVENTF_RIGHTUP, X, Y, 0, 0);
      Thread.Sleep(50);
      mouse_event(MOUSEEVENTF_RIGHTDOWN | MOUSEEVENTF_RIGHTUP, X, Y, 0, 0);
    }  
  }
0

There are 0 best solutions below