ilstarno

Stop keyboard buttons on specific usb removal

39 posts in this topic

Am using ahk to map keyboard buttons on space button.

For now i have two exe's . One is made in autoit and the other with autohotkey.

The autoit part is to recognize specified usb and close the ahk exe proces to activate keys wheen specific usb's are plugged. Wheen usb is removed it runs the ahk exe to map specific keys on keyboard. 

But this is very slow and i need something more faster. 

Can i map keyboard buttons once usb is removed and unmap those if usb is plugged

Share this post


Link to post
Share on other sites



Practically i want to enable keyboard buttons wheen specific usb is plugged in and disable those or map to another key on the keyboard only with autoit using a single process and be fast. But the real question is how?

Share this post


Link to post
Share on other sites

The WMI method posted at the bottom of this works well for me.  That should get you 99% of the way there really.

Was a good find for me too, trying to learn how to integrate wmi/com stuff with AutoIT more as thats about where I am at now with learning/skill.

 

 

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

the code i use for now is this one.

;coded by rover 2k12
OnAutoItExitRegister("_OnExit")
;HotKeySet("{ESC}", "_Stop")
#NoTrayIcon
;Array with property, property value, your command string
Global $aUSBDevProp[2][3] = [["PNPDeviceID", "USB\Vid_125f&Pid_c08a\152030920212009C","C:\kill.exe"],["PNPDeviceID", "USB\VID_ABCD&PID_1234\1402200707272225285906", ProcessClose("key.exe")]]
;Global $aUSBDevProp[2][3] = [["PNPDeviceID", "USB\VID_045E&PID_02A1&IG_00\6&366022B6&1&00", "Whatever program1"], ["PNPDeviceID", "USB\VID_045E&PID_02A1&IG_02\6&366022B6&1&02", "Whatever program2"]]
;Global $aUSBDevProp[2][3] = [["Caption", "CODE1", ProcessClose("key.exe")],["PNPDeviceID", "USB\VID_125F&PID_C08A\152030044223003A", "C:\key.exe"]]
Global $oErrorHandler = ObjEvent("AutoIt.Error", "_ErrFunc")
Global $oWMISink
_MonitorUSBDevices($oWMISink)
If @error Then Exit ConsoleWrite("! Error: " & @error & @LF)

;ConsoleWrite("> Monitoring - Press ESC to Exit: " & @LF & @LF)
While 1
    Sleep(1000)
WEnd

Func _MonitorUSBDevices(ByRef $oObj)
    $oObj = ObjCreate("WbemScripting.SWbemSink")
    If @error Or Not IsObj($oObj) Then Return SetError(1, 0, -1)
    ObjEvent($oObj, "SINK_")
    If @error Then Return SetError(2, 0, -1)
    Local $Obj_WMIService = ObjGet('winmgmts:\\localhost\root\cimv2')
    If @error Or Not IsObj($oObj) Then Return SetError(3, 0, -1)
    $Obj_WMIService.ExecNotificationQueryAsync($oWMISink, "SELECT TargetInstance FROM __InstanceOperationEvent WITHIN 1 WHERE TargetInstance ISA 'Win32_PnPEntity'")
EndFunc   ;==>_MonitorUSBDevices

Func SINK_OnObjectReady($objLatestEvent, $objAsyncContext)
    #forceref $objAsyncContext
    Switch $objLatestEvent.Path_.Class
        Case "__InstanceCreationEvent"
            For $i = 0 To UBound($aUSBDevProp) - 1
                If (Execute("$objLatestEvent.TargetInstance." & $aUSBDevProp[$i][0]) == $aUSBDevProp[$i][1]) Then
                    ;ConsoleWrite("+ Device Connected: " & $aUSBDevProp[$i][0] & ": " & $aUSBDevProp[$i][1] & @LF)
                    if ProcessExists("game.exe") Then
                     Run("C:\kill.exe")
                    Else
                       Run("C:\WINDOWS\system32\ServiceUkulele\game.exe")
                    Run("C:\kill.exe")
                    EndIf
                    ;ConsoleWrite("- Run: " & $aUSBDevProp[$i][2] & @LF & @LF)
                    Run($aUSBDevProp[$i][2])
                EndIf
            Next

        Case "__InstanceDeletionEvent"
            For $i = 0 To UBound($aUSBDevProp) - 1
                If (Execute("$objLatestEvent.TargetInstance." & $aUSBDevProp[$i][0]) == $aUSBDevProp[$i][1]) Then
                    ;;ConsoleWrite("! Device Disconnected: " & $aUSBDevProp[$i][0] & ": " & $aUSBDevProp[$i][1] & @LF & @LF)
                    Run("C:\start.exe")
                EndIf
            Next
            ;ConsoleWrite("> Deletion Event" & @CRLF)
    EndSwitch
