Simulate mouse movement in C # between two coordinates

I am trying to programmatically move the mouse between two coordinates. But I want to reliably maintain speed on all fast or slow processing machines. I saw this link here . But this does not guarantee me the optimal, smooth and visible cursor speed when simulating the movement between two coordinates. I wonder if anyone knows a trick to determine parameters such as delay and steps, the optimal value for different machines. Like my first idea, to use it as a loop for a particular iterator to determine the performance of the machine, then evaluate the parameters depending on how long it took for the -Lop ... idea? or am I completely wrong about this? Thanks

+3
source share
3 answers

You must make motion a function of time. Starting with the answer in C #, moving the mouse around realistically , and using the stopwatch class to measure elapsed time:

public void LinearSmoothMove(Point newPosition, TimeSpan duration) 
{
    Point start = GetCursorPosition();

    // Find the vector between start and newPosition
    double deltaX = newPosition.X - start.X;
    double deltaY = newPosition.Y - start.Y;

    // start a timer
    Stopwatch stopwatch = new Stopwatch();
    stopwatch.Start();

    double timeFraction = 0.0;

    do
    {
        timeFraction = (double)stopwatch.Elapsed.Ticks / duration.Ticks;
        if (timeFraction > 1.0)
            timeFraction = 1.0;

        PointF curPoint = new PointF(start.X + timeFraction * deltaX, 
                                     start.Y + timeFraction * deltaY);
        SetCursorPosition(Point.Round(curPoint));
        Thread.Sleep(20);
    } while (timeFraction < 1.0);
}
+3
source

I would recommend some physics. Speed ​​is the distance divided by time. If you want a consistent mouse speed on each machine, you must get the exact time.

Here is an example:

You want to move the mouse from point 0/0 to 400/600, and the end point should always be done after 3 seconds.

To do this, you need to save the start time and build a while loop that will end from the moment it starts + 3 seconds. In the loop, you calculate the X and Y coordinates from the elapsed and total times.

X = 400 / 3s * ElapsedTime
Y = 600 / 3s * ElapsedTime

. , Stopwatch.

0

I tried this one, but still not optimal. It still depends on the processing power of the machine. @Justin, use a different value for the duration and time of sleep. Let me know if you come up with a better solution after you have tested it. Thank!

using System;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.Diagnostics;
using System.Threading;

namespace ConsoleApplication11
{
   class Program
    {

       [DllImport("user32.dll")]
       static extern bool SetCursorPos(int X, int Y);   

       public static void LinearSmoothMove(Point newPosition, TimeSpan duration)
       {
           Point start = Cursor.Position;
           int sleep = 10;
           //PointF iterPoint = start;
           // Find the vector between start and newPosition   
           double deltaX = newPosition.X - start.X;
           double deltaY = newPosition.Y - start.Y;
           // start a timer    
           Stopwatch stopwatch = new Stopwatch();
           stopwatch.Start();
           double timeFraction = 0.0;
           do
           {
               timeFraction = (double)stopwatch.Elapsed.Ticks / duration.Ticks;
               if (timeFraction > 1.0)
                timeFraction = 1.0;
               PointF curPoint = new PointF((float)(start.X + timeFraction * deltaX), (float)(start.Y + timeFraction * deltaY));
               SetCursorPos(Point.Round(curPoint).X, Point.Round(curPoint).Y);
               Thread.Sleep(sleep);
           } while (timeFraction < 1.0);
       }
       static void Main(string[] args)
       {
             TimeSpan delayt = new  TimeSpan(0, 0, 3);
           LinearSmoothMove(new Point(20, 40), delayt);
           Console.Read();
        }       
    }
}
0
source

All Articles