You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用Python实现Windows 10屏幕冻结及鼠标禁用功能

Windows屏幕冻结+键鼠禁用实现方案

没有原生CMD命令可以直接实现该需求,通过Python调用系统API是最便捷的实现方式,以下是可直接集成到你的久坐提醒程序的代码方案:

前置依赖

首先安装需要的第三方库:

pip install pynput

本方案用到的Tkinter为Python标准库,无需额外安装。

功能说明

  • 实现全屏置顶覆盖层,遮挡所有桌面内容,模拟屏幕冻结效果
  • 全局拦截所有鼠标、键盘输入事件,普通操作完全失效
  • 支持自定义锁定时长,超时后自动恢复所有权限
  • 不修改系统底层配置,所有操作可恢复,不会导致系统崩溃

完整实现代码

import tkinter as tk
from pynput import mouse, keyboard
import threading
import time

class ScreenLocker:
    def __init__(self, lock_seconds=300):
        self.lock_seconds = lock_seconds
        self.remaining = lock_seconds
        self.running = True

        # 初始化全屏提醒窗口
        self.root = tk.Tk()
        self.root.attributes("-fullscreen", True)
        self.root.attributes("-topmost", True)
        self.root.attributes("-alpha", 0.9)  # 窗口透明度,可调整
        self.root.config(bg="#2c3e50")
        
        # 提醒文本
        self.tip_label = tk.Label(
            self.root,
            text="久坐时间已到,请起身活动\n倒计时结束后自动恢复操作",
            font=("微软雅黑", 40),
            bg="#2c3e50",
            fg="white"
        )
        self.tip_label.pack(expand=True)
        
        # 倒计时文本
        self.countdown_label = tk.Label(
            self.root,
            text=f"{self.remaining} 秒",
            font=("微软雅黑", 60, "bold"),
            bg="#2c3e50",
            fg="#e74c3c"
        )
        self.countdown_label.pack(expand=True)

        # 启动键鼠拦截线程
        self.mouse_listener = mouse.Listener(on_click=self.block_event, on_scroll=self.block_event, on_move=self.block_event)
        self.keyboard_listener = keyboard.Listener(on_press=self.block_event, on_release=self.block_event)
        self.mouse_listener.start()
        self.keyboard_listener.start()

        # 启动倒计时线程
        threading.Thread(target=self.countdown, daemon=True).start()

        self.root.mainloop()

    def block_event(self, *args):
        # 拦截所有键鼠事件,返回False即不传递给系统
        return not self.running

    def countdown(self):
        while self.remaining > 0 and self.running:
            self.countdown_label.config(text=f"{self.remaining} 秒")
            time.sleep(1)
            self.remaining -= 1
        # 倒计时结束,解除锁定
        self.running = False
        self.mouse_listener.stop()
        self.keyboard_listener.stop()
        self.root.destroy()

# 调用示例,锁定300秒即5分钟,可根据需求修改时长
if __name__ == "__main__":
    ScreenLocker(lock_seconds=300)

可选优化

如果需要预留紧急解锁入口,可修改block_event方法增加快捷键判断,示例如下(按Ctrl+Alt+Shift+U可提前解锁):

def block_event(self, key):
    # 仅用于键盘事件的紧急解锁判断,鼠标事件直接拦截
    if isinstance(key, keyboard.KeyCode):
        # 检测组合键Ctrl+Alt+Shift+U
        ctrl_pressed = keyboard.Controller().pressed(keyboard.Key.ctrl)
        alt_pressed = keyboard.Controller().pressed(keyboard.Key.alt)
        shift_pressed = keyboard.Controller().pressed(keyboard.Key.shift)
        if ctrl_pressed and alt_pressed and shift_pressed and key.char == 'u':
            self.running = False
            return True
    return not self.running

注意事项

  • 无法拦截系统内核级快捷键Ctrl+Alt+Del,这是Windows系统安全机制限制,属于正常情况
  • 测试时建议先把锁定时长设为10秒以内,避免误操作导致无法使用电脑
  • 可以根据需求调整窗口透明度、提醒文字、背景颜色等样式,适配你的久坐提醒程序的UI风格

内容的提问来源于stack exchange,提问作者Katkoota

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.25 10:36:08