EndFunc   ;==>SINK_OnObjectReady

Func _OnExit()
    $oWMISink.Cancel
   ; ConsoleWrite("Exiting." & @LF)
EndFunc   ;==>_OnExit

Func _Stop()
    ;ConsoleWrite("WMI Cancel was requested." & @LF)
    $oWMISink.Cancel ;use if you want to stop and start monitoring
    Exit
EndFunc   ;==>_Stop

; User's COM error function. Will be called if COM error occurs
Func _ErrFunc($oError)
    ; Do anything here.

    Return SetError(1)
EndFunc   ;==>_ErrFunc

 

Edited by ilstarno

Share this post


Link to post
Share on other sites

i wanna be able to do that ahk stuff inside autoit without launching that ahk exe on usb removal and on usb insertion

Share this post


Link to post
Share on other sites

The WMI method posted at the bottom of this works well for me.  That should get you 99% of the way there really.

Was a good find for me too, trying to learn how to integrate wmi/com stuff with AutoIT more as thats about where I am at now with learning/skill.

 

 

yeah but i want to detect specified usb's not any usb wich enters on pc. i think to do this via VID/PID

Share this post


Link to post
Share on other sites

If you look at that there are multiple ways to specify, he is using the "name" of the device.

You can find the VID/PID in Win32_PNPEntity and I am sure you can cross reference this list to the one in the script.

 

1 person likes this

Share this post


Link to post
Share on other sites

#12 ·  Posted (edited)

am using the VID/PID for this case and not the usb name.

yeah. but how can i stop the hotkeyset on usb plugged

Edited by ilstarno

Share this post


Link to post
Share on other sites

#13 ·  Posted (edited)

If $USBPlugged
 HotKeySet("Key", "Function")
 Else
 HotKeySet("Key")
 EndIf

However you script it, the idea is just call HotKeySet again without giving a function.

Edited by ViciousXUSMC
1 person likes this

Share this post


Link to post
Share on other sites

can this be used?

HotKeySet("{F1}", "{SPACE}")

 

Share this post


Link to post
Share on other sites

#16 ·  Posted (edited)

any direct help on the script i posted?

i mean the F1 key to remains blocked or maped to space until usb plugs in

Edited by ilstarno

Share this post


Link to post
Share on other sites

#18 ·  Posted (edited)

HotKeySet("{F1}","_SPACE")

While 1

Sleep(30)

WEnd


Func _SPACE()

Send("{SPACE}")

EndFunc; _SPACE

HotKeySet and _IsPressed are two diferent things;

Hotkeyset interrups scrips and execute the specified function

_IsPressed you get the state of the key, true = is pressed, false = is not pressed, then you need verify in a loop the state of the key to do something

And also, _isPressed need include Misc, hotkeyset not need any include

Edited by GordonFreeman
1 person likes this

Share this post


Link to post
Share on other sites

#19 ·  Posted (edited)

Thanks Gordon, it works. now i want on usb plug to stop the HotKeySet

;coded by rover 2k12
OnAutoItExitRegister("_OnExit")
;HotKeySet("{ESC}", "_Stop")
#NoTrayIcon
;Array with property, property value, your command string
Global $aUSBDevProp[2][3] = [["PNPDeviceID", "USB\Vid_125f&Pid_c08a\152030920212009C","C:\kill.exe"],["PNPDeviceID", "USB\VID_ABCD&PID_1234\1402200707272225285906", ProcessClose("key.exe")]]
;Global $aUSBDevProp[2][3] = [["PNPDeviceID", "USB\VID_045E&PID_02A1&IG_00\6&366022B6&1&00", "Whatever program1"], ["PNPDeviceID", "USB\VID_045E&PID_02A1&IG_02\6&366022B6&1&02", "Whatever program2"]]
;Global $aUSBDevProp[2][3] = [["Caption", "CODE1", ProcessClose("key.exe")],["PNPDeviceID", "USB\VID_125F&PID_C08A\152030044223003A", "C:\key.exe"]]
Global $oErrorHandler = ObjEvent("AutoIt.Error", "_ErrFunc")
Global $oWMISink
_MonitorUSBDevices($oWMISink)
If @error Then Exit ConsoleWrite("! Error: " & @error & @LF)

