Results 1 to 2 of 2
Like Tree1Likes
  • 1 Post By dexon

Thread: Human Wind Mouse Movement class

  1. #1
    Member
    dexon is offline

    Posts
    36

    Human Wind Mouse Movement class

    Hey, sharing the human wind mouse movement script I found to make life easier for some devs. I added the callback.
    Making it harder getting detected for mouse bots.

    Showcase:
    #1:


    #2:


    #3:


    Code:
    Code:
    static class HWMM {
        static Random random = new Random();
        static int mouseSpeed = 8;
    
        static void Main(string[] args) {
            MoveMouse(0, 0, 0, 0);
        }
    
        static void MoveMouse(int x, int y, Action callback = new Action(() => {})) {
            int rx = 10, int ry = 10;
    
            Point c = new Point();
            GetCursorPos(out c);
    
            x += random.Next(rx);
            y += random.Next(ry);
    
            double randomSpeed = Math.Max((random.Next(mouseSpeed) / 2.0 + mouseSpeed) / 10.0, 0.1);
    
            WindMouse(c.X, c.Y, x, y, 9.0, 3.0, 10.0 / randomSpeed, 15.0 / randomSpeed, 10.0 * randomSpeed, 10.0 * randomSpeed, callback); 
        }
    
        static void WindMouse(double xs, double ys, double xe, double ye,
            double gravity, double wind, double minWait, double maxWait,
            double maxStep, double targetArea) {
    
            double dist, windX = 0, windY = 0, veloX = 0, veloY = 0, randomDist, veloMag, step;
            int oldX, oldY, newX = (int)Math.Round(xs), newY = (int)Math.Round(ys);
    
            double waitDiff = maxWait - minWait;
            double sqrt2 = Math.Sqrt(2.0);
            double sqrt3 = Math.Sqrt(3.0);
            double sqrt5 = Math.Sqrt(5.0);
    
            dist = Hypot(xe - xs, ye - ys);
    
            while (dist > 1.0) {
    
                wind = Math.Min(wind, dist);
    
                if (dist >= targetArea) {
                    int w = random.Next((int)Math.Round(wind) * 2 + 1);
                    windX = windX / sqrt3 + (w - wind) / sqrt5;
                    windY = windY / sqrt3 + (w - wind) / sqrt5;
                }
                else {
                    windX = windX / sqrt2;
                    windY = windY / sqrt2;
                    if (maxStep < 3)
                        maxStep = random.Next(3) + 3.0;
                    else
                        maxStep = maxStep / sqrt5;
                }
    
                veloX += windX;
                veloY += windY;
                veloX = veloX + gravity * (xe - xs) / dist;
                veloY = veloY + gravity * (ye - ys) / dist;
    
                if (Hypot(veloX, veloY) > maxStep) {
                    randomDist = maxStep / 2.0 + random.Next((int)Math.Round(maxStep) / 2);
                    veloMag = Hypot(veloX, veloY);
                    veloX = (veloX / veloMag) * randomDist;
                    veloY = (veloY / veloMag) * randomDist;
                }
    
                oldX = (int)Math.Round(xs);
                oldY = (int)Math.Round(ys);
                xs += veloX;
                ys += veloY;
                dist = Hypot(xe - xs, ye - ys);
                newX = (int)Math.Round(xs);
                newY = (int)Math.Round(ys);
    
                if (oldX != newX || oldY != newY)
                    SetCursorPos(newX, newY);
    
                step = Hypot(xs - oldX, ys - oldY);
                int wait = (int)Math.Round(waitDiff * (step / maxStep) + minWait);
                Thread.Sleep(wait);
            }
    
            int endX = (int)Math.Round(xe);
            int endY = (int)Math.Round(ye);
            if (endX != newX || endY != newY)
                SetCursorPos(endX, endY);
    
            callback();
        }
    
        static double Hypot(double dx, double dy) {
            return Math.Sqrt(dx * dx + dy * dy);
        }
    
        [DllImport("user32.dll")]
        static extern bool SetCursorPos(int X, int Y);
    
        [DllImport("user32.dll")]
        public static extern bool GetCursorPos(out Point p);
    }
    Usage:
    Code:
    /*async with callback*/
    HWMM.MoveMouse(x, y, new Action(() => { Debug.WriteLine("Mouse movement completed!"); }));
    
    /*async without callback*/
    HWMM.MoveMouse(x, y);
    Sources:
    c# - Human-like mouse movement - Code Review Stack Exchange
    C# moving the mouse around realistically - Stack Overflow
    pas likes this.

  2. #2
    pas
    Member
    pas is offline

    Posts
    37
    Thanks for this.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •