简体   繁体   English

当按下键时循环不会停止 - python

[英]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.我的 python 代码每 4 秒在屏幕上点击一次,但在按下 q 时停止(一次),再次按下 q 时它继续。
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...我也用 break 尝试了这个,而不是不点击,但它只在我按住 q 一段时间并且我必须再次运行代码才能再次开始点击时才有效......

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...编辑:@stacker 的答案有效,但它不会每 4 秒点击一次......
That was my first code, but I want it to press it every 4 seconds and then their are issues...那是我的第一个代码,但我希望它每 4 秒按下一次,然后它们就会出现问题......

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.当您按下 q 时,您将退出while click:循环,只需添加一个新的 bool 即可解决此问题。 Output: 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.当 time.sleep function 正在执行时,您无法记录任何键盘事件,因为它完全阻止了代码。 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. time.time() function 返回 Unix 时间。 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.我们在 UNIX 时间加上 4 秒所需的延迟时间,并找出脚本在延迟 4 秒后何时继续执行。 Until the time reaches the 4-second limit, it keeps checking for keypresses.在时间达到 4 秒限制之前,它会一直检查按键。


but it stops when q is pressed (once) and it continues when q is pressed again.但当 q 被按下(一次)时它会停止,当 q 再次被按下时它会继续。

As a side note, please note that the variable which you modify on keypress is the one on which the while loop runs.作为旁注,请注意您在按键上修改的变量是运行 while 循环的变量。 So if it's set to False, your loop ends.因此,如果它设置为 False,您的循环就会结束。 If you want it to be continuous, you should create a separate variable for the loop, or just run an infinite loop.如果你希望它是连续的,你应该为循环创建一个单独的变量,或者只是运行一个无限循环。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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