Need to make this run faster, i.e/ make circles with mouse very fast, and loop it until some predefined keyboard input.
Can someone please help me out initial code below.
import pyautogui as pg
import math
resolution = pg.size()
radius = 100 # Circle radius
duration = 0.00000000000000000000000000001 # Duration of each move(mouse speed)
steps = 20 # Number of 'moves' or points on the circle
mid_x = resolution[0] / 2
mid_y = resolution[1] / 2
def GetXnY(angle):
x = radius * math.sin(math.pi * 2 * angle / 360)
y = radius * math.cos(math.pi * 2 * angle / 360)
return (x, y)
def DoACircle():
angle = 0
while angle <= 360:
x, y = GetXnY(angle)
pg.moveTo((mid_x + x), (mid_y - y), duration = duration)
angle = angle + (360/steps)
DoACircle()
It moves the mouse to slowly and I need to loop with keyboard interrup, not sure how to do it.
To increase speed you will need to increase the steps, reduce duration and there is a better way to optimise code.
OPTIMISATION:
mid_x = resolution[0] / 2
mid_y = resolution[1] / 2
def GetXnY(angle):
x = radius * math.sin(math.pi * 2 * angle / 360)
y = radius * math.cos(math.pi * 2 * angle / 360)
return (x, y)
def DoACircle():
angle = 0
while angle <= 360:
x, y = GetXnY(angle)
pg.moveTo((mid_x + x), (mid_y - y), duration=duration)
angle += 360 / steps
# Main loop that runs until 'q' is pressed
while not keyboard.is_pressed('q'):
DoACircle()
This part is good, id adjust this alone if you want to change pace.
# Parameters
resolution = pg.size()
radius = 100 # Circle radius
duration = 0 # Duration of each move (mouse speed)
steps = 100 # Number of 'moves' or points on the circle
However, you will need to import:
import keyboard
EDIT: Forgot to add that you need to install the keyboard module:
pip install keyboard