Home > Net >  How to start and stop the same loop by the keyboard
How to start and stop the same loop by the keyboard

Time:02-22

i want to make an macro clicker for games, but i dont know how to start and stop clicking loop by the same key on keyboard. Here is where I am:

def start_pvp_clicking():
bind_pvp_key = entry1.get()
pyautogui.PAUSE = 0.08
while True:
    counter = 0
    if keyboard.is_pressed(bind_pvp_key):
         while counter == 0:
            print("clicking")
            if keyboard.is_pressed(bind_pvp_key):
                while True:
                    print("not clicking")
                    break

when i press a key, loop is starting but i cant stop it and run again by the same key pls help

CodePudding user response:

The counter won't be updated after each time in the loop. It will remain 0 forever so the program will run forever. Just update the counter to counter 1 to prevent infinity loop and change (while counter == 0) because that loop will stop after the counter is bigger than 0, and you have 3 while loops and the last one sucks, there is no need to add the last loop, in the first loop call (while true) it will run forever because true will remain true forever. I recommended adding a parameter called gameTicks = 0.02, then just temporarily stopping functioning each gameTicks (0.02). If you did what I said, the program should work well.

CodePudding user response:

Try something like this:

while True:
    key_togle = 0
    if keyboard.is_pressed(bind_pvp_key):
        key_togle = 1
        while key_togle:
            print('Doing stuff')
            if keyboard.is_pressed(bind_pvp_key):
                key_togle = 0

    print('Not doing stuff')
  • Related