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

如何用Python从设备管理器检查设备状态并实现触摸屏启停切换

检查并切换笔记本触摸屏状态的Python方案

在Windows环境下,可以通过调用系统工具(devcon或PowerShell命令)结合Python实现设备状态检查与切换,以下是具体实现:

方案一:使用devcon工具

前置准备

  1. 获取devcon.exe:从Windows SDK的bin目录中提取,或者将其放置在系统环境变量PATH包含的路径(如C:\Windows\System32),确保命令行可直接调用。
  2. 获取触摸屏硬件ID:打开设备管理器 → 找到触摸屏设备 → 右键「属性」→「详细信息」→ 选择「硬件ID」,复制对应的ID(格式示例:HID\VID_04F3&PID_0C1A&COL01)。

Python代码实现

import subprocess

def get_touchscreen_status(hardware_id):
    """查询触摸屏当前状态"""
    result = subprocess.run(
        ["devcon", "status", hardware_id],
        capture_output=True,
        text=True,
        check=False
    )
    output = result.stdout.lower()
    if "running" in output:
        return "启用"
    elif "disabled" in output:
        return "禁用"
    return "未知"

def toggle_touchscreen(hardware_id):
    """切换触摸屏启用/禁用状态"""
    current_status = get_touchscreen_status(hardware_id)
    if current_status == "启用":
        subprocess.run(["devcon", "disable", hardware_id], check=True)
        print("触摸屏已禁用")
    elif current_status == "禁用":
        subprocess.run(["devcon", "enable", hardware_id], check=True)
        print("触摸屏已启用")
    else:
        print("无法识别设备状态,操作终止")

if __name__ == "__main__":
    # 替换为你的触摸屏硬件ID
    TOUCHSCREEN_HWID = "HID\\VID_XXXX&PID_XXXX&COL01"
    
    current_status = get_touchscreen_status(TOUCHSCREEN_HWID)
    print(f"当前触摸屏状态:{current_status}")
    toggle_touchscreen(TOUCHSCREEN_HWID)

方案二:使用PowerShell命令(无需额外工具)

如果不想安装devcon,可以直接调用PowerShell的设备管理命令,通过设备名称匹配:

import subprocess

def get_touchscreen_status(device_name):
    """通过设备名称查询状态"""
    cmd = f"Get-PnpDevice -FriendlyName '*{device_name}*' | Select-Object -ExpandProperty Status"
    result = subprocess.run(
        ["powershell", cmd],
        capture_output=True,
        text=True,
        check=False
    )
    return result.stdout.strip()

def toggle_touchscreen(device_name):
    """切换触摸屏状态"""
    current_status = get_touchscreen_status(device_name)
    if current_status == "OK":
        # 禁用设备
        cmd = f"Disable-PnpDevice -FriendlyName '*{device_name}*' -Confirm:$false"
        subprocess.run(["powershell", cmd], check=True)
        print("触摸屏已禁用")
    elif current_status == "Error":
        # 启用设备
        cmd = f"Enable-PnpDevice -FriendlyName '*{device_name}*' -Confirm:$false"
        subprocess.run(["powershell", cmd], check=True)
        print("触摸屏已启用")
    else:
        print(f"无法识别设备状态:{current_status}")

if __name__ == "__main__":
    # 替换为你的触摸屏设备名称(如"触摸屏"或"Touch Screen")
    TOUCHSCREEN_NAME = "触摸屏"
    
    current_status = get_touchscreen_status(TOUCHSCREEN_NAME)
    print(f"当前触摸屏状态:{current_status}")
    toggle_touchscreen(TOUCHSCREEN_NAME)

关键注意事项

  • 必须以管理员身份运行Python脚本,修改设备状态需要系统权限。
  • 设备名称或硬件ID需准确匹配,否则会出现无法识别设备的问题。

内容的提问来源于stack exchange,提问作者David Tomić

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.13 00:37:35