Python的自动化键鼠

该博客介绍了如何使用Python的pyautogui和threading库实现自动化键鼠操作,包括单点、双击、删除等交互,并通过多线程增加程序的扩展性和可中断性。用户可以通过按下Esc键退出程序,Delete键执行删除操作。主要步骤包括目标位置的选择和一系列预定义的鼠标操作序列。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

基于 Python,实现自动化键鼠,用于简化机械的单点,双击,删除等交互操作,通过开线程方式增加扩展性并可随时中止程序。

使用以下的技术

  • pyautogui 自动化图形交互界面编程
  • threading 多线程编程

实践

import pyautogui
import threading
import keyboard
import time


flag = True


def watch_key(key):
    name = key.name
    print(name)
    global flag
    if name == 'esc':
        flag = False
        print('此时flag为False,将退出子线程main')
    elif name == 'delete':
        print('此时执行delete,删除一个目标')
    else:
        print('想退出子线程main,请按esc')


class MyThread(threading.Thread):
    def __init__(self, thread_id, name):
        threading.Thread.__init__(self)
        self.thread_id = thread_id
        self.name = name

    def run(self):
        print("开始线程: " + self.name)
        main()
        print("退出线程: " + self.name)


def CurrentMousePlace():
    pyautogui.alert(text='请先选中对话框非确认按钮区域\n再将鼠标移至目标上方\n最后回车确认', title='命令')
    CurrentMouseW, CurrentMouseH = pyautogui.position()
    return CurrentMouseW, CurrentMouseH


def MouseOperationCl(wight, height, time=1.0):
    if pyautogui.onScreen(wight, height):
        pyautogui.moveTo([wight, height], duration=time)
        pyautogui.click(button='left')
    else:
        print('当前屏幕上没有鼠标')


def MouseOperationDcl(wight, height, time=1.0):
    if pyautogui.onScreen(wight, height):
        pyautogui.moveTo([wight, height], duration=time)
        pyautogui.doubleClick(button='left')
    else:
        print('当前屏幕上没有鼠标')


def MouseOperationClPd(wight, height, time=1.0):
    if pyautogui.onScreen(wight, height):
        pyautogui.moveTo([wight, height], duration=time)
        pyautogui.click(button='left')
        pyautogui.press('Delete')
    else:
        print('当前屏幕上没有鼠标')


def step_one():
    w1, h1 = CurrentMousePlace()
    print(w1, h1)
    w2, h2 = CurrentMousePlace()
    print(w2, h2)
    w3, h3 = CurrentMousePlace()
    print(w3, h3)
    w4, h4 = CurrentMousePlace()
    print(w4, h4)
    return w1, h1, w2, h2, w3, h3, w4, h4


def step_two(w1, h1, w2, h2, w3, h3, w4, h4):
    MouseOperationDcl(w1, h1, time=0.5)
    time.sleep(1.5)

    MouseOperationDcl(w2, h2, time=0.5)
    time.sleep(1.5)

    MouseOperationCl(w3, h3, time=0.5)
    time.sleep(1.5)

    MouseOperationCl(w4, h4, time=0.5)
    time.sleep(1.5)


def step_three(w1, h1, w2, h2, w3, h3, w4, h4):
    MouseOperationDcl(w1, h1, time=0.5)
    time.sleep(3.5)

    MouseOperationClPd(w2, h2, time=0.5)
    MouseOperationDcl(w2, h2, time=0.5)
    MouseOperationCl(w3, h3, time=0.5)
    time.sleep(3.5)

    MouseOperationCl(w4, h4, time=0.5)


def main():
    print(pyautogui.size())
    keyboard.on_press(watch_key)  # 自动创建一个非阻塞的子线程,若使用hook则按下与释放都会触发
    if flag:
        w1, h1, w2, h2, w3, h3, w4, h4 = step_one()
    if flag:
        pyautogui.alert(text='准备开始', title='提示')
    if flag:
        step_two(w1, h1, w2, h2, w3, h3, w4, h4)
    if flag:
        for i in range(1, 1500):
            if flag:
                step_three(w1, h1, w2, h2, w3, h3, w4, h4)


if __name__ == '__main__':
    thread_main = MyThread(1, 'main')
    thread_main.start()  # 将子线程命名为main,会自动调用run执行其中的main()函数
    thread_main.join()  # 阻塞主线程,等待子线程main运行完成
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

昊大侠

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值