import time from pynput import keyboard, mouse from pynput.mouse import Button import threading control = keyboard.Controller() IS_LEFT_DOWN = True IS_RIGHT_DOWN = True IS_START = False IS_FLAG_T = time.time() def on_press(key): global IS_LEFT_DOWN, IS_RIGHT_DOWN, IS_START try: if key.char == "=": IS_START = not IS_START except AttributeError: pass def on_click(x, y, button: Button, pressed): global IS_LEFT_DOWN, IS_RIGHT_DOWN, IS_START, IS_FLAG_T if "left" in str(button): if pressed: print("鼠标左键按下") IS_LEFT_DOWN = True else: print("鼠标左键释放") IS_FLAG_T = time.time() IS_LEFT_DOWN = False if "right" in str(button): if pressed: print("鼠标右键按下") IS_RIGHT_DOWN = True else: print("鼠标右键释放") IS_RIGHT_DOWN = False def listener_mouse(): with mouse.Listener(on_click=on_click) as listener: listener.join() def listener_keyword(): with keyboard.Listener(on_press=on_press) as listener: listener.join() def down_w(): return while True: time.sleep(0.1) print(IS_LEFT_DOWN, IS_RIGHT_DOWN) if IS_LEFT_DOWN or IS_RIGHT_DOWN: ... else: if IS_START: # 鼠标左键释放后的0.5s 不做任何处理 if time.time() - IS_FLAG_T < 0.5: continue control.press("w") control.release("w") control.press("w") control.release("w") def eat(): f = 9.5 tmp = True last_t = time.time() while True: time.sleep(0.01) if IS_START: t = time.time() if t - last_t > f: last_t = t if tmp: control.press("1") control.release("1") else: control.press("2") control.release("2") tmp = not tmp threading.Thread(target=listener_mouse).start() threading.Thread(target=listener_keyword).start() threading.Thread(target=eat).start() threading.Thread(target=down_w).start()