写一个自动化记录鼠标/键盘的动作,然后可以重复执行的python程序

发布于:2024-09-19 ⋅ 阅读:(8) ⋅ 点赞:(0)
import sys
import threading
import time
from PyQt5.QtWidgets import *
from auto_fun import *
import pyautogui
import pynput
from PyQt5.QtCore import pyqtSignal
from MouseModule import *
from pynput import keyboard

local_list = []  # 保存操作坐标、动作、文本
begain_time = 0
now_time = 0
text_list = []
input_str = ""  # 从键盘输入的数据
click_flag = False
keyborad_flag = False


class window(QMainWindow, Ui_MainWindow):
    lcd_signal = pyqtSignal(int)  # 循环计数信号
    display_signal = pyqtSignal(str)  # 操作记录显示信号

    def __init__(self):
        super(window, self).__init__()
        self.mouse_active = False
        self.mouse_listener = None
        self.keyboard_active = False
        self.keyboard_listener = None
        self.setupUi(self)
        self.init()

    #初始化
    def init(self):
        self.pushButton_3.clicked.connect(self.begin_note)
        self.pushButton_4.clicked.connect(self.begin_action)
        self.lcd_signal.connect(self.lcd_signal_fun)               #信号触发计时
        self.display_signal.connect(self.display_signal_fun)      #信号触发显示界面

    def keyboard_start(self):
        self.keyboard_active = True
        self.keyboard_listener = keyboard.Listener(on_press=self.on_press)
        self.keyboard_listener.start()

    def keyboard_stop(self):
        if self.keyboard_listener:
            self.keyboard_active = False
            self.keyboard_listener.stop()

    def on_press(self,key):
        global input_str
        global begain_time
        print("键盘事件:", key)
        test_key = str(key)  # 必须有这一步否则会出问题 崩溃
        str_data = test_key.replace("'", "")

        if str_data == "Key.enter":
            str_data = '\n'

        if "Key" not in str_data:
            input_str = input_str + str_data
            now_time = time.time()
            action = {}
            action["act"] = "note"
            action["input_text"] = input_str  # 键盘输入的数据   如果需要回车需要贾’\n‘
            action["sleep"] = int((now_time - begain_time)) + 1  # 保存延迟的时间
            begain_time = now_time
            local_list.append(action)
            print(local_list)
            input_str = ''
            self.display_signal.emit("输入文本:" + action["input_text"])

    def mouse_start(self):
        self.mouse_active = True
        self.mouse_listener = mouse.Listener(on_click=self.on_click)
        self.mouse_listener.start()

    def mouse_stop(self):
        if self.mouse_listener:
            self.mouse_active = False
            self.mouse_listener.stop()

    def on_click(self, x, y, button, pressed):
        global begain_time
        global local_list
        if self.mouse_active:
            if pressed:
                print('Button {0} pressed at ({1}, {2})'.format(button, x, y))
                action = {}
                action["local"] = [x, y]
                action["act"] = "click"
                now_time = time.time()
                action["sleep"] = int((now_time - begain_time)) + 1  # 保存延迟的时间
                begain_time = now_time
                local_list.append(action)  # local_list 操作记录表格
                self.display_signal.emit("点击坐标:" + str(x) + " " + str(y))

    def display_signal_fun(self, text):
        self.textEdit.insertPlainText(text + '\n')

    def lcd_signal_fun(self, num):
        self.lcdNumber.display(num + 1)
        print("动作执行:", num)

    def action_thread(self):
        global local_list
        num = self.spinBox.text()
        print("开始执行动作循环")
        for i in range(0, int(num)):
            for item in local_list:
                if item["act"] == "click":
                    pyautogui.click(int(item["local"][0]), int(item["local"][1]))
                    time.sleep(item["sleep"])
                    print("鼠标点击", item["local"][0], item["local"][1])

                elif item['act'] == "note":
                    pyautogui.typewrite(item["input_text"])
                    time.sleep(item["sleep"])
            self.lcd_signal.emit(i)

    # 创建一个线程开始执行记录的动作
    def begin_action(self):
        fd = threading.Thread(target=self.action_thread)
        fd.start()

    # 开始记录动作
    def begin_note(self):
        global begain_time
        global click_flag
        global keyborad_flag
        global local_list
        if self.pushButton_3.text() == "开始录制":
            self.pushButton_3.setText("停止录制")
            local_list = []  # 清空指令列表
            self.mouse_start()
            self.keyboard_start()
            begain_time = time.time()

        elif self.pushButton_3.text() == "停止录制":
            self.pushButton_3.setText("开始录制")
            print("停止录制")
            self.mouse_stop()
            self.keyboard_stop()
            local_list.pop()
            print("动作执行列表:", local_list)

def main():
    app = QApplication(sys.argv)
    win = window()
    win.show()
    sys.exit(app.exec_())


if __name__ == '__main__':
    main()

##ui界面
# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'auto_fun.ui'
#
# Created by: PyQt5 UI code generator 5.15.9
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again.  Do not edit this file unless you know what you are doing.


from PyQt5 import QtCore, QtGui, QtWidgets


class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(600, 300)
        MainWindow.setMinimumSize(QtCore.QSize(600, 300))
        MainWindow.setMaximumSize(QtCore.QSize(600, 300))
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.horizontalLayout_2 = QtWidgets.QHBoxLayout(self.centralwidget)
        self.horizontalLayout_2.setContentsMargins(0, 0, 0, 0)
        self.horizontalLayout_2.setSpacing(0)
        self.horizontalLayout_2.setObjectName("horizontalLayout_2")
        self.frame_2 = QtWidgets.QFrame(self.centralwidget)
        self.frame_2.setMinimumSize(QtCore.QSize(290, 290))
        self.frame_2.setMaximumSize(QtCore.QSize(290, 290))
        self.frame_2.setFrameShape(QtWidgets.QFrame.StyledPanel)
        self.frame_2.setFrameShadow(QtWidgets.QFrame.Raised)
        self.frame_2.setObjectName("frame_2")
        self.lcdNumber = QtWidgets.QLCDNumber(self.frame_2)
        self.lcdNumber.setGeometry(QtCore.QRect(70, 30, 131, 51))
        self.lcdNumber.setStyleSheet("background-color: rgb(0, 111, 166);")
        self.lcdNumber.setObjectName("lcdNumber")
        self.pushButton_3 = QtWidgets.QPushButton(self.frame_2)
        self.pushButton_3.setGeometry(QtCore.QRect(20, 200, 75, 23))
        self.pushButton_3.setStyleSheet("QPushButton\n"
"{\n"
"    /*字体为微软雅黑*/\n"
"    font-family:Microsoft Yahei;\n"
"    /*字体大小为20点*/\n"
"    font-size:10pt;\n"
"    /*字体颜色为白色*/    \n"
"    color:white;\n"
"    /*背景颜色*/  \n"
"    background-color:rgb(170 , 170 , 170);\n"
"    /*边框圆角半径为8像素*/ \n"
"    border-radius:8px;\n"
"}\n"
"QPushButton:hover\n"
"{\n"
"    /*背景颜色*/  \n"
"    background-color:rgb(44 , 137 , 255);\n"
"}\n"
"\n"
"QPushButton:pressed\n"
"{\n"
"    /*背景颜色*/  \n"
"    background-color:rgb(14 , 135 , 228);\n"
"    /*左内边距为3像素,让按下时字向右移动3像素*/  \n"
"    padding-left:3px;\n"
"    /*上内边距为3像素,让按下时字向下移动3像素*/  \n"
"    padding-top:3px;\n"
"}")
        self.pushButton_3.setObjectName("pushButton_3")
        self.pushButton_4 = QtWidgets.QPushButton(self.frame_2)
        self.pushButton_4.setGeometry(QtCore.QRect(170, 200, 75, 23))
        self.pushButton_4.setStyleSheet("QPushButton\n"
"{\n"
"    /*字体为微软雅黑*/\n"
"    font-family:Microsoft Yahei;\n"
"    /*字体大小为20点*/\n"
"    font-size:10pt;\n"
"    /*字体颜色为白色*/    \n"
"    color:white;\n"
"    /*背景颜色*/  \n"
"    background-color:rgb(170 , 170 , 170);\n"
"    /*边框圆角半径为8像素*/ \n"
"    border-radius:8px;\n"
"}\n"
"QPushButton:hover\n"
"{\n"
"    /*背景颜色*/  \n"
"    background-color:rgb(44 , 137 , 255);\n"
"}\n"
"\n"
"QPushButton:pressed\n"
"{\n"
"    /*背景颜色*/  \n"
"    background-color:rgb(14 , 135 , 228);\n"
"    /*左内边距为3像素,让按下时字向右移动3像素*/  \n"
"    padding-left:3px;\n"
"    /*上内边距为3像素,让按下时字向下移动3像素*/  \n"
"    padding-top:3px;\n"
"}")
        self.pushButton_4.setObjectName("pushButton_4")
        self.layoutWidget = QtWidgets.QWidget(self.frame_2)
        self.layoutWidget.setGeometry(QtCore.QRect(80, 140, 104, 22))
        self.layoutWidget.setObjectName("layoutWidget")
        self.formLayout = QtWidgets.QFormLayout(self.layoutWidget)
        self.formLayout.setContentsMargins(0, 0, 0, 0)
        self.formLayout.setObjectName("formLayout")
        self.spinBox = QtWidgets.QSpinBox(self.layoutWidget)
        self.spinBox.setObjectName("spinBox")
        self.formLayout.setWidget(0, QtWidgets.QFormLayout.FieldRole, self.spinBox)
        self.label = QtWidgets.QLabel(self.layoutWidget)
        self.label.setStyleSheet("font: 75 11pt \"Agency FB\";")
        self.label.setObjectName("label")
        self.formLayout.setWidget(0, QtWidgets.QFormLayout.LabelRole, self.label)
        self.horizontalLayout_2.addWidget(self.frame_2)
        self.frame = QtWidgets.QFrame(self.centralwidget)
        self.frame.setMinimumSize(QtCore.QSize(290, 290))
        self.frame.setMaximumSize(QtCore.QSize(290, 290))
        self.frame.setFrameShape(QtWidgets.QFrame.StyledPanel)
        self.frame.setFrameShadow(QtWidgets.QFrame.Raised)
        self.frame.setObjectName("frame")
        self.gridLayout = QtWidgets.QGridLayout(self.frame)
        self.gridLayout.setContentsMargins(0, 0, 0, 0)
        self.gridLayout.setSpacing(0)
        self.gridLayout.setObjectName("gridLayout")
        self.pushButton_2 = QtWidgets.QPushButton(self.frame)
        self.pushButton_2.setStyleSheet("/*按钮按下态*/\n"
"QPushButton:pressed\n"
"{\n"
"    /*背景颜色*/  \n"
" \n"
"    /*左内边距为3像素,让按下时字向右移动3像素*/  \n"
"    padding-left:3px;\n"
"    /*上内边距为3像素,让按下时字向下移动3像素*/  \n"
"    padding-top:3px;\n"
"}")
        self.pushButton_2.setObjectName("pushButton_2")
        self.gridLayout.addWidget(self.pushButton_2, 1, 1, 1, 1)
        self.textEdit = QtWidgets.QTextEdit(self.frame)
        self.textEdit.setMinimumSize(QtCore.QSize(200, 200))
        self.textEdit.setMaximumSize(QtCore.QSize(290, 290))
        self.textEdit.setStyleSheet("background-color: rgb(0, 0, 0);\n"
"color: rgb(255, 255, 255);")
        self.textEdit.setObjectName("textEdit")
        self.gridLayout.addWidget(self.textEdit, 0, 0, 1, 2)
        self.pushButton = QtWidgets.QPushButton(self.frame)
        self.pushButton.setStyleSheet("/*按钮按下态*/\n"
"QPushButton:pressed\n"
"{\n"
"    /*背景颜色*/  \n"
"    /*左内边距为3像素,让按下时字向右移动3像素*/  \n"
"    padding-left:3px;\n"
"    /*上内边距为3像素,让按下时字向下移动3像素*/  \n"
"    padding-top:3px;\n"
"}")
        self.pushButton.setObjectName("pushButton")
        self.gridLayout.addWidget(self.pushButton, 1, 0, 1, 1)
        self.horizontalLayout_2.addWidget(self.frame)
        MainWindow.setCentralWidget(self.centralwidget)

        self.retranslateUi(MainWindow)
        # self.pushButton_4.clicked.connect(MainWindow.lower) # type: ignore
        self.pushButton_2.clicked.connect(self.textEdit.clear) # type: ignore
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        self.pushButton_3.setText(_translate("MainWindow", "开始录制"))
        self.pushButton_4.setText(_translate("MainWindow", "执行"))
        self.label.setText(_translate("MainWindow", "执行次数"))
        self.pushButton_2.setText(_translate("MainWindow", "清空记录"))
        self.pushButton.setText(_translate("MainWindow", "保存记录"))


网站公告

今日签到

点亮在社区的每一天
去签到