可通过powershell调用blockinput api禁用键盘,需管理员权限运行批处理文件,锁键后所有输入(含ctrl+alt+del)失效;解锁需另建脚本或设置自动恢复延迟。

想在Windows系统中快速禁用所有键盘输入,防止他人误操作或临时保护隐私,可以用批处理配合PowerShell命令实现锁键功能。
创建基础锁键批处理文件
新建一个文本文档,将后缀名改为 .bat,例如命名为 lock_keyboard.bat。
右键该文件→“编辑”,粘贴以下内容:
@echo off
PowerShell -Command "Add-Type -AssemblyName System.Windows.Forms; [System.Windows.Forms.SendKeys]::SendWait('{NUMLOCK}')"
这行PowerShell命令本身不锁键,但为后续操作铺路——它触发一次系统级按键事件,确保后续API调用环境就绪。
调用Windows API真正禁用键盘
批处理无法直接调用底层API,必须借助PowerShell执行Win32函数。替换上一步中的命令为:
@echo off
PowerShell -Command "$sig='[DllImport(\"user32.dll\")]public static extern bool BlockInput(bool fBlock);'; $type=Add-Type -MemberDefinition $sig -Name 'Win32' -Namespace 'Keyboard' -PassThru; $type::BlockInput($true)"
【必须以管理员身份运行】,否则BlockInput将静默失败,键盘依然可用。
保存文件,双击运行后键盘立即无响应,包括Ctrl+Alt+Del、Win键、所有USB/蓝牙键盘输入均被拦截。
解锁键盘的配套脚本
方法一:新建另一个批处理文件 unlock_keyboard.bat,内容为:
@echo off
PowerShell -Command "$sig='[DllImport(\"user32.dll\")]public static extern bool BlockInput(bool fBlock);'; $type=Add-Type -MemberDefinition $sig -Name 'Win32' -Namespace 'Keyboard' -PassThru; $type::BlockInput($false)"
方法二:在同一锁键脚本末尾追加延迟解锁(慎用):
@echo off
PowerShell -Command "$sig='[DllImport(\"user32.dll\")]public static extern bool BlockInput(bool fBlock);'; $type=Add-Type -MemberDefinition $sig -Name 'Win32' -Namespace 'Keyboard' -PassThru; $type::BlockInput($true); Start-Sleep -Seconds 30; $type::BlockInput($false)"
这会在锁定30秒后自动恢复,适合演示或定时防护场景,但若中途需提前操作则无法中断。











