Results 1 to 2 of 2

Thread: Help with Human Mouse Move in python 3 using pyautogui

  1. #1
    Join Date
    Dec 2007
    0 Post(s)
    43 Post(s)

    Question Help with Human Mouse Move in python 3 using pyautogui

    here is what I have so far but it's not human enough, How can I get the speed to vary along the path? also any other suggestions are apprettiated!

    import pyautogui
    import random
    def move_mouse_randomly(box_x, box_y, box_width, box_height, duration=1.0, pause=0.0):
        # Calculate the center of the box
        center_x = box_x + box_width / 2
        center_y = box_y + box_height / 2
        # Generate random x and y coordinates within the box
        x = random.uniform(box_x, box_x + box_width)
        y = random.uniform(box_y, box_y + box_height)
        # Calculate the distance from the center
        distance_from_center = ((x - center_x) ** 2 + (y - center_y) ** 2) ** 0.5
        # Use a Gaussian function to weight the coordinates towards the center
        weight = random.gauss(0, distance_from_center / 3)
        # Get the current mouse position
        current_x, current_y = pyautogui.position()
        # Calculate the distance to move the mouse
        distance_x = x + weight - current_x
        distance_y = y + weight - current_y
        # Calculate the number of steps to take
        steps = int(duration * 10)  # 10 steps per second
        # Calculate the delay between steps
        delay = duration / steps
        # Move the mouse in a straight line
        for i in range(steps):
            pyautogui.moveRel(distance_x / steps, distance_y / steps)
        # Pause before returning the cursor to its original position
    This function takes four required arguments: the x and y coordinates of the top-left corner of the box, and the width and height of the box. It also has two optional arguments:

    duration: the time, in seconds, to take to move the mouse. The default value is 1.0 seconds.
    pause: the time, in seconds, to pause after moving the mouse. The default value is 0.0 seconds.
    To use this function, you can call it with the coordinates and dimensions of the box:
    move_mouse_randomly(100, 100, 200, 200)
    This will move the mouse slowly and smoothly, in an imperfect straight line, to a random spot within the box with the top-left corner at (100, 100) and dimensions of 200x200 pixels, with a bias towards the center of the box.

    You can also specify the duration and pause arguments to customize the movement:

    move_mouse_randomly(100, 100, 200, 200, duration=0.5, pause=1.0)

  2. #2
    Join Date
    Dec 2007
    0 Post(s)
    43 Post(s)

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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