This uses my (unpublished) Window Message libray but it is based on the same code as Dimitris used for dzEventsMagic. Make a dzEventMagic object called WMevent and change the event name to WMevent_MagicEvent.
Note that you get multiple messages, hence the check for a time window. You will have to track whether the device is present or not. Also note that you may need a delay after getting the event before the device is visible. You will have to play with this for yourself.
This event may happen for other things than a USB device. This works on my desktop for USB sticks and should work on a device with a USB port but for what range of devices I have no idea.
Code:
Sub Globals
'Declare the global variables here.
WM_DEVICECHANGE = 537 ' 0x0219
TimeChanged = Now
End Sub
Sub App_Start
Form1.Show
WMevent.New1("Form1", false)
WMevent.Hook(WM_DEVICECHANGE)
End Sub
Sub Form1_Close
WMevent.UnHook(WM_DEVICECHANGE)
End Sub
Sub WMEvent_WmEvent
msg = WMevent.wParam
If msg = 7 AND Now > TimeChanged + cTicksPerSecond*3 Then
TimeChanged = Now
Msgbox("Device added or removed")
End If
End Sub