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

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

背景描述

在笔记本电脑中,在外接键盘的时候,有时我们希望禁用内置键盘,防止意外按键。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:暂停脚本,以便用户可以看到操作结果。
    通过以上步骤,您可以轻松地通过批处理脚本来切换笔记本键盘的启用与禁用状态。
相关推荐
浪潮IT馆13 分钟前
Windows 10 安装 PostgreSQL 9.6.24 完整教程
数据库·windows·postgresql
sukalot1 小时前
Windows 驱动实例分析系列:libwdi 驱动分析 - libwdi 篇(一)
windows
奇树谦1 小时前
Windows 下使用 PowerShell 和 CMD 排查端口占用
windows
万年咸鱼13 小时前
Qt Windows 使用管理员权限运行 Cmd
windows
2601_9623649716 小时前
从录入到解锁,完整梳理 Windows Hello 识别链路
windows·安全·电脑
hacker_LeeFei17 小时前
Windows 下配置 Redis 8.10.2 开机自启:一次踩坑全记录
数据库·windows·redis
赵文宇(温玉)18 小时前
怎么下载 Windows 的 ISO 系统镜像
windows
2601_9621773021 小时前
Windows 安装 Codex CLI 入门级教程
人工智能·windows·node.js·ai编程
shada1 天前
CMake指定 MSVC 工具集与 Windows SDK
c++·windows
the3clipse1 天前
Windows 10 与 Windows 11 视频编解码区别:AV1、HEVC、HDR 与 D3D12 编码支持对比
windows·h.265·hevc·hdr·av1·视频解码·d3d12