Search the Community
Showing results for tags 'sendex'.
-
As many are aware, the standard AutoIt Send() function can leave modifier keys (usually Shift, Alt, Ctrl) in a pressed-like state. While there are at least a few alternatives that can be found on this forum, as well as one in the Wiki (which i can't find at the moment), they have all failed me at some point, leaving a modifier key "stuck". So far i have had no issues with this function however. LAST UPDATED: 21-SEP-2014 #include-once ; #FUNCTION# ==================================================================================================================== ; Name ..........: _SendEx ; Description ...: alternative to built-in Send() function which prevents modifier keys from being left in a pressed state ; Syntax ........: _SendEx($sSendKeys[, $iTimeout[, $sReleaseKeys[, $hUser32Dll]]]) ; Parameters ....: $sSendKeys - string of keys to send in Send() format ; $hUser32Dll - [optional] handle to user32.dll ; $iTimeout - [optional] maximum time in ms to attempt to release pressed keys (minimum = 250) ; $sReleaseKeys - [optional] comma seperated string of keys to release before issuing Send() (no spaces). ; defaults to: Shift, Ctrl, Alt, L Win, R Win ; Return values .: Success returns 1, else sets @error to 1 and returns an error message string ; Release date ..: 25-May-2014 ; Modify date ...: 21-Sep-2014 ; Author ........: iCode ; Modified by ...: ; Remarks .......: ; Related .......: Send() ; Link ..........: ; Example .......: No ; =============================================================================================================================== Func _SendEx($sSendKeys, $hUser32Dll = "", $iTimeout = 2000, $sReleaseKeys = "0x10,0x11,0x12,0x5B,0x5C") Local $bCloseDll, $sRet Local $iDelay = Opt("SendKeyDelay") + Opt("SendKeyDownDelay") If $iDelay < 50 Then $iDelay = 50 Local $aReleaseKeys = StringSplit($sReleaseKeys, ",") If @error Then Return SetError(1, 0, "Failed to create release key array") If $hUser32Dll <> "user32.dll" Then $hUser32Dll = DllOpen("user32.dll") If @error Then Return SetError(1, 0, "Failed to open handle to user32.dll") $bCloseDll = True EndIf $sRet = __ReleaseKeys($aReleaseKeys, $iTimeout, $hUser32Dll) If Not @error Then Send($sSendKeys) Sleep($iDelay) $sRet = __ReleaseKeys($aReleaseKeys, $iTimeout, $hUser32Dll) EndIf If $bCloseDll Then DllClose("user32.dll") If $sRet Then Return SetError(1, 0, $sRet) Return 1 EndFunc ; #INTERNAL_USE_ONLY# =========================================================================================================== ; Name ..........: __ReleaseKeys ; Description ...: release pressed keys ; Syntax ........: __ReleaseKeys(Byref $aReleaseKeys, $iTimeout, $hUser32Dll) ; Parameters ....: $aReleaseKeys - [in/out] An array of unknowns. ; $iTimeout - An integer value. ; $hUser32Dll - A handle value. ; Return values .: None ; Author ........: iCode ; Modified ......: 25-May-2014 ; Remarks .......: ; Related .......: ; Link ..........: ; Example .......: No ; =============================================================================================================================== Func __ReleaseKeys(ByRef $aReleaseKeys, $iTimeout, $hUser32Dll) If $iTimeout < 50 Then $iTimeout = 250 Local $aRet, $hTimer = TimerInit() For $i = 0 To UBound($aReleaseKeys) - 1 $aRet = DllCall($hUser32Dll, "short", "GetAsyncKeyState", "int", $aReleaseKeys[$i]) If @error Then Return SetError(1, 0, "Dll call GetAsyncKeyState failed with key: " & $aReleaseKeys[$i]) If BitAND($aRet[0], 0x8000) <> 0 Then Do Sleep(100) DllCall($hUser32Dll, "int", "keybd_event", "int", $aReleaseKeys[$i], "int", 0, "long", 2, "long", 0) If @error Then Return SetError(1, 0, "Dll call keybd_event failed with key: " & $aReleaseKeys[$i]) $aRet = DllCall($hUser32Dll, "short", "GetAsyncKeyState", "int", $aReleaseKeys[$i]) If TimerDiff($hTimer) >= $iTimeout Then Return SetError(1, 0, "Time out limit reached") Until BitAND($aRet[0], 0x8000) = 0 EndIf Next EndFunc Change log... 29-JUN-2014 - changed the order of the parameters for _SendEx, moving $sReleaseKeys to the last position 21-SEP-2014 - in the interest of efficiency, i removed the StringStripWS() function - $sReleaseKeys can no longer have spaces in the string