import pyautogui
import keyboard
import threading
# 标志变量,用于指示是否终止操作
terminate = False
def perform_operation():
while not terminate:
pyautogui.moveTo(1765, 545, duration=1)
pyautogui.click(button="left")
pyautogui.scroll(-600)
print("操作执行中...")
def listen_for_esc():
global terminate
keyboard.wait('esc') # 监听 ESC 键的按下事件
terminate = True # 设置标志变量为 True,终止操作
# 创建并启动线程
operation_thread = threading.Thread(target=perform_operation)
esc_thread = threading.Thread(target=listen_for_esc)
operation_thread.start()
esc_thread.start()
# 主线程等待操作线程结束
operation_thread.join()
print("操作已终止")
局限:在变化窗口大小后就无法使用