简体   繁体   中英

While loop doesn't stop when key is pressed - python

my python code clicks on the screen every 4 seconds, but it stops when q is pressed (once) and it continues when q is pressed again.
I also tried this with break, instead of not click, but it only works when I hold q for a while and I have to run the code again to start clicking again...

Does anyone have an idea how I don't have to hold the key and how I can prevent to many inputs?

import time
import pyautogui
import keyboard

time.sleep(5)
click = True
print(click)
while click:
    if keyboard.is_pressed("q"):
        print(click)
        click = not click
    time.sleep(4)
    if click:
        pyautogui.click()

Edit: the answer of @stacker works, but it doesn't click every 4 seconds...
That was my first code, but I want it to press it every 4 seconds and then their are issues...

import time
import pyautogui
import keyboard

time.sleep(5)
should_loop = True
click = True;
while should_loop:
    if keyboard.is_pressed("q"):
        print("Stopped/Started clicking!")
        click = not click
        time.sleep(1) # sleep for 1 second so the button doesnt get tapped 
               # again too fast
    if click:
        time.sleep(4) # sleep 4 seconds before clicking
        print("Clicked!")
        pyautogui.click()

When you were pressing q you would exit the while click: loop, just add a new bool to fix this. Output:

qStopped/Started clicking!
Clicked!
Clicked!
  ...
Clicked!
Clicked!
qStopped/Started clicking!

The issue here is the sleep delay. You aren't able to record any keyboard event when the time.sleep function is being executed because it blocks the code completely. You want to be able to read the keyboard events even during the sleep delay. The solution is simple. Just make your own sleep code.

import time
import pyautogui
import keyboard

time.sleep(5)
click = True
print(click)
while click:
    expected_continue_time = time.time() + 4
    while time.time() < expected_continue_time:
        if keyboard.is_pressed("q"):
            print(click)
            click = not click
            break

    if click:
        pyautogui.click() 

time.time() function returns Unix time. We add 4 seconds, the required delay time in the UNIX time, and find out when the script would continue after a 4 seconds delay. Until the time reaches the 4-second limit, it keeps checking for keypresses.


but it stops when q is pressed (once) and it continues when q is pressed again.

As a side note, please note that the variable which you modify on keypress is the one on which the while loop runs. So if it's set to False, your loop ends. If you want it to be continuous, you should create a separate variable for the loop, or just run an infinite loop.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM