Code for How to Control your Mouse in Python Tutorial


View on Github

control_mouse.py

import mouse

# left click
mouse.click('left')

# right click
mouse.click('right')

# middle click
mouse.click('middle')

# get the position of mouse
print(mouse.get_position())
# In [12]: mouse.get_position()
# Out[12]: (714, 488)

# presses but doesn't release
mouse.hold('left')
# mouse.press('left')

# drag from (0, 0) to (100, 100) relatively with a duration of 0.1s
mouse.drag(0, 0, 100, 100, absolute=False, duration=0.1)

# whether a button is clicked
print(mouse.is_pressed('right'))

# move 100 right & 100 down
mouse.move(100, 100, absolute=False, duration=0.2)

# make a listener when left button is clicked
mouse.on_click(lambda: print("Left Button clicked."))
# make a listener when right button is clicked
mouse.on_right_click(lambda: print("Right Button clicked."))

# remove the listeners when you want
mouse.unhook_all()

# scroll down
mouse.wheel(-1)

# scroll up
mouse.wheel(1)

# record until you click right
events = mouse.record()

# replay these events
mouse.play(events[:-1])

draw.py

import mouse
import math
import time

def draw_square(size):
    # click and hold the left mouse button
    mouse.press()
    mouse.move(size, 0, absolute=False, duration=0.2)
    mouse.move(0, size, absolute=False, duration=0.2)
    mouse.move(-size, 0, absolute=False, duration=0.2)
    mouse.move(0, -size, absolute=False, duration=0.2)
    # release the left mouse button
    mouse.release()


def draw_circle(radius):
    # click and hold the left mouse button
    mouse.press()
    # move the mouse in a circle
    for i in range(0, 360, 5):
        # convert degrees to radians
        angle = math.radians(i)
        # calculate the x and y coordinates
        x = radius * math.cos(angle)
        y = radius * math.sin(angle)
        # move the mouse to the calculated position
        mouse.move(x, y, absolute=False, duration=0.01)
    # release the left mouse button
    mouse.release()

if __name__ == "__main__":
    # Place the mouse at the starting point and then call
    draw_square(200)
    time.sleep(1)
    draw_circle(10)