Breaking infinite loop with key stroke

Jul 26, 2022
Reaction score
Hello, I am trying to make an infinite loop that left clicks until I press a certain key. I have tried what looked logical to me but none of the methods worked. Can someone point me in the right direction?

I used the following modules:
pyautogui (for the actions I want my loop to perform)
sys ( I tried binding sys.exit() and sys.quit() to a key)
keyboard ( for controlling the loop with keyboard hotkeys)

I tried binding sys.exit() to a hotkey within a while True loop (Try1+2), trying the same with lambda: (Try3).
Then I switched my approach and tried making an infinite while loop that runs when the count is above 0 and making a hotkey that makes the count equal to 0 thus, breaking the loop in my understanding (Try4). In my last attempt I tried to bind break to a hotkey and if the hotkey is not pressed, make my script do a leftclick (Try5). I ran out of ideas at this point.

I would like someone to help me understand why I'm failing.


  • Try 1.PNG
    Try 1.PNG
    12.8 KB · Views: 3
  • Try2.PNG
    12.5 KB · Views: 2
  • Try3.PNG
    11.6 KB · Views: 1
  • Try4.PNG
    15.4 KB · Views: 2
  • Try5.PNG
    12.8 KB · Views: 3


Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question