Jump to content

Stop keyboard buttons on specific usb removal


 Share

Recommended Posts

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

Link to comment
Share on other sites

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
Link to comment
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

Link to comment
Share on other sites

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
Link to comment
Share on other sites

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
Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...