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

如何用Python获取Windows 10设备的笔和触摸属性信息?

获取Windows 10笔和触摸设备信息的Python方案

下面是几种可行的方案,用于获取Windows 10设备的笔和触摸属性信息:

方法1:通过WMI查询触摸/笔设备(推荐)

借助pywin32包调用Windows Management Instrumentation (WMI),可以直接查询系统中的触摸控制器和笔设备。

首先安装依赖包:

pip install pywin32

示例代码:

import win32com.client

def get_touch_pen_details():
    wmi = win32com.client.GetObject("winmgmts:")
    
    # 查询触摸控制器
    print("=== 触摸控制器信息 ===")
    for controller in wmi.InstancesOf("Win32_TouchController"):
        print(f"设备名称: {controller.Name}")
        print(f"硬件ID: {controller.HardwareID}")
        print(f"设备状态: {controller.Status}\n")
    
    # 查询笔设备(部分笔设备归类在通用PnP设备中,通过关键词筛选)
    print("=== 笔设备信息 ===")
    pen_query = "SELECT * FROM Win32_PnPEntity WHERE Description LIKE '%Pen%' OR PNPDeviceID LIKE '%Pen%'"
    for device in wmi.ExecQuery(pen_query):
        print(f"设备名称: {device.Name}")
        print(f"设备描述: {device.Description}")
        print(f"硬件ID: {device.HardwareID}\n")

if __name__ == "__main__":
    get_touch_pen_details()

方法2:用ctypes判断触摸/笔支持性

如果只需判断系统是否支持触摸或笔输入,无需详细设备信息,可以直接调用Windows API:

import ctypes

def check_touch_pen_support():
    user32 = ctypes.windll.user32
    SM_DIGITIZER = 94
    status = user32.GetSystemMetrics(SM_DIGITIZER)
    
    # 解析位掩码判断设备支持情况(参考Windows官方API文档)
    supports_touch = (status & 0x00000001) != 0  # 集成触摸设备
    supports_pen = (status & 0x00000004) != 0     # 集成笔设备
    
    print(f"支持触摸输入: {'是' if supports_touch else '否'}")
    print(f"支持笔输入: {'是' if supports_pen else '否'}")

if __name__ == "__main__":
    check_touch_pen_support()

方法3:读取注册表获取设备详情

Windows注册表存储了所有硬件设备的元数据,可以直接遍历HID设备项筛选触摸和笔设备:

import winreg

def get_registry_device_info():
    hid_root_path = r"SYSTEM\CurrentControlSet\Enum\HID"
    
    with winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, hid_root_path) as hid_key:
        index = 0
        while True:
            try:
                device_id = winreg.EnumKey(hid_key, index)
                device_subpath = f"{hid_root_path}\\{device_id}"
                
                with winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, device_subpath) as sub_key:
                    sub_index = 0
                    while True:
                        try:
                            instance_id = winreg.EnumKey(sub_key, sub_index)
                            instance_fullpath = f"{device_subpath}\\{instance_id}"
                            
                            with winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, instance_fullpath) as inst_key:
                                friendly_name, _ = winreg.QueryValueEx(inst_key, "FriendlyName")
                                if "Touch" in friendly_name or "Pen" in friendly_name:
                                    print(f"设备名称: {friendly_name}")
                                    print(f"设备注册表路径: {instance_fullpath}\n")
                            
                            sub_index += 1
                        except OSError:
                            break
                index += 1
            except OSError:
                break

if __name__ == "__main__":
    get_registry_device_info()

内容的提问来源于stack exchange,提问作者acs123

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.14 06:50:14