Breaking infinite loop with key stroke

Joined
Jul 26, 2022
Messages
2
Reaction score
0
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.
 

Attachments

  • Try 1.PNG
    Try 1.PNG
    12.8 KB · Views: 37
  • Try2.PNG
    Try2.PNG
    12.5 KB · Views: 34
  • Try3.PNG
    Try3.PNG
    11.6 KB · Views: 35
  • Try4.PNG
    Try4.PNG
    15.4 KB · Views: 31
  • Try5.PNG
    Try5.PNG
    12.8 KB · Views: 33
Joined
Nov 4, 2022
Messages
4
Reaction score
2
Use 'CONTROL' + 'C' to cause a keyboard interrupt with the code below.
Python:
try:
    while True:
        print("Something")
except KeyboardInterrupt:
    pass
 

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

Members online

No members online now.

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,710
Latest member
bernietqt

Latest Threads

Top