-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathWM_DEVICECHANGE.ahk
52 lines (42 loc) · 1.56 KB
/
WM_DEVICECHANGE.ahk
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
; =============================================================================================================================================================
; Author ........: jNizM
; Released ......: 2017-03-03
; Modified ......: 2023-01-13
; Tested with....: AutoHotkey v2.0.2 (x64)
; Tested on .....: Windows 11 - 22H2 (x64)
; Function ......: WM_DEVICECHANGE
;
; Parameter(s)...:
;
; Return ........: Notifies an application of a change to the hardware configuration of a device or the computer.
; =============================================================================================================================================================
#Requires AutoHotkey v2.0
OnMessage 0x0219, WM_DEVICECHANGE
Persistent
WM_DEVICECHANGE(wParam, lParam, *)
{
static DBT_DEVICEARRIVAL := 0x8000
static DBT_DEVICEREMOVECOMPLETE := 0x8004
static DBT_DEVTYP_VOLUME := 0x00000002
if (wParam = DBT_DEVICEARRIVAL) || (wParam = DBT_DEVICEREMOVECOMPLETE)
{
if (NumGet(lParam, 4, "UInt") = DBT_DEVTYP_VOLUME)
{
Device := FirstDriveFromMask(NumGet(lParam, 12, "UInt"))
DeviceChangeInfo(Device, wParam)
}
}
}
FirstDriveFromMask(unitmask)
{
i := 0
while (unitmask > 1) && (++i < 0x1A)
unitmask >>= 1
return 0x41 + i
}
DeviceChangeInfo(Device, ChangeInfo)
{
TrayTip Chr(Device) ":\ " ((ChangeInfo = 0x8000) ? "plugged in" : "is removed"), "DeviceChange"
SetTimer () => TrayTip(), -3000
}
; =============================================================================================================================================================