Advertisement
Guest User

Natural Mouse Movement sample

a guest
Oct 29th, 2017
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.60 KB | None | 0 0
  1.     class MouseRandom
  2.     {
  3.  
  4.         static Random random = new Random();
  5.         static int mouseSpeed = 10;
  6.        
  7.         public void MoveMouse(int x, int y, int rx, int ry)
  8.         {
  9.             System.Drawing.Point c = new System.Drawing.Point();
  10.             GetCursorPos(out c);
  11.  
  12.             x += random.Next(rx);
  13.             y += random.Next(ry);
  14.  
  15.             double randomSpeed = Math.Max((random.Next(mouseSpeed) / 2.0 + mouseSpeed) / 10.0, 0.1);
  16.  
  17.             WindMouse(c.X, c.Y, x, y, 9.0, 3.0, 10.0 / randomSpeed,
  18.                 15.0 / randomSpeed, 10.0 * randomSpeed, 10.0 * randomSpeed);
  19.         }
  20.  
  21.         void WindMouse(double xs, double ys, double xe, double ye,
  22.             double gravity, double wind, double minWait, double maxWait,
  23.             double maxStep, double targetArea)
  24.         {
  25.  
  26.             double dist, windX = 0, windY = 0, veloX = 0, veloY = 0, randomDist, veloMag, step;
  27.             int oldX, oldY, newX = (int)Math.Round(xs), newY = (int)Math.Round(ys);
  28.  
  29.             double waitDiff = maxWait - minWait;
  30.             double sqrt2 = Math.Sqrt(2.0);
  31.             double sqrt3 = Math.Sqrt(3.0);
  32.             double sqrt5 = Math.Sqrt(5.0);
  33.  
  34.             dist = Hypot(xe - xs, ye - ys);
  35.  
  36.             while (dist > 1.0)
  37.             {
  38.  
  39.                 wind = Math.Min(wind, dist);
  40.  
  41.                 if (dist >= targetArea)
  42.                 {
  43.                     int w = random.Next((int)Math.Round(wind) * 2 + 1);
  44.                     windX = windX / sqrt3 + (w - wind) / sqrt5;
  45.                     windY = windY / sqrt3 + (w - wind) / sqrt5;
  46.                 }
  47.                 else
  48.                 {
  49.                     windX = windX / sqrt2;
  50.                     windY = windY / sqrt2;
  51.                     if (maxStep < 3)
  52.                         maxStep = random.Next(3) + 3.0;
  53.                     else
  54.                         maxStep = maxStep / sqrt5;
  55.                 }
  56.  
  57.                 veloX += windX;
  58.                 veloY += windY;
  59.                 veloX = veloX + gravity * (xe - xs) / dist;
  60.                 veloY = veloY + gravity * (ye - ys) / dist;
  61.  
  62.                 if (Hypot(veloX, veloY) > maxStep)
  63.                 {
  64.                     randomDist = maxStep / 2.0 + random.Next((int)Math.Round(maxStep) / 2);
  65.                     veloMag = Hypot(veloX, veloY);
  66.                     veloX = (veloX / veloMag) * randomDist;
  67.                     veloY = (veloY / veloMag) * randomDist;
  68.                 }
  69.  
  70.                 oldX = (int)Math.Round(xs);
  71.                 oldY = (int)Math.Round(ys);
  72.                 xs += veloX;
  73.                 ys += veloY;
  74.                 dist = Hypot(xe - xs, ye - ys);
  75.                 newX = (int)Math.Round(xs);
  76.                 newY = (int)Math.Round(ys);
  77.  
  78.                 if (oldX != newX || oldY != newY)
  79.                     SetCursorPos(newX, newY);
  80.  
  81.                 step = Hypot(xs - oldX, ys - oldY);
  82.                 int wait = (int)Math.Round(waitDiff * (step / maxStep) + minWait);
  83.                 System.Threading.Thread.Sleep(wait);
  84.             }
  85.  
  86.             int endX = (int)Math.Round(xe);
  87.             int endY = (int)Math.Round(ye);
  88.             if (endX != newX || endY != newY)
  89.                 SetCursorPos(endX, endY);
  90.         }
  91.  
  92.         double Hypot(double dx, double dy)
  93.         {
  94.             return Math.Sqrt(dx * dx + dy * dy);
  95.         }
  96.  
  97.         [System.Runtime.InteropServices.DllImport("user32.dll")]
  98.         static extern bool SetCursorPos(int X, int Y);
  99.  
  100.         [System.Runtime.InteropServices.DllImport("user32.dll")]
  101.         public static extern bool GetCursorPos(out System.Drawing.Point p);
  102.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement