This is the code I'm using and I want that the cursor comes back to the 759, 718 after 12 seconds and the continue with the clicks.
import time
import threading
import random
from pynput.mouse import Controller, Button
from pynput.keyboard import Listener, KeyCode
TOGGLE_KEY = KeyCode(char="1")
clicking = False
mouse = Controller()
#a=random.randint(176, 718)
#b=random.randint(732, 1215)
def clicker():
while True:
if clicking:
mouse.click(Button.right, 1)
for a in range(0, 10):
a=random.randint(732, 1215)
for b in range(0, 10):
b=random.randint(176, 500)
mouse.position=(a, b)
time.sleep(0.001)
def toggle_event(key):
if key == TOGGLE_KEY:
mouse.position = (759, 718)
global clicking
clicking = not clicking
click_threat = threading.Thread(target=clicker)
click_threat.start()
with Listener(on_press=toggle_event) as listener:
listener.join()
I tried different ways to add time with "if" but I didn't succeed I also tried with another time.sleep but the whole code thinks it should wait 12 seconds to execute and doesn't do the clicks every 0.001 seconds.
If I understood your plan correctly, you need to set a start time variable before the loop starts, and inside the loop, for every iteration, you should check if the current time - the start time is greater or equal to 12 seconds (and reset start time if needed.
for example:
def do_something():
start_time = time.time()
while True:
if time.time() - start_time >= 12:
do_something_else()
start_time = time.time()
Besides that you should check your algorithm logic, there is not much sense in what you're doing there.
you're looping variable a trough range from 0 to 10 (B.T.W range(0, 10) is the same as range(10)), and every time variable a got the next value in the range you are replacing the value with a new random value. and you do nothing with the new value until the loop ends. So when you're looping variable b, the value for a will be the last value it held before the b loop started.
if you want to generate 10 randoms numbers try something like:
for _ in range(10):
a = random.randint(732, 1215)
b = random.randint(176, 500)
mouse.position=(a, b)
time.sleep(0.001)