Run a script when usb device is connected

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
vsub
Posts: 541
Joined: 29 Sep 2015, 03:39

Run a script when usb device is connected

Post by vsub » 16 Jan 2022, 07:41

Is that possible
I want to automatically run a script when a specific usb device is connected

User avatar
boiler
Posts: 16772
Joined: 21 Dec 2014, 02:44

Re: Run a script when usb device is connected

Post by boiler » 16 Jan 2022, 07:52

See this thread and others similar to it found by googling "autohotkey detect usb".

garry
Posts: 3740
Joined: 22 Dec 2013, 12:50

Re: Run a script when usb device is connected

Post by garry » 16 Jan 2022, 08:51

EDIT : shorter only with onmessage , starts charmap depending serialnumber from USB-Drive

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

;================================================================================
also an example , when GUI close it minimize , connect USB then maximize
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.

vsub
Posts: 541
Joined: 29 Sep 2015, 03:39

Re: Run a script when usb device is connected

Post by vsub » 16 Jan 2022, 13:02

Sorry I guess I should have mentioned that the usb device is not a storage drive,it's usb headphones

garry
Posts: 3740
Joined: 22 Dec 2013, 12:50

Re: Run a script when usb device is connected

Post by garry » 16 Jan 2022, 15:01

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
 }

vsub
Posts: 541
Joined: 29 Sep 2015, 03:39

Re: Run a script when usb device is connected

Post by vsub » 16 Jan 2022, 17:23

Thanks,it seems I only needed the onmessage code 0x219

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
I get an error if I put the code inside the notify_change function

User avatar
flyingDman
Posts: 2791
Joined: 29 Sep 2013, 19:01

Re: Run a script when usb device is connected

Post by flyingDman » 16 Jan 2022, 17:56

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

garry
Posts: 3740
Joined: 22 Dec 2013, 12:50

Re: Run a script when usb device is connected

Post by garry » 17 Jan 2022, 05:02

thank you all for help
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
;==========================================

vsub
Posts: 541
Joined: 29 Sep 2015, 03:39

Re: Run a script when usb device is connected

Post by vsub » 17 Jan 2022, 10:13

flyingDman wrote:
16 Jan 2022, 17:56
Why would you settimer to -1000 ? I just used this code (without the "-") to check if my bluetooth headset is recognized. It works well.
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 once

And also constantly getting a list of devices,makes the 0x219 trigger pointless

Post Reply

Return to “Ask for Help (v1)”