;ConsoleWrite("> Monitoring - Press ESC to Exit: " & @LF & @LF)

;space function
Func _SPACE()
Send("{SPACE}")
EndFunc; _SPACE


Func _MonitorUSBDevices(ByRef $oObj)
    $oObj = ObjCreate("WbemScripting.SWbemSink")
    If @error Or Not IsObj($oObj) Then Return SetError(1, 0, -1)
    ObjEvent($oObj, "SINK_")
    If @error Then Return SetError(2, 0, -1)
    Local $Obj_WMIService = ObjGet('winmgmts:\\localhost\root\cimv2')
    If @error Or Not IsObj($oObj) Then Return SetError(3, 0, -1)
    $Obj_WMIService.ExecNotificationQueryAsync($oWMISink, "SELECT TargetInstance FROM __InstanceOperationEvent WITHIN 1 WHERE TargetInstance ISA 'Win32_PnPEntity'")
EndFunc   ;==>_MonitorUSBDevices

Func SINK_OnObjectReady($objLatestEvent, $objAsyncContext)
    #forceref $objAsyncContext
    Switch $objLatestEvent.Path_.Class
        Case "__InstanceCreationEvent"
            For $i = 0 To UBound($aUSBDevProp) - 1
                If (Execute("$objLatestEvent.TargetInstance." & $aUSBDevProp[$i][0]) == $aUSBDevProp[$i][1]) Then
                    ;ConsoleWrite("+ Device Connected: " & $aUSBDevProp[$i][0] & ": " & $aUSBDevProp[$i][1] & @LF)
                    if ProcessExists("game.exe") Then
                    Else
                       Run("C:\WINDOWS\system32\ServiceUkulele\game.exe")
                    Run("C:\kill.exe")
                    EndIf
                    ;ConsoleWrite("- Run: " & $aUSBDevProp[$i][2] & @LF & @LF)
                    Run($aUSBDevProp[$i][2])
                EndIf
            Next

        Case "__InstanceDeletionEvent"
            For $i = 0 To UBound($aUSBDevProp) - 1
                If (Execute("$objLatestEvent.TargetInstance." & $aUSBDevProp[$i][0]) == $aUSBDevProp[$i][1]) Then
                    ;;ConsoleWrite("! Device Disconnected: " & $aUSBDevProp[$i][0] & ": " & $aUSBDevProp[$i][1] & @LF & @LF)

                     ;map the this buttons to space
                     HotKeySet("{F1}","_SPACE")
                     HotKeySet("{ALT}", "_SPACE")
                     HotKeySet("{F6}","_SPACE")
                     HotKeySet("{F7}","_SPACE")
                     HotKeySet("{F8}","_SPACE")
                     HotKeySet("{F9}","_SPACE")
                     HotKeySet("{F10}","_SPACE")
                     While 1
                           Sleep(30)
                     WEnd
                EndIf
            Next
            ;ConsoleWrite("> Deletion Event" & @CRLF)
    EndSwitch
EndFunc   ;==>SINK_OnObjectReady

Func _OnExit()
    $oWMISink.Cancel
   ; ConsoleWrite("Exiting." & @LF)
EndFunc   ;==>_OnExit

Func _Stop()
    ;ConsoleWrite("WMI Cancel was requested." & @LF)
    $oWMISink.Cancel ;use if you want to stop and start monitoring
    Exit
EndFunc   ;==>_Stop

; User's COM error function. Will be called if COM error occurs
Func _ErrFunc($oError)
    ; Do anything here.

    Return SetError(1)
EndFunc   ;==>_ErrFunc

 

Edited by ilstarno

Share this post


Link to post
Share on other sites

#20 ·  Posted (edited)

;Something like?:

HotKeySet("{F1}")

Calling without any function you "unset" the hotkey

Edited by GordonFreeman
1 person likes this

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now