pyqt5_windows_hid_auto_detect_hotplug

本文介绍了如何使用Python的PyQt5库结合hidapi在Windows系统中实现实时监控USB设备的插入和移除事件。作者通过注册设备通知,监听WM_DEVICECHANGE消息来检测特定设备的连接状态。

pyqt5 implements hid hotplug auto detect in Windows OS

before using this code should install below package:

pip --install hidapi

pip --install PyQt5

# author: yifei.su
# date: 2023-11-13

import sys
import ctypes
from ctypes.wintypes import MSG
import ctypes.wintypes as wintypes

import hid

from PyQt5.QtWidgets import *
from PyQt5.QtCore import *

NULL = 0
INVALID_HANDLE_VALUE = -1
DEVICE_NOTIFY_WINDOW_HANDLE = 0x00000000
WM_DEVICECHANGE = 0x0219           
DBT_DEVTYP_DEVICEINTERFACE = 5
DBT_DEVICEREMOVECOMPLETE = 0x8004   
DBT_DEVICEARRIVAL = 0x8000         


user32 = ctypes.windll.user32
RegisterDeviceNotification = user32.RegisterDeviceNotificationW
UnregisterDeviceNotification = user32.UnregisterDeviceNotification


class GUID(ctypes.Structure):
    _pack_ = 1
    _fields_ = [("Data1", ctypes.c_ulong),
                ("Data2", ctypes.c_ushort),
                ("Data3", ctypes.c_ushort),
                
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值