ilstarno Posted November 3, 2015 Share Posted November 3, 2015 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 More sharing options...
Bert Posted November 3, 2015 Share Posted November 3, 2015 Is there any reason this can't all be done in AutoIt? If you can do it in AHK, it can be done in AutoIt. My thinking is instead of killing a process, just have the process know when the USB drive is inserted and act accordingly. The Vollatran project My blog: http://www.vollysinterestingshit.com/ Link to comment Share on other sites More sharing options...
JohnOne Posted November 3, 2015 Share Posted November 3, 2015 That is what the OP is asking. AutoIt Absolute Beginners Require a serial Pause Script Video Tutorials by Morthawt ipify Monkey's are, like, natures humans. Link to comment Share on other sites More sharing options...
ilstarno Posted November 3, 2015 Author Share Posted November 3, 2015 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? Link to comment Share on other sites More sharing options...
JohnOne Posted November 3, 2015 Share Posted November 3, 2015 I think the only way you map keys with AutoIt is With HotkeySet.Posting your AHK code with help with a better answer. AutoIt Absolute Beginners Require a serial Pause Script Video Tutorials by Morthawt ipify Monkey's are, like, natures humans. Link to comment Share on other sites More sharing options...
ViciousXUSMC Posted November 3, 2015 Share Posted November 3, 2015 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. Link to comment Share on other sites More sharing options...
ilstarno Posted November 3, 2015 Author Share Posted November 3, 2015 (edited) the code i use for now is this one.expandcollapse popup;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 November 3, 2015 by ilstarno Link to comment Share on other sites More sharing options...
ilstarno Posted November 3, 2015 Author Share Posted November 3, 2015 (edited) and the ahk key isf1::space f2::space alt:space f4::space Edited November 3, 2015 by ilstarno Link to comment Share on other sites More sharing options...
ilstarno Posted November 3, 2015 Author Share Posted November 3, 2015 i wanna be able to do that ahk stuff inside autoit without launching that ahk exe on usb removal and on usb insertion Link to comment Share on other sites More sharing options...
ilstarno Posted November 3, 2015 Author Share Posted November 3, 2015 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 More sharing options...
ViciousXUSMC Posted November 3, 2015 Share Posted November 3, 2015 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. ilstarno 1 Link to comment Share on other sites More sharing options...
ilstarno Posted November 3, 2015 Author Share Posted November 3, 2015 (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 November 3, 2015 by ilstarno Link to comment Share on other sites More sharing options...
ViciousXUSMC Posted November 3, 2015 Share Posted November 3, 2015 (edited) If $USBPlugged HotKeySet("Key", "Function") Else HotKeySet("Key") EndIfHowever you script it, the idea is just call HotKeySet again without giving a function. Edited November 3, 2015 by ViciousXUSMC ilstarno 1 Link to comment Share on other sites More sharing options...
ilstarno Posted November 3, 2015 Author Share Posted November 3, 2015 can this be used?HotKeySet("{F1}", "{SPACE}") Link to comment Share on other sites More sharing options...
JohnOne Posted November 3, 2015 Share Posted November 3, 2015 (edited) No, because a function cannot be called {SPACE}, "{" is an illegal character.Re-read the help file for HotkeySet. Edited November 3, 2015 by JohnOne ilstarno 1 AutoIt Absolute Beginners Require a serial Pause Script Video Tutorials by Morthawt ipify Monkey's are, like, natures humans. Link to comment Share on other sites More sharing options...
ilstarno Posted November 3, 2015 Author Share Posted November 3, 2015 (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 November 3, 2015 by ilstarno Link to comment Share on other sites More sharing options...
ilstarno Posted November 3, 2015 Author Share Posted November 3, 2015 (edited) #include <Misc.au3> HotKeySet("{F1}", "_s") While 1 Sleep(10) WEnd Func _s() If _IsPressed('F1')=1 Then Send("{SPACE}") EndFuncI HOPE THIS HELPS, ANY CORRECTION? Edited November 3, 2015 by ilstarno Link to comment Share on other sites More sharing options...
GordonFreeman Posted November 3, 2015 Share Posted November 3, 2015 (edited) HotKeySet("{F1}","_SPACE") While 1 Sleep(30) WEnd Func _SPACE() Send("{SPACE}") EndFunc; _SPACEHotKeySet 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 somethingAnd also, _isPressed need include Misc, hotkeyset not need any include Edited November 3, 2015 by GordonFreeman ilstarno 1 Frabjous Installation Link to comment Share on other sites More sharing options...
ilstarno Posted November 3, 2015 Author Share Posted November 3, 2015 (edited) Thanks Gordon, it works. now i want on usb plug to stop the HotKeySetexpandcollapse popup;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 November 3, 2015 by ilstarno Link to comment Share on other sites More sharing options...
GordonFreeman Posted November 4, 2015 Share Posted November 4, 2015 (edited) ;Something like?: HotKeySet("{F1}")Calling without any function you "unset" the hotkey Edited November 4, 2015 by GordonFreeman ilstarno 1 Frabjous Installation Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now