最近在学习pyautogui库,写个重复鼠标操作的小工具实践一下,使用说明见代码。实现了重复点击某一个坐标位置和自动滚动滑鼠的操作
"""
鼠标重复操作
python3.8 依赖的模块
pip install pynput==1.6.8 为了使用pyinstaller 打包成二进制,高版本的不兼容
pip install pyautogui
快捷键说明
alt+s 持续点击鼠标所在位置
alt+u 持续向上滚动滑鼠
alt+d 持续向下滚动滑鼠
alt+c 取消动作,每次执行重复动作时,先执行alt+c;
alt+q 退出程序
"""
import sys
import pyautogui
from pynput.keyboard import GlobalHotKeys
from threading import Thread
pyautogui.FAILSAFE = False
cancel = [False]
interval = [1.0]
def click_position(x, y):
while cancel[0] is False:
pyautogui.click(x, y)
print(f"点击坐标:{x},{y}")
pyautogui.sleep(interval[0])
def scroll_window(offset):
while cancel[0] is False:
pyautogui.scroll(offset)
print(f"滚动的偏移速度:{offset}")
pyautogui.sleep(interval[0])
def on_click():
x, y = pyautogui.position()
print(f"点击坐标位置:({x},{y})")
job = Thread(target=click_position, args=(x, y))
job.daemon = True
cancel[0] = False
job.start()
def on_exit():
print("退出")
cancel[0] = True
sys.exit(0)
def on_cancel():
print("取消")
cancel[0] = True
def on_scroll_down():
"""
向下滚动
"""
job = Thread(target=scroll_window, args=(-1024,))
job.daemon = True
cancel[0] = False
job.start()
def on_scroll_up():
"""
向上滚动
"""
job = Thread(target=scroll_window, args=(1024,))
job.daemon = True
cancel[0] = False
job.start()
if __name__ == "__main__":
help_text = """
重复操作鼠标
快捷键说明:
alt+s 持续点击鼠标所在位置;
alt+u 持续向上滚动滑鼠;
alt+d 持续向下滚动滑鼠;
alt+c 取消动作 每次执行重复动作时,先执行alt+c;
alt+q 退出程序;
操作间隔为1秒;
"""
print(help_text)
# 绑定全局快捷键事件
with GlobalHotKeys(
{'<alt>+s': on_click,
'<alt>+q': on_exit,
'<alt>+c': on_cancel,
'<alt>+d': on_scroll_down,
'<alt>+u': on_scroll_up}
) as listener:
listener.join()