0

I made an autoclicker and i can stop it by pressing b but only at the right timing. I didn't find anything that would allow me to stop the program by pressing a button at any time without accessing the console

Here's the program:

from time import sleep
import keyboard
import mouse

state=True
while state:
    if keyboard.is_pressed("b"):
        state=False
    else:
        mouse.click()
        sleep(1)
pyleky11
  • 5
  • 2
  • Does this answer your question? [Using a key listener to stop a loop](https://stackoverflow.com/questions/72422755/using-a-key-listener-to-stop-a-loop) – Michael Ruth Nov 21 '22 at 19:20

1 Answers1

0

I already answered at Using a key listener to stop a loop

You can simply use the add_hotkey method. Example:

import keyboard

state = True

def stop():
    state  = False # The function you want to execute to stop the loop

keyboard.add_hotkey("b", stop) # add the hotkey
Lanfix
  • 66
  • 6