python 如何监听U盘插拔事件
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python 如何监听U盘插拔事件相关的知识,希望对你有一定的参考价值。
windows XP平台下, 当U盘插入系统或者从系统中拔出时,如何通过Python监听U盘插拔事件,谢谢!!
可以这么做,参考:import win32api, win32con, win32gui
from ctypes import *
#
# Device change events (WM_DEVICECHANGE wParam)
#
DBT_DEVICEARRIVAL = 0x8000
DBT_DEVICEQUERYREMOVE = 0x8001
DBT_DEVICEQUERYREMOVEFAILED = 0x8002
DBT_DEVICEMOVEPENDING = 0x8003
DBT_DEVICEREMOVECOMPLETE = 0x8004
DBT_DEVICETYPESSPECIFIC = 0x8005
DBT_CONFIGCHANGED = 0x0018
#
# type of device in DEV_BROADCAST_HDR
#
DBT_DEVTYP_OEM = 0x00000000
DBT_DEVTYP_DEVNODE = 0x00000001
DBT_DEVTYP_VOLUME = 0x00000002
DBT_DEVTYPE_PORT = 0x00000003
DBT_DEVTYPE_NET = 0x00000004
#
# media types in DBT_DEVTYP_VOLUME
#
DBTF_MEDIA = 0x0001
DBTF_NET = 0x0002
WORD = c_ushort
DWORD = c_ulong
class DEV_BROADCAST_HDR (Structure):
_fields_ = [
("dbch_size", DWORD),
("dbch_devicetype", DWORD),
("dbch_reserved", DWORD)
]
class DEV_BROADCAST_VOLUME (Structure):
_fields_ = [
("dbcv_size", DWORD),
("dbcv_devicetype", DWORD),
("dbcv_reserved", DWORD),
("dbcv_unitmask", DWORD),
("dbcv_flags", WORD)
]
def drive_from_mask (mask):
n_drive = 0
while 1:
if (mask & (2 ** n_drive)): return n_drive
else: n_drive += 1
class Notification:
def __init__(self):
message_map =
win32con.WM_DEVICECHANGE : self.onDeviceChange
wc = win32gui.WNDCLASS ()
hinst = wc.hInstance = win32api.GetModuleHandle (None)
wc.lpszClassName = "DeviceChangeDemo"
wc.style = win32con.CS_VREDRAW | win32con.CS_HREDRAW;
wc.hCursor = win32gui.LoadCursor (0, win32con.IDC_ARROW)
wc.hbrBackground = win32con.COLOR_WINDOW
wc.lpfnWndProc = message_map
classAtom = win32gui.RegisterClass (wc)
style = win32con.WS_OVERLAPPED | win32con.WS_SYSMENU
self.hwnd = win32gui.CreateWindow (
classAtom,
"Device Change Demo",
style,
0, 0,
win32con.CW_USEDEFAULT, win32con.CW_USEDEFAULT,
0, 0,
hinst, None
)
def onDeviceChange (self, hwnd, msg, wparam, lparam):
#
# WM_DEVICECHANGE:
# wParam - type of change: arrival, removal etc.
# lParam - what\'s changed?
# if it\'s a volume then...
# lParam - what\'s changed more exactly
#
dev_broadcast_hdr = DEV_BROADCAST_HDR.from_address (lparam)
if wparam == DBT_DEVICEARRIVAL:
print "Something\'s arrived"
if dev_broadcast_hdr.dbch_devicetype == DBT_DEVTYP_VOLUME:
print "It\'s a volume!"
dev_broadcast_volume = DEV_BROADCAST_VOLUME.from_address (lparam)
if dev_broadcast_volume.dbcv_flags &; DBTF_MEDIA:
print "with some media"
drive_letter = drive_from_mask (dev_broadcast_volume.dbcv_unitmask)
print "in drive", chr (ord ("A") + drive_letter)
return 1
if __name__==\'__main__\':
w = Notification ()
win32gui.PumpMessages ()追问
您好,这个程序我在Pycharm中执行, 插入U盘后,弹出错误框.
不可能
参考技术A watchdog 包electron 监听U盘插拔事件,并获得U盘路径
思路,用electron监听窗口事件,在监听的回调里,运行C#代码,获取当前电脑的U盘路径,获取到就返回路径,获取不到就返回0
在这里卡了好久,分享出来让大家参考
(环境没必要和我的一样)
我的环境:node 10.2.0
electron 7.3.1
依赖: "electron-edge-js": "^12.14.2"
代码 :
hookWindowMessage electron 官方方法:链接https://www.electronjs.org/docs/api/browser-window#winhookwindowmessagemessage-callback-windows
mainWindow.hookWindowMessage(0x0219, (wParam, lParam) => {
//不要以为下面代码中的 /**/是注释。在edge中想要执行C#代码 就需要 /**/包括着
var edge = require("electron-edge-js");
const findUSB=edge.func(function () {/*
using System.IO;
async (input) => {
DriveInfo[] allDrives = DriveInfo.GetDrives();
foreach (DriveInfo d in allDrives)
{
if (d.DriveType == DriveType.Removable)
{
return d.Name;
}
}
return "0";
}
*/});
findUSB(‘0‘,function (error, result) {
if (error) throw error;
console.log(result);
if(result!=="0"){
console.log(fs.readdirSync(result))
}
});
})
以上是关于python 如何监听U盘插拔事件的主要内容,如果未能解决你的问题,请参考以下文章
用win32 API监听U盘插拔并取得其盘符/取得当前插入U盘的盘符
Android 高性能音频Oboe 音频流打开后 耳机 / 音箱 插拔事件处理 ( 动态注册广播接收者监听耳机插拔事件 | 重新打开 Oboe 音频流 )