Spark
Spark

Reputation: 1037

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

Upvotes: 2

Views: 2126

Answers (3)

RafaelMeritello
RafaelMeritello

Reputation: 1

public static void MoverSuavementePara(string arg)
{
    (int, int) pos = RegexHelper.pos_math(arg);
    int endX = (int)(pos.Item1 * Screen.PrimaryScreen.Bounds.Width);
    int endY = (int)(pos.Item2 * Screen.PrimaryScreen.Bounds.Height);
    (int, int) cord_start = NormalizarCoordenadas(Cursor.Position.X, Cursor.Position.Y); 
    int startX = cord_start.Item1;
    int startY = cord_start.Item2;
    
    int duracao = 890;

    DateTime startTime = DateTime.Now;

    while ((DateTime.Now - startTime).TotalMilliseconds <= duracao)
    {
        double progress = (DateTime.Now - startTime).TotalMilliseconds / duracao;
        int currentX = (int)(startX + progress * (endX - startX));
        int currentY = (int)(startY + progress * (endY - startY));

        Inputer.inputer.Mouse.MoveMouseTo(currentX, currentY);
        System.Threading.Thread.Sleep(10);
    }

    Inputer.inputer.Mouse.MoveMouseTo(endX, endY);
}


static (int, int) NormalizarCoordenadas(int x, int y)
{
    return (
        NormalizarCoordenada(x, SystemInformation.VirtualScreen.Left, SystemInformation.VirtualScreen.Right, 65535),
        NormalizarCoordenada(y, SystemInformation.VirtualScreen.Top, SystemInformation.VirtualScreen.Bottom, 65535)
    );
}

static int NormalizarCoordenada(int coordenada, int telaMin, int telaMax, int alvoMax)
{
    return (int)((double)(coordenada - telaMin) / (telaMax - telaMin) * alvoMax);
} 

Upvotes: 0

Max Young
Max Young

Reputation: 1662

FlaUI allows for the mouse to move smoothly. Here is the Mouse class in FlaUI for reference.

Upvotes: 0

Spark
Spark

Reputation: 1037

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.

Upvotes: 4

Related Questions