Simple Mouse Monitor 说明文档
概述
simple_mouse_monitor.py
是一个简单的鼠标监控工具,用于监控鼠标位置并在鼠标移动到屏幕右下角时自动锁定 Windows 系统屏幕。这个工具特别适合需要快速锁屏保护隐私的场景。
功能特性
- 自动锁屏: 当鼠标移动到屏幕右下角指定区域时,自动触发系统锁屏
- 可配置参数: 支持自定义触发区域大小和检查间隔
- 多线程设计: 使用独立线程进行监控,不阻塞主程序
- 异常处理: 包含完善的错误处理机制
- 用户友好: 提供清晰的状态提示和操作说明
系统要求
- 操作系统: Windows (使用 Windows API)
- Python 版本: Python 3.x
-
依赖库:
-
ctypes
(Python 标准库) -
threading
(Python 标准库) -
time
(Python 标准库)
-
类结构
SimpleMouseMonitor 类
主要的监控类,包含以下方法:
构造函数
__init__(self, corner_size=50, check_interval=0.1)
-
corner_size
: 右下角触发区域的大小(像素),默认 50 -
check_interval
: 鼠标位置检查间隔(秒),默认 0.1
主要方法
- get_mouse_position(): 获取当前鼠标坐标位置
- is_in_bottom_right_corner(x, y): 检查鼠标是否在右下角触发区域
- lock_screen(): 调用 Windows API 锁定屏幕
- monitor_loop(): 主监控循环,持续检查鼠标位置
- start(): 启动监控服务
- stop(): 停止监控服务
使用方法
基本使用
# 使用默认参数创建监控器
monitor = SimpleMouseMonitor()
monitor.start()
自定义参数
# 自定义触发区域大小和检查间隔
monitor = SimpleMouseMonitor(corner_size=100, check_interval=0.05)
monitor.start()
直接运行
python simple_mouse_monitor.py
工作原理
- 初始化: 获取屏幕分辨率,设置触发区域参数
- 监控循环: 在独立线程中持续检查鼠标位置
- 位置检测: 每隔指定时间间隔获取鼠标坐标
- 触发判断: 检查鼠标是否在屏幕右下角指定区域内
-
执行锁屏: 调用 Windows API
LockWorkStation()
锁定屏幕 - 防重复触发: 锁屏后暂停 2 秒,避免重复触发
配置参数说明
参数 | 类型 | 默认值 | 说明 |
---|---|---|---|
corner_size | int | 50 | 右下角触发区域的边长(像素) |
check_interval | float | 0.1 | 鼠标位置检查间隔(秒) |
使用场景
- 办公环境: 需要快速锁屏保护工作内容
- 公共场所: 临时离开时快速保护隐私
- 演示场景: 演示过程中需要快速锁屏
- 安全要求: 对屏幕安全有较高要求的环境
注意事项
- Windows 专用: 此工具仅适用于 Windows 系统
- 管理员权限: 某些情况下可能需要管理员权限才能正常锁屏
- 触发区域: 默认触发区域为右下角 50x50 像素,可根据需要调整
- 退出方式: 使用 Ctrl+C 组合键退出程序
- 性能影响: 检查间隔设置过小可能会增加 CPU 使用率
故障排除
常见问题
-
锁屏不生效
- 检查是否有足够的系统权限
- 确认 Windows 锁屏功能是否正常
-
程序无响应
- 使用 Ctrl+C 强制退出
- 检查是否有异常错误信息
-
触发过于敏感
- 增大
corner_size
参数 - 调整
check_interval
参数
- 增大
扩展建议
- 添加其他触发区域(左上角、左下角等)
- 支持自定义快捷键触发
- 添加声音提示功能
- 支持配置文件保存设置
- 添加系统托盘图标
版本信息
- 当前版本: 1.0
- 最后更新: 2025年
- 兼容性: Windows 7/8/10/11
许可证
本工具为开源软件,具体许可证信息请查看项目根目录。
以下为源代码
import time
import ctypes
from ctypes import wintypes
import threading
class SimpleMouseMonitor:
def __init__(self, corner_size=50, check_interval=0.1):
"""
初始化鼠标监控器
:param corner_size: 右下角触发区域的大小(像素)
:param check_interval: 检查间隔(秒)
"""
self.corner_size = corner_size
self.check_interval = check_interval
self.running = False
self.screen_width = ctypes.windll.user32.GetSystemMetrics(0)
self.screen_height = ctypes.windll.user32.GetSystemMetrics(1)
def get_mouse_position(self):
"""获取当前鼠标位置"""
point = wintypes.POINT()
ctypes.windll.user32.GetCursorPos(ctypes.byref(point))
return point.x, point.y
def is_in_bottom_right_corner(self, x, y):
"""检查鼠标是否在右下角"""
return (x >= self.screen_width - self.corner_size and
y >= self.screen_height - self.corner_size)
def lock_screen(self):
"""锁定屏幕"""
print("检测到鼠标在右下角,正在锁屏...")
ctypes.windll.user32.LockWorkStation()
def monitor_loop(self):
"""监控循环"""
print(f"开始监控鼠标移动...")
print(f"屏幕分辨率: {self.screen_width}x{self.screen_height}")
print(f"右下角触发区域: {self.corner_size}x{self.corner_size} 像素")
print("移动鼠标到右下角即可锁屏,按 Ctrl+C 退出")
while self.running:
try:
x, y = self.get_mouse_position()
if self.is_in_bottom_right_corner(x, y):
self.lock_screen()
# 锁屏后暂停一段时间,避免重复触发
time.sleep(2)
time.sleep(self.check_interval)
except Exception as e:
print(f"监控过程中出现错误: {e}")
break
def start(self):
"""启动监控"""
self.running = True
monitor_thread = threading.Thread(target=self.monitor_loop)
monitor_thread.daemon = True
monitor_thread.start()
try:
while self.running:
time.sleep(1)
except KeyboardInterrupt:
print("\n正在停止监控...")
self.stop()
def stop(self):
"""停止监控"""
self.running = False
print("监控已停止")
if __name__ == "__main__":
# 创建监控器实例
# corner_size: 右下角触发区域大小(默认50像素)
# check_interval: 检查间隔(默认0.1秒)
monitor = SimpleMouseMonitor(corner_size=50, check_interval=0.1)
# 启动监控
monitor.start()