简体   繁体   English

IDLE Python - 如何在按下键时执行循环?

[英]IDLE Python - How to make it so when a key is pressed, the loop runs?

I have made a macro and I want it so when I run the py file, nothing happens, but when I click a certain key, the if statement runs the while loop, and when I click that key again, the script pauses.我制作了一个宏,我想要它,所以当我运行 py 文件时,什么也没有发生,但是当我单击某个键时,if 语句会运行 while 循环,当我再次单击该键时,脚本会暂停。

import time
import keyboard

time.sleep(3)

while True:
    try:
        #slot-1
        pyautogui.press('2')
        pyautogui.click(button= 'left', clicks=1)
        time.sleep(0.1)
        #slot-2
        pyautogui.press('3')
        pyautogui.click(button= 'left', clicks=1)
        time.sleep(0.1)
        #slot-3
        pyautogui.press('4')
        pyautogui.click(button= 'left', clicks=1)
        time.sleep(0.1)
        #slot-4
        pyautogui.press('5')
        pyautogui.click(button= 'left', clicks=1)
        time.sleep(0.1)
        if keyboard.is_pressed('alt'):
            print('Ending Loop')
            break
    except:
        break

For starting the loop you can use input() (Press return / enter to directly start)要启动循环,您可以使用 input() (按 return / enter 直接启动)

For finishing it press ctrl + c and catch KeyboardInterrupt exception要完成它,请按 ctrl + c 并捕获 KeyboardInterrupt 异常

import time
import keyboard

time.sleep(3)

# press enter to start
input()

# press ctrl + c to finish
while True:
    try:
        #slot-1
        pyautogui.press('2')
        pyautogui.click(button= 'left', clicks=1)
        time.sleep(0.1)
        #slot-2
        pyautogui.press('3')
        pyautogui.click(button= 'left', clicks=1)
        time.sleep(0.1)
        #slot-3
        pyautogui.press('4')
        pyautogui.click(button= 'left', clicks=1)
        time.sleep(0.1)
        #slot-4
        pyautogui.press('5')
        pyautogui.click(button= 'left', clicks=1)
        time.sleep(0.1)
        if keyboard.is_pressed('alt'):
            print('Ending Loop')
            break
    except Exception as e:
        if e == KeyboardInterrupt:
            print('script stopped by user')

I've used a mix of threading and used global variables in order to solve your problem.为了解决您的问题,我混合使用了线程和使用全局变量。 I also had to resort to the pynput module instead of "keyboard".我还不得不求助于 pynput 模块而不是“键盘”。 Although this is a slight workaround, the best solution in your scenario would be the use of "async" (asynchronous programming) in order to prevent the issue of blocking while loops.虽然这是一个轻微的解决方法,但在您的方案中,最好的解决方案是使用“async”(异步编程),以防止出现阻塞 while 循环的问题。 These are loops which will not allow any other part of your program to run.这些是不允许程序的任何其他部分运行的循环。 This means our program will not be able to detect key presses once it is inside a while loop.这意味着我们的程序一旦进入 while 循环,就无法检测到按键。 I have not used async simply because I am not familiar with its syntax and did not want to make this answer more complicated than it should be.我没有使用 async 仅仅是因为我不熟悉它的语法并且不想让这个答案变得比它应该的更复杂。

import time
import keyboard
import pyautogui
from pynput.keyboard import Key, Listener
import threading
time.sleep(3)

closed = False

def begin_clicks():
    global closed
    while True:
        print(closed)
        if closed:
            break
        try:
            #slot-12
            pyautogui.press('2')
            pyautogui.click(button= 'left', clicks=1)
            time.sleep(0.1)
            #slot-2
            pyautogui.press('3')
            pyautogui.click(button= 'left', clicks=1)
            time.sleep(0.1)
            #slot-33
            pyautogui.press('4')
            pyautogui.click(button= 'left', clicks=1)
            time.sleep(0.1)
            #slot-4
            pyautogui.press('5')
            pyautogui.click(button= 'left', clicks=1)
            time.sleep(0.1)
    
        except:
            pass

thread = None

def on_press(key):
    global closed
    if str(key)[1] == "p":
        closed = False
        threading.Thread(target=begin_clicks).start()
    elif key == Key.alt:
        closed = True
        
        
with Listener(
        on_press=on_press) as listener:
    listener.join()

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

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