切换笔记本键盘的启用与禁用状态

使用批处理脚本切换笔记本键盘的启用与禁用状态

背景描述

在笔记本电脑中,在外接键盘的时候,有时我们希望禁用内置键盘,防止意外按键。Windows 系统中,键盘通常与 i8042prt 服务相关联。通过修改这个服务的启动类型,我们可以控制键盘的启用与禁用状态。可以编写一个批处理脚本来自动化这一操作。

详细步骤及代码解释

1. 在管理员模式下运行脚本

在 Windows 中,右键点击批处理文件,并选择"以管理员身份运行"来确保脚本具有必要的权限。

2. 脚本内容

以下是脚本的详细内容,以及每个步骤的解释:

bat 复制代码
@echo off
setlocal enabledelayedexpansion
:: 步骤1: 设置延迟变量扩展
:: 这允许我们在循环中修改变量并立即使用它们的最新值
:: 步骤2: 检查 i8042prt 服务的启动类型
for /f "tokens=4" %%A in ('sc qc i8042prt ^| findstr "START_TYPE"') do (
  set start_type=%%A
)
:: 步骤3: 根据服务的启动类型执行相应的操作
:: 这里是脚本的核心部分,根据服务状态启用或禁用键盘
:: 如果服务当前是禁用状态
if /i "%start_type%"=="DISABLED" (
    echo Service i8042prt is currently disabled. Enabling it...
    sc config i8042prt start=auto
    echo Service i8042prt has been enabled.
) 
:: 如果服务当前是手动启动状态
else if /i "%start_type%"=="DEMAND_START" (
    echo Service i8042prt is currently set to start on demand. Disabling it...
    sc config i8042prt start=disabled
    echo Service i8042prt has been disabled.
) 
:: 如果服务当前是自动启动状态
else if /i "%start_type%"=="AUTO_START" (
    echo Service i8042prt is currently set to start automatically. Disabling it...
    sc config i8042prt start=disabled
    echo Service i8042prt has been disabled.
) 
:: 如果服务启动类型未知
else (
    echo Unable to recognize the start type "%start_type%" for the service i8042prt.
)
:: 步骤4: 结束批处理脚本
:start
endlocal
pause

3. 解释

  • @echo off:关闭命令回显,使输出更清晰简洁。
  • setlocal enabledelayedexpansion:开启延迟变量扩展。
  • for /f "tokens=4" %%A in ('sc qc i8042prt ^| findstr "START_TYPE"') do (...):检索 i8042prt 服务的启动类型。
  • if /i "%start_type%"=="DISABLED" 等条件判断:根据服务当前状态启用或禁用键盘。
  • endlocal:清理脚本中使用的变量。
  • pause:暂停脚本,以便用户可以看到操作结果。
    通过以上步骤,您可以轻松地通过批处理脚本来切换笔记本键盘的启用与禁用状态。
相关推荐
幸福清风8 小时前
【Python】基于Tkinter库实现文件夹拖拽与选择功能
windows·python·microsoft·tkinter
winkel_wang11 小时前
关闭电脑的“快速启动”功能
windows
清风wxy12 小时前
C语言基础数组作业(冒泡算法)
c语言·开发语言·数据结构·c++·windows·算法
ZhengEnCi1 天前
CMD 与 Python 完全区别指南-小白必看的编程入门解惑宝典
windows·python
华阙之梦1 天前
【在 Windows 上运行 Apache Hadoop 或 Spark/GeoTrellis 涉及 HDFS 】
hadoop·windows·apache
电手1 天前
微软宣布删除“另存为”选项,今后文件将默认保存到云盘
windows·microsoft·电脑·onedrive·windows11·windows10
AirDroid_cn2 天前
Win11 远程桌面:连接公司电脑时,提示 “证书错误” 如何解决?
windows·网络协议·https·ssl·电脑技巧
blackorbird2 天前
使用 Overpass Turbo 查找监控摄像头
运维·服务器·数据库·windows
zhuyasen2 天前
踩坑实录:Go 1.25.x 编译的 exe 在 Windows 提示“此应用无法运行”
windows·golang
千歌叹尽执夏2 天前
ubuntu24.04lts和Windows11家庭版远程桌面连接若干问题(解决)
windows·远程连接·xrdp·ubuntu24.04lts