最新消息: USBMI致力于为网友们分享Windows、安卓、IOS等主流手机系统相关的资讯以及评测、同时提供相关教程、应用、软件下载等服务。

python Windows下面监听USB插拔、按键事件

业界 admin 10浏览 0评论

在开发python的时候,我们有时候想监听USB插拔、按键事件。废话不多说,直接上代码吧,代码如下:

import sys
from PyQt5.QtWidgets import QMainWindow, QApplication
from ctypes.wintypes import MSG
import datetime
import ctypes
from ctypes.wintypes import MSG
import ctypes.wintypes as wintypes


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

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),
                ("Data4", ctypes.c_

在开发python的时候,我们有时候想监听USB插拔、按键事件。废话不多说,直接上代码吧,代码如下:

import sys
from PyQt5.QtWidgets import QMainWindow, QApplication
from ctypes.wintypes import MSG
import datetime
import ctypes
from ctypes.wintypes import MSG
import ctypes.wintypes as wintypes


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

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),
                ("Data4", ctypes.c_
发布评论

评论列表 (0)

  1. 暂无评论