简体   繁体   中英

My code isn't working (python, pyautogui)

The code works once even though I have it set in a loop, what do I need to change to make it work.

import pyautogui, time
time.sleep(5)
while True: 
    pyautogui.press("e")
    pyautogui.click()
    if w or a or s or d:
        stop()

Assuming from the other replies you gave, you want to press e and click until it is pressed either w , a , s or d and if it is pressed one of those four, you want the program to end. If you want to do that you can use the keyboard library in python. The function keyboard.is_pressed(key) should check if the pressed key is any of the four above and if it is, it will terminate the program. You can install the library by doing pip install keyboard and for example, you can rewrite your program like that:

import pyautogui, time, keyboard
time.sleep(5)
while True:
    keyboard.press_and_release('e')   #this is the same as pyautogui.press("e")
    pyautogui.click()
    if (keyboard.is_pressed('w') or keyboard.is_pressed('a')) or (keyboard.is_pressed('s') or keyboard.is_pressed('d')):
        break   #this is your stop() function

EDIT: I've edited the comment with the correct explanation since I've edited only the code earlier.

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