添加防止锁屏工具 - Python和Rust版本
This commit is contained in:
207
anti_lockscreen.py
Normal file
207
anti_lockscreen.py
Normal file
@@ -0,0 +1,207 @@
|
||||
import sys
|
||||
import random
|
||||
import threading
|
||||
import time
|
||||
from PySide6.QtWidgets import QApplication, QWidget, QVBoxLayout, QPushButton, QLabel, QSpinBox, QHBoxLayout
|
||||
from PySide6.QtCore import Qt, QThread, Signal
|
||||
from PySide6.QtGui import QFont
|
||||
import pyautogui
|
||||
from loguru import logger
|
||||
|
||||
|
||||
class AntiLockThread(QThread):
|
||||
log_signal = Signal(str)
|
||||
|
||||
def __init__(self, interval=30):
|
||||
super().__init__()
|
||||
self.interval = interval
|
||||
self.running = False
|
||||
|
||||
def run(self):
|
||||
self.running = True
|
||||
self.log_signal.emit("防止锁屏已启动")
|
||||
logger.info("防止锁屏线程已启动")
|
||||
|
||||
while self.running:
|
||||
try:
|
||||
# 获取当前鼠标位置
|
||||
current_x, current_y = pyautogui.position()
|
||||
|
||||
# 随机微动 1-3 像素
|
||||
offset_x = random.choice([-1, 1]) * random.randint(1, 3)
|
||||
offset_y = random.choice([-1, 1]) * random.randint(1, 3)
|
||||
|
||||
# 移动鼠标
|
||||
pyautogui.moveTo(current_x + offset_x, current_y + offset_y, duration=0.1)
|
||||
time.sleep(0.1)
|
||||
pyautogui.moveTo(current_x, current_y, duration=0.1)
|
||||
|
||||
msg = f"鼠标微动: ({current_x}, {current_y}) -> ({current_x + offset_x}, {current_y + offset_y}) -> ({current_x}, {current_y})"
|
||||
self.log_signal.emit(msg)
|
||||
logger.info(msg)
|
||||
|
||||
# 等待指定间隔
|
||||
for _ in range(self.interval):
|
||||
if not self.running:
|
||||
break
|
||||
time.sleep(1)
|
||||
|
||||
except Exception as e:
|
||||
error_msg = f"发生错误: {str(e)}"
|
||||
self.log_signal.emit(error_msg)
|
||||
logger.error(error_msg)
|
||||
time.sleep(1)
|
||||
|
||||
def stop(self):
|
||||
self.running = False
|
||||
self.log_signal.emit("防止锁屏已停止")
|
||||
logger.info("防止锁屏线程已停止")
|
||||
|
||||
|
||||
class AntiLockScreenApp(QWidget):
|
||||
def __init__(self):
|
||||
super().__init__()
|
||||
self.anti_lock_thread = None
|
||||
self.init_ui()
|
||||
|
||||
def init_ui(self):
|
||||
self.setWindowTitle("防止锁屏工具")
|
||||
self.setFixedSize(400, 250)
|
||||
|
||||
# 主布局
|
||||
layout = QVBoxLayout()
|
||||
layout.setSpacing(15)
|
||||
layout.setContentsMargins(20, 20, 20, 20)
|
||||
|
||||
# 标题
|
||||
title_label = QLabel("防止锁屏工具")
|
||||
title_label.setFont(QFont("Microsoft YaHei", 16, QFont.Bold))
|
||||
title_label.setAlignment(Qt.AlignCenter)
|
||||
layout.addWidget(title_label)
|
||||
|
||||
# 说明文字
|
||||
desc_label = QLabel("点击开始后,程序会定期模拟鼠标微动,防止系统自动锁屏")
|
||||
desc_label.setWordWrap(True)
|
||||
desc_label.setAlignment(Qt.AlignCenter)
|
||||
layout.addWidget(desc_label)
|
||||
|
||||
# 间隔设置
|
||||
interval_layout = QHBoxLayout()
|
||||
interval_label = QLabel("微动间隔(秒):")
|
||||
self.interval_spinbox = QSpinBox()
|
||||
self.interval_spinbox.setRange(10, 300)
|
||||
self.interval_spinbox.setValue(30)
|
||||
self.interval_spinbox.setSuffix(" 秒")
|
||||
interval_layout.addStretch()
|
||||
interval_layout.addWidget(interval_label)
|
||||
interval_layout.addWidget(self.interval_spinbox)
|
||||
interval_layout.addStretch()
|
||||
layout.addLayout(interval_layout)
|
||||
|
||||
# 状态标签
|
||||
self.status_label = QLabel("状态: 已停止")
|
||||
self.status_label.setFont(QFont("Microsoft YaHei", 10))
|
||||
self.status_label.setAlignment(Qt.AlignCenter)
|
||||
self.status_label.setStyleSheet("color: red;")
|
||||
layout.addWidget(self.status_label)
|
||||
|
||||
# 日志标签
|
||||
self.log_label = QLabel("")
|
||||
self.log_label.setWordWrap(True)
|
||||
self.log_label.setAlignment(Qt.AlignCenter)
|
||||
self.log_label.setStyleSheet("color: gray; font-size: 10px;")
|
||||
layout.addWidget(self.log_label)
|
||||
|
||||
# 按钮布局
|
||||
button_layout = QHBoxLayout()
|
||||
|
||||
# 开始按钮
|
||||
self.start_btn = QPushButton("开始")
|
||||
self.start_btn.setFixedHeight(40)
|
||||
self.start_btn.setFont(QFont("Microsoft YaHei", 11))
|
||||
self.start_btn.setStyleSheet("""
|
||||
QPushButton {
|
||||
background-color: #4CAF50;
|
||||
color: white;
|
||||
border: none;
|
||||
border-radius: 5px;
|
||||
}
|
||||
QPushButton:hover {
|
||||
background-color: #45a049;
|
||||
}
|
||||
QPushButton:pressed {
|
||||
background-color: #3d8b40;
|
||||
}
|
||||
""")
|
||||
self.start_btn.clicked.connect(self.start_anti_lock)
|
||||
button_layout.addWidget(self.start_btn)
|
||||
|
||||
# 停止按钮
|
||||
self.stop_btn = QPushButton("停止")
|
||||
self.stop_btn.setFixedHeight(40)
|
||||
self.stop_btn.setFont(QFont("Microsoft YaHei", 11))
|
||||
self.stop_btn.setStyleSheet("""
|
||||
QPushButton {
|
||||
background-color: #f44336;
|
||||
color: white;
|
||||
border: none;
|
||||
border-radius: 5px;
|
||||
}
|
||||
QPushButton:hover {
|
||||
background-color: #da190b;
|
||||
}
|
||||
QPushButton:pressed {
|
||||
background-color: #b71c1c;
|
||||
}
|
||||
""")
|
||||
self.stop_btn.clicked.connect(self.stop_anti_lock)
|
||||
self.stop_btn.setEnabled(False)
|
||||
button_layout.addWidget(self.stop_btn)
|
||||
|
||||
layout.addLayout(button_layout)
|
||||
layout.addStretch()
|
||||
|
||||
self.setLayout(layout)
|
||||
|
||||
def start_anti_lock(self):
|
||||
interval = self.interval_spinbox.value()
|
||||
self.anti_lock_thread = AntiLockThread(interval)
|
||||
self.anti_lock_thread.log_signal.connect(self.update_log)
|
||||
self.anti_lock_thread.start()
|
||||
|
||||
self.status_label.setText("状态: 运行中")
|
||||
self.status_label.setStyleSheet("color: green;")
|
||||
self.start_btn.setEnabled(False)
|
||||
self.stop_btn.setEnabled(True)
|
||||
self.interval_spinbox.setEnabled(False)
|
||||
|
||||
def stop_anti_lock(self):
|
||||
if self.anti_lock_thread and self.anti_lock_thread.isRunning():
|
||||
self.anti_lock_thread.stop()
|
||||
self.anti_lock_thread.wait()
|
||||
|
||||
self.status_label.setText("状态: 已停止")
|
||||
self.status_label.setStyleSheet("color: red;")
|
||||
self.start_btn.setEnabled(True)
|
||||
self.stop_btn.setEnabled(False)
|
||||
self.interval_spinbox.setEnabled(True)
|
||||
|
||||
def update_log(self, message):
|
||||
self.log_label.setText(message)
|
||||
logger.info(message)
|
||||
|
||||
def closeEvent(self, event):
|
||||
self.stop_anti_lock()
|
||||
event.accept()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
logger.add("anti_lockscreen.log", rotation="1 MB", retention="7 days")
|
||||
|
||||
app = QApplication(sys.argv)
|
||||
app.setStyle("Fusion")
|
||||
|
||||
window = AntiLockScreenApp()
|
||||
window.show()
|
||||
|
||||
sys.exit(app.exec())
|
||||
Reference in New Issue
Block a user