private void button1_Click(object sender, EventArgs e) { button1.Size = new Size(RandomGenerator.NextInt(20, 125), RandomGenerator.NextInt(20, 125)); button1.Location = new Point( NormalDistributor.WeightedRandom(0, this.Size.Width - button1.Size.Width - 100), NormalDistributor.WeightedRandom(0, this.Size.Height - button1.Size.Height - 100)); }
/// <summary> /// Move mouse cursor to a random position within a rectangle /// </summary> /// <param name="rect">Rectangle</param> /// <param name="speed">Cursor speed</param> public void MoveTo(Rectangle rect, double speed = SpeedDefault) { // keep looping until the cursor is within range while (!rect.Contains(Location)) { // WeightedRandom is not always return values within the given range. MoveTo(NormalDistributor.WeightedRandom(rect.X, rect.Right), NormalDistributor.WeightedRandom(rect.Y, rect.Bottom)); } }
/// <summary> /// Types a message /// </summary> /// <param name="message">Message to type</param> /// <param name="speed">Typing speed in keys per second.</param> public static void Type(string message, double speed = SPEED_DEFAULT) { const double HOLD_STD_DEV = 29; const double PAUSE_STD_DEV = 11; const double HOLD_RATIO = 103.0 / (24.0 + 103.0); // 103 ms hold, 24 ms pause is standard const double PAUSE_RATIO = 1.0 - HOLD_RATIO; double time_per_key_ms = (1.0 / speed) * 1000.0; double hold_mean = HOLD_RATIO * time_per_key_ms; double pause_mean = PAUSE_RATIO * time_per_key_ms; bool shift_down = false; NormalDistributor hold_key_random_time = new NormalDistributor(hold_mean, HOLD_STD_DEV, 0); NormalDistributor pause_random_time = new NormalDistributor(pause_mean, PAUSE_STD_DEV, 0); try { foreach (char c in message) { KeyData key_data = KeyData.FromCharacter(c); if (key_data.ShiftPressed) { if (!shift_down) { KeyDown(Keys.RShiftKey); shift_down = true; } } else { if (shift_down) { KeyUp(Keys.RShiftKey); shift_down = false; } } TapKey(key_data.KeyCode, hold_key_random_time.GetInt()); Thread.Sleep(pause_random_time.GetInt()); } } finally { if (shift_down) { KeyUp(Keys.RShiftKey); } } }
public override void MoveTo(int x, int y, double speed = SpeedDefault) { const int MEAN = 100; const int DEV = 50; int loops_between_poll = Math.Max(1, (int)(speed * (PollingPeriod / 1000.0))); int maxrandy_x = NormalDistributor.NormalDistribution(MEAN, DEV); int maxrandy_y = NormalDistributor.NormalDistribution(MEAN, DEV); Point destination = User32.AbsoluteToRelativePoint(x, y); while (destination != Cursor.Position) { int xdif = destination.X - Cursor.Position.X; int ydif = destination.Y - Cursor.Position.Y; // +1 prevents div/0 int xdif_magnitude = Math.Abs(xdif) + 1; int ydif_magnitude = Math.Abs(ydif) + 1; int x_addition = Math.Sign(xdif); int y_addition = Math.Sign(ydif); int relative_x = 0; int relative_y = 0; for (int i = 0; i < loops_between_poll && (xdif != relative_x || ydif != relative_y); i++) { if (RandomGenerator.NextInt(0, maxrandy_x) <= (xdif_magnitude * MEAN) / ydif_magnitude) { relative_x += x_addition; } if (RandomGenerator.NextInt(0, maxrandy_y) <= (ydif_magnitude * MEAN) / xdif_magnitude) { relative_y += y_addition; } xdif_magnitude = Math.Abs(xdif + relative_x) + 1; ydif_magnitude = Math.Abs(ydif + relative_y) + 1; } WaitForNextPoll(); JumpPosition(relative_x, relative_y); } }