Run a script when usb device is connected
Run a script when usb device is connected
Is that possible
I want to automatically run a script when a specific usb device is connected
I want to automatically run a script when a specific usb device is connected
Re: Run a script when usb device is connected
See this thread and others similar to it found by googling "autohotkey detect usb".
Re: Run a script when usb device is connected
EDIT : shorter only with onmessage , starts charmap depending serialnumber from USB-Drive
also an example , when GUI close it minimize , connect USB then maximize
modify/add > should check serialnumber
Code: Select all
#persistent
OnMessage(0x219, "notify_change")
notify_change(wParam, lParam, msg, hwnd)
{
settimer,checkdrive,2000
}
return
checkdrive:
i=0
alldrives:=""
DriveGet, A, List,
StringSplit,D, A,
loop,%d0%
{
DRV:= % d%a_index% . ":\"
i++
DriveGet , type1 , type , %DRV% ;- removable / fixed
DriveGet , cap1 , capacity , %DRV%
DrivespaceFree , free1 , %DRV%
DriveGet , fs1 , fs , %DRV% ;- FAT
DriveGet , label1 , label , %DRV% ;- volume label >\\server1
DriveGet , serial1 , serial , %DRV% ;- volume serial number
DriveGet , status1 , status , %DRV%
alldrives .= drv . " " . type1 . " " . free1 . "-MB`n"
}
VT:=D0 ;- totaldrives
last = %drv%
DriveGet , serial2 , serial , %last% ;- volume serial number
if (serial2="3772709444")
{
settimer,checkdrive,off
try
run,charmap
}
else
{
settimer,checkdrive,off
run,::{20d04fe0-3aea-1069-a2d8-08002b30309d}
msgbox, 262208,Info ,Alldrives=`n%alldrives%`n
}
return
esc::exitapp
;================================================================================
modify/add > should check serialnumber
Code: Select all
;see also DriveGetOb() from user BoBo
;https://www.autohotkey.com/boards/viewtopic.php?f=10&t=80688
;https://www.autohotkey.com/boards/viewtopic.php?f=76&t=80592
#Warn
#NoEnv
DetectHiddenWindows, On
Setworkingdir,%a_scriptdir%
setformat,float,0.2
SendMode Input
SetBatchLines -1
SetWinDelay, 0
filename1:="Active Drives"
Gui,1: -dpiscale
;Gui, -MaximizeBox -MinimizeBox +AlwaysOnTop
Gui,+AlwaysOnTop
Gui,font,s10,Tahoma
Gui,Margin,0,0
Gui, Add, ListView,x10 y10 w1730 h200 Grid vLV1 , Drive|STATUS|TYPE|FORMAT|Capacity-MB|FREE-MB|Capacity-GB|FREE-GB|Procent|SerialNR|LABEL|StatusCD
LV_ModifyCol(1,100),LV_ModifyCol(2,"100 Center"),LV_ModifyCol(3,"150 Center"),LV_ModifyCol(4,"100 Center"),LV_ModifyCol(5,"150 Right"),LV_ModifyCol(6,"150 Right"),LV_ModifyCol(7,"100 Right"),LV_ModifyCol(8,"100 Right"),LV_ModifyCol(9,"150 Center"),LV_ModifyCol(10,"150 Right"),LV_ModifyCol(11,"250 Center"),LV_ModifyCol(12,"180 Center")
Gui,Show,w1750 h250 ,%filename1%
gosub,drivegetshow
OnMessage(0x219, "notify_change")
Return
;---------------
GuiClose:
Gui,1:minimize
return
#IfWinExist Active Drives ahk_class AutoHotkeyGUI ahk_exe AutoHotkey.exe
ESC::ExitApp
#IfWinExist
;---------------
notify_change(wParam, lParam, msg, hwnd)
{
Gui,Show,w1750,Active Drives
gosub,drivegetshow
}
;---------------
drivegetshow:
LV_Delete()
DriveGet, A, List,
GuiControl,1: -Redraw,LV1
loop,parse,A
{
type1=
cap1=
free1=
fs1=
label1=
serial1=
status1=
statusCD=
drv=
x:=a_loopfield
drv=%x%:\
DriveGet , type1 , type , %DRV% ;- removable / fixed
DriveGet , cap1 , capacity , %DRV%
DrivespaceFree , free1 , %DRV%
DriveGet , fs1 , fs , %DRV% ;- FAT
DriveGet , label1 , label , %DRV% ;- volume label >\\server1
DriveGet , serial1 , serial , %DRV% ;- volume serial number
DriveGet , status1 , status , %DRV%
DriveGet , statusCD, StatusCD , %DRV% ;- CDRom CDFS Audio CD open / stopped
prc :=(free1*100)/cap1
cap1a :=StrReplace(cap1 , Substr(cap1 , -2), "'" Substr(cap1 , -2))
free1a:=StrReplace(free1, Substr(free1, -2), "'" Substr(free1, -2))
cap2 :=Floor(cap1 /1024)
free2 :=Floor(free1/1024)
if (type1="CDROM")
LV_Add("", drv,status1,type1,fs1,,,,,,serial1,label1,statuscd)
else
LV_Add("", drv,status1,type1,fs1,cap1a,free1a,cap2,free2,prc . " %",serial1,label1,statuscd)
}
GuiControl,1: +Redraw,LV1
return
;============================================
Last edited by garry on 16 Jan 2022, 13:21, edited 2 times in total.
Re: Run a script when usb device is connected
Sorry I guess I should have mentioned that the usb device is not a storage drive,it's usb headphones
Re: Run a script when usb device is connected
just added an else after if , maybe see a message and your existing drives with 'OnMessage(0x219, "notify_change")' , when connect an USB device
Code: Select all
;....
else
{
settimer,checkdrive,off
run,::{20d04fe0-3aea-1069-a2d8-08002b30309d}
msgbox, 262208,Info ,Alldrives=`n%alldrives%`n
}
Re: Run a script when usb device is connected
Thanks,it seems I only needed the onmessage code 0x219
Is there is a smarter way of doing this
I get an error if I put the code inside the notify_change function
Is there is a smarter way of doing this
Code: Select all
OnMessage(0x219, "notify_change")
notify_change(wParam, lParam, msg, hwnd)
{
SetTimer,DV,-1000
Return
}
DV:
for device in ComObjGet("winmgmts:").ExecQuery("Select * from Win32_PnPEntity")
{
If InStr(device.name,"USB Heaphones")
{
Msgbox
break
}
}
Return
- flyingDman
- Posts: 2817
- Joined: 29 Sep 2013, 19:01
Re: Run a script when usb device is connected
Why would you settimer to -1000 ? I just used this code (without the "-") to check if my bluetooth headset is recognized. It works well.
14.3 & 1.3.7
Re: Run a script when usb device is connected
thank you all for help
I tried this
I tried this
Code: Select all
#warn
global aa
searchfor:="C-Media USB Headphone Set"
OnMessage(0x219, "notify_change")
notify_change(wParam, lParam, msg, hwnd)
{
SetTimer,DV,1000
}
return
;-----------------------------------
DV:
sleep,1000
i=0
e:=""
for device in ComObjGet("winmgmts:").ExecQuery("Select * from Win32_PnPEntity")
{
If InStr(device.name,searchfor)
{
settimer,DV,off
i++
aa:=device.name
e .= i . "--" . aa . "`r`n"
}
}
settimer,DV,off
aa:=""
if e<>
msgbox, 262208,DEVICES ,%e%
e:=""
Return
;-----------
esc::exitapp
;==========================================
Re: Run a script when usb device is connected
Because I don't want the function to constantly get all of the devices every second and also when I enable my bluetooth,the 0x219 is executed twice so with that code it will reset the SetTimer countdown before attempting to get the devices so the scan will be done only onceflyingDman wrote: ↑16 Jan 2022, 17:56Why would you settimer to -1000 ? I just used this code (without the "-") to check if my bluetooth headset is recognized. It works well.
And also constantly getting a list of devices,makes the 0x219 trigger pointless