Search code examples
c#ui-automationinputsimulator

InputSimulator smooth linear mouse movement between 2 points


I'm struggling to figure out why the below function is only ever moving my mouse from 0, 0 screen coords, to my final destination, even though Cursor.Position is returning the correct screen coords. If anybody could enlighten me i'd be most grateful.

public void MoveAndClick(int x, int y, int steps)
{
    Point start = Cursor.Position;
    PointF iterPoint = start;

    PointF slope = new PointF(x - start.X, y - start.Y);
    slope.X = slope.X / steps;
    slope.Y = slope.Y / steps;

    for(int i=0; i<steps; i++)
    {
        iterPoint = new PointF(iterPoint.X + slope.X, iterPoint.Y + slope.Y);
        inputSim.Mouse.MoveMouseTo(iterPoint.X, iterPoint.Y);
        Thread.Sleep(10);
    }

    inputSim.Mouse.MoveMouseTo(x, y);
    inputSim.Mouse.RightButtonDown();
    inputSim.Mouse.RightButtonUp();
}

Taken from https://stackoverflow.com/a/913703/2014393


Solution

  • DOH! Always stop working when you're sleepy and go to bed.

    The InputSimulator library requires that you translate coordinates like so:

    int startX = 65535 * Cursor.Position.X / Screen.PrimaryScreen.Bounds.Width;
    int startY = 65535 * Cursor.Position.Y / Screen.PrimaryScreen.Bounds.Height;
    

    I was converting the end coordinates but completely forgot to translate the starting coordinates, derp.