【问题标题】:Pyautogui Break LoopPyautogui 中断循环
【发布时间】:2022-01-25 22:08:41
【问题描述】:

所以我经常忽略 w 被按下的问题?无论如何我可以让它 100% 我假设它的循环而不听输入?

我尝试在整个代码中添加监听器,但没有任何乐趣

t=0

当真时:

if(keyboard.is_pressed("q")):
    t=0
    while t==0 :
        
        if(keyboard.is_pressed("w")):
            t=0
            break
    
        pyautogui.moveTo(798,717)
        pyautogui.moveTo(778,775)
        
        if(keyboard.is_pressed("w")):
            t=0
             break
            
        pyautogui.click()
        pyautogui.moveTo(599,883)
        
        if(keyboard.is_pressed("w")):
            t=0
             break
            
        pyautogui.click(interval=0.76)
        
        if(keyboard.is_pressed("w")):
            print("OK")
            t=1
            pyautogui.moveTo(900,715)
            pyautogui.moveTo(891,776)
            

if(keyboard.is_pressed("w")):
    print("OK")
    t=1
    pyautogui.moveTo(900,715)
    pyautogui.moveTo(891,776)
            
        
        

    
    

【问题讨论】:

    标签: python pyautogui


    【解决方案1】:

    问题是鼠标移动会阻止代码执行,is_pressed 未处理。

    看起来你正在使用keyboard模块,所以你应该使用add_hotkey方法而不是is_pressed

    import time
    
    import keyboard
    import pyautogui
    
    def press_ctrl_c():
        pyautogui.hotkey('ctrl', 'c')
    
    keyboard.add_hotkey('q', press_ctrl_c)
    
    while True:
        try:
            # Your code with mouse moves
            print('moving mouse')
            time.sleep(5)
        except KetboardInterrupt:
            break
    

    【讨论】:

    • 不幸的是没用,我认为你在正确的轨道上,但它仍然阻止了 w 新闻
    • 无论如何它都会阻塞,这就是它的实现方式。您可以尝试将所有内容包装到单独的线程/进程中,以使其成为非阻塞和响应式的,但它更难正确实施,并且需要更多关于您的问题的上下文
    • 另外你的循环应该是while not should_stop:而不是while t==0 :
    • 我要做的就是在按键上继续以循环方式移动鼠标,在另一个按键上它停止
    • @user3518144,我编辑了我的答案,看看。如果它解决了您的问题,请考虑将答案标记为“已接受”
    猜你喜欢
    • 2019-05-05
    • 1970-01-01
    • 2020-12-09
    • 1970-01-01
    • 2020-10-22
    • 2022-01-08
    • 1970-01-01
    • 1970-01-01
    • 2011-08-30
    相关资源
    最近更新 更多