from PyQt5 import QtWidgets, uic, QtCore
from pynput import keyboard, mouse
import pygetwindow as gw
from PyQt5.QtGui import QPainter, QColor, QCursor
from PyQt5.QtCore import Qt, QRectF
from pynput.mouse import Button, Controller
import pyautogui
import threading
import time
import os
ui_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'form.ui')
key_time_dict = {} # Global Dictionary to store keys and times
window = None # Global Window
controller = None # Global Controller
mouseX = 0 # Global Mouse X
mouseY = 0 # Global Mouse Y
class MyLineEdit(QtWidgets.QLineEdit):
def mousePressEvent(self, event):
super(MyLineEdit, self).mousePressEvent(event) # 让QLineEdit处理其鼠标点击事件
print(f'----------------Mouse left button pressed: {event.text()}')
def keyPressEvent(self, event):
super(MyLineEdit, self).keyPressEvent(event)
key = event.key()
print(f'------------------Key pressed: {key}')
# Now `key` holds the key code of the pressed key.
# Do something with it…
key_char = event.text()
print(f'-----------------Pressed key char: {key_char}')
class ToggleButton(QtWidgets.QPushButton):
def __init__(self, parent=None):
super(ToggleButton, self).__init__(parent)
self.setCheckable(True)
self.setMinimumWidth(66)
self.setMinimumHeight(28)
def paintEvent(self, event):
label = "ON" if self.isChecked() else "OFF"
bg_color = QColor(0, 155, 0) if self.isChecked() else QColor(155, 155, 155)
radius = 10
width = 28
center = self.rect().center()
painter = QPainter(self)
painter.setRenderHint(QPainter.Antialiasing)
painter.translate(center)
painter.setBrush(QColor(255, 255, 255))
pen = painter.pen()
pen.setWidth(2)
pen.setColor(QColor(155, 155, 155))
painter.setPen(pen)
painter.drawRoundedRect(QRectF(-width, -radius, 2*width, 2*radius), radius, radius)
painter.setBrush(QColor(bg_color))
sw_rect = QRectF(-radius, -radius, width, 2*radius)
if not self.isChecked():
sw_rect.moveLeft(-width)
else:
sw_rect.moveRight(width)
painter.drawRoundedRect(sw_rect, radius, radius)
painter.setBrush(QColor(255, 255, 255))
painter.drawText(sw_rect, Qt.AlignCenter, label)
class Ui(QtWidgets.QMainWindow):
def __init__(self):
super(Ui, self).__init__()
uic.loadUi(ui_path, self)
# ==================== KeyBoard Auto Press ====================
self.button = self.findChild(QtWidgets.QPushButton, 'Add')
self.button.clicked.connect(self.buttonClicked)
self.line_edit = self.findChild(QtWidgets.QLineEdit, 'lineName')
self.spin_box = self.findChild(QtWidgets.QDoubleSpinBox, 'doubleSpinBox')
self.list_widget = self.findChild(QtWidgets.QListWidget, 'listWidget')
self.statusButton = self.findChild(QtWidgets.QPushButton, 'statusButton')
tmpParent = self.statusButton.parent() # Store the parent of the QPushButton
tmpGeometry = self.statusButton.geometry() # Store the geometry of the QPushButton
self.statusButton.setParent(None) # Unparent the QPushButton
self.statusButton = ToggleButton(self) # Replace with ToggleButton
# tmpParent.layout().addWidget(self.statusButton) # Add the ToggleButton to the layout of the parent
self.statusButton.setGeometry(tmpGeometry) # Set geometry or place it where you want
self.statusButton.clicked.connect(lambda: self.statusButtonClicked(keyboard.Key.f1))
self.statusButton.toggled.connect(lambda: print(f"Toggle button state: {self.statusButton.isChecked()}"))
# ==================== Mouse Auto Press ====================
self.xPos = self.findChild(QtWidgets.QLineEdit, 'xPos')
self.yPos = self.findChild(QtWidgets.QLineEdit, 'yPos')
self.mouseDelay = self.findChild(QtWidgets.QDoubleSpinBox, 'mouseDelay')
self.mouseStatusButton = self.findChild(QtWidgets.QPushButton, 'mouseStatusButton')
tmpParent = self.mouseStatusButton.parent() # Store the parent of the QPushButton
tmpGeometry = self.mouseStatusButton.geometry() # Store the geometry of the QPushButton
self.mouseStatusButton.setParent(None) # Unparent the QPushButton
self.mouseStatusButton = ToggleButton(self) # Replace with ToggleButton
# tmpParent.layout().addWidget(self.statusButton) # Add the ToggleButton to the layout of the parent
self.mouseStatusButton.setGeometry(tmpGeometry) # Set geometry or place it where you want
# self.mouseStatusButton.clicked.connect(lambda: self.statusButtonClicked(keyboard.Key.f1))
self.mouseStatusButton.toggled.connect(lambda: print(f"Toggle mouseStatusButton state: {self.mouseStatusButton.isChecked()}"))
# ==================== Key Map ====================
self.buttonAddMap = self.findChild(QtWidgets.QPushButton, 'AddMap')
self.buttonAddMap.clicked.connect(self.addMapButtonClicked)
self.fromKey = self.findChild(QtWidgets.QLineEdit, 'fromKey')
self.toKey = self.findChild(QtWidgets.QLineEdit, 'toKey')
tmpLineEditParent = self.fromKey.parent() # Store the parent of the QPushButton
tmpLineEditGeometry = self.fromKey.geometry() # Store the geometry of the QPushButton
self.fromKey.setParent(None) # Unparent the QPushButton
self.fromKey = MyLineEdit(self) # Replace with ToggleButton
self.fromKey.setGeometry(tmpLineEditGeometry) # Set geometry or place it where you want
# tmpLineEditParent.layout().addWidget(self.fromKey) # Add the ToggleButton to the layout of the parent
self.mapStatusButton = self.findChild(QtWidgets.QPushButton, 'mapStatusButton')
tmpParent = self.mapStatusButton.parent() # Store the parent of the QPushButton
tmpGeometry = self.mapStatusButton.geometry() # Store the geometry of the QPushButton
self.mapStatusButton.setParent(None) # Unparent the QPushButton
self.mapStatusButton = ToggleButton(self) # Replace with ToggleButton
# tmpParent.layout().addWidget(self.statusButton) # Add the ToggleButton to the layout of the parent
self.mapStatusButton.setGeometry(tmpGeometry) # Set geometry or place it where you want
self.mapStatusButton.clicked.connect(lambda: self.statusButtonClicked(keyboard.Key.f4))
self.mapStatusButton.toggled.connect(lambda: print(f"Toggle mapStatusButton state: {self.mapStatusButton.isChecked()}"))
self.show()
def buttonClicked(self):
try:
key = self.line_edit.text()
time = self.spin_box.value()
# 添加前检查是否重复,如果重复则不添加
if key in key_time_dict or key not in pyautogui.KEYBOARD_KEYS:
return
key_time_dict[key] = time
self.list_widget.addItem(f'Key: {key} : Sec: {time}')
except Exception as e:
print(e)
# 按键映射添加按钮 @todo
def addMapButtonClicked(self):
try:
key = self.line_edit.text()
time = self.spin_box.value()
# 添加前检查是否重复,如果重复则不添加
if key in key_time_dict or key not in pyautogui.KEYBOARD_KEYS:
return
key_time_dict[key] = time
self.list_widget.addItem(f'Key: {key} : Sec: {time}')
except Exception as e:
print(e)
def statusButtonClicked(self, key):
print("statusButtonClicked")
try:
controller.on_press(key)
except Exc