Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Randomly move the mouse along a curve before clicking on a control element
#1
I want to implement a feature:
for example, I need to input three different pieces of text into Notepad in three separate steps. Before each input, the mouse pointer must click on a specific position within the Notepad window.  

Note: The pointer's movement trajectory must be unique each time, not a straight line, but a curved motion.

Is this possible? Thank you in advance for any suggestions and help.
#2
Code:
Copy      Help
MouseMoveRandomCurve(new(500, 500), 0.2);

static void MouseMoveRandomCurve(POINT xy, double curveFactor = 0.15) {
    var start = mouse.xy;
    int steps = 50 + Random.Shared.Next(30);
    double dx = xy.x - start.x, dy = xy.y - start.y;
    double dist = Math.Sqrt(dx * dx + dy * dy);
    if (dist < 1) return;
    
    // normalize direction
    double dirX = dx / dist, dirY = dy / dist;
    // perpendicular direction
    double px = -dirY, py = dirX;
    
    // random curve parameters
    double curveAmp = dist * curveFactor;              // user-controlled curvature
    double curvePhase = Random.Shared.NextDouble() * Math.PI * 2;
    double curveFreq = _NextDouble(0.7, 1.3);
    
    for (int i = 1; i <= steps; i++) {
        double t = (double)i / steps;
        double tt = t * t * (3 - 2 * t); // smooth ease-in-out
        
        double fade = Math.Sin(t * Math.PI); // zero at start/end
        double offset = Math.Sin(tt * Math.PI * curveFreq + curvePhase) * fade * curveAmp;
        
        double x = start.x + dx * tt + px * offset;
        double y = start.y + dy * tt + py * offset;
        
        // clamp to screen
        var r = screen.of((int)x, (int)y).Rect;
        x = Math.Clamp(x, r.left, r.right - 1);
        y = Math.Clamp(y, r.top, r.bottom - 1);
        
        mouse.move((int)x, (int)y);
        wait.ms(8);
    }

    mouse.move(xy);
    
    static double _NextDouble(double min, double max)
        => min + (max - min) * Random.Shared.NextDouble();
}

Thanks to ChatGPT.
#3
thank you so much!


Forum Jump:


Users browsing this thread: 1 Guest(s)