自动启用禁用触摸板的脚本

wen 实用脚本 2

本文目录导读:

自动启用禁用触摸板的脚本

  1. Windows PowerShell 脚本
  2. 自动检测并切换脚本(更智能版本)
  3. 运行方式
  4. 注意事项

Windows PowerShell 脚本

# toggle-touchpad.ps1
# 自动切换触摸板启用/禁用状态
# 查找触摸板设备
$touchpad = Get-PnpDevice | Where-Object { 
    $_.FriendlyName -like "*触摸板*" -or 
    $_.FriendlyName -like "*Touchpad*" -or 
    $_.FriendlyName -like "*触控板*"
}
if (-not $touchpad) {
    Write-Host "未找到触摸板设备" -ForegroundColor Red
    exit 1
}
# 切换状态
if ($touchpad.Status -eq "OK") {
    Disable-PnpDevice -InstanceId $touchpad.InstanceId -Confirm:$false
    Write-Host "触摸板已禁用" -ForegroundColor Yellow
} else {
    Enable-PnpDevice -InstanceId $touchpad.InstanceId -Confirm:$false
    Write-Host "触摸板已启用" -ForegroundColor Green
}

自动检测并切换脚本(更智能版本)

# smart-toggle-touchpad.ps1
# 智能切换 - 检测外接鼠标时自动禁用触摸板
$touchpad = Get-PnpDevice | Where-Object { 
    $_.FriendlyName -like "*触摸板*" -or 
    $_.FriendlyName -like "*Touchpad*"
}
$mouse = Get-PnpDevice | Where-Object { 
    $_.FriendlyName -like "*鼠标*" -or 
    $_.FriendlyName -like "*Mouse*"
}
if (-not $touchpad) {
    Write-Host "未找到触摸板" -ForegroundColor Red
    exit
}
# 检查是否有外接鼠标
$hasExternalMouse = $mouse | Where-Object { 
    $_.Status -eq "OK" -and 
    $_.FriendlyName -notlike "*HID*" -and 
    $_.FriendlyName -notlike "*PS/2*"
}
if ($hasExternalMouse) {
    # 有外接鼠标,禁用触摸板
    if ($touchpad.Status -eq "OK") {
        Disable-PnpDevice -InstanceId $touchpad.InstanceId -Confirm:$false
        Write-Host "检测到外接鼠标,已自动禁用触摸板" -ForegroundColor Yellow
    } else {
        Write-Host "触摸板已经禁用" -ForegroundColor Gray
    }
} else {
    # 没有外接鼠标,启用触摸板
    if ($touchpad.Status -ne "OK") {
        Enable-PnpDevice -InstanceId $touchpad.InstanceId -Confirm:$false
        Write-Host "未检测到外接鼠标,已自动启用触摸板" -ForegroundColor Green
    } else {
        Write-Host "触摸板已经启用" -ForegroundColor Gray
    }
}

运行方式

方法1:手动运行

  1. 保存脚本为 .ps1 文件
  2. 以管理员身份打开 PowerShell
  3. 执行:Set-ExecutionPolicy RemoteSigned -Scope CurrentUser
  4. 运行:.\toggle-touchpad.ps1

方法2:创建快捷方式

# 创建 toggle-touchpad.bat 批处理文件
@echo off
powershell -Command "Start-Process PowerShell -Verb RunAs -ArgumentList '-NoProfile -ExecutionPolicy Bypass -File ""%~dp0toggle-touchpad.ps1""'"

方法3:设置自动定时任务

# 创建每分钟检查一次的计划任务
$action = New-ScheduledTaskAction -Execute "PowerShell.exe" -Argument "-NoProfile -ExecutionPolicy Bypass -File C:\scripts\smart-toggle-touchpad.ps1"
$trigger = New-ScheduledTaskTrigger -Once -At (Get-Date).AddMinutes(1) -RepetitionInterval (New-TimeSpan -Minutes 1) -RepetitionDuration (New-TimeSpan -Days 365)
Register-ScheduledTask -TaskName "AutoToggleTouchpad" -Action $action -Trigger $trigger -RunLevel Highest

注意事项

  • 需要管理员权限运行
  • 不同电脑的触摸板名称可能不同,可能需要修改 FriendlyName 的匹配条件
  • 部分触摸板可能不支持通过 PowerShell 控制

需要我帮你适配特定品牌的触摸板,或者添加其他功能吗?

抱歉,评论功能暂时关闭!