from pynput.mouse import Listener, Button, Controller
import threading
import time
import keyboard
# 创建鼠标控制器对象
mouse = Controller()
# 定义移动函数
def move_mouse():
while move_thread_running.is_set(): # 判断移动线程是否运行
mouse.move(0, 5) # 向下移动5像素
time.sleep(1) # 暂停1秒
# 定义鼠标按下事件处理函数
def on_press(x, y, button, pressed):
global move_thread_running
if button == Button.left:
if pressed: # 按下鼠标左键时启动移动函数
move_thread_running = threading.Event()
move_thread_running.set()
move_thread.start()
else: # 松开鼠标左键时停止移动函数
move_thread_running.clear()
if keyboard.is_pressed('f1'): # 按下F1键时结束程序
move_thread_running.clear()
return False
# 创建移动线程对象
move_thread = threading.Thread(target=move_mouse)
move_thread_running = threading.Event()
# 启动鼠标监听器
with Listener(on_press=on_press) as listener:
listener.join()
pynput学习
于 2024-03-28 16:01:29 首次发布