Jump to content
Sign in to follow this  
iCode

SendEx - yet another alternative to the built-in Send function

Recommended Posts

iCode

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

Edited by iCode
  • Like 3

FUNCTIONS: WinDock (dock window to screen edge) | EditCtrl_ToggleLineWrap (line/word wrap for AU3 edit control) | SendEX (yet another alternative to Send( ) ) | Spell Checker (Hunspell wrapper) | SentenceCase (capitalize first letter of sentences)

CODE SNIPPITS: Dynamic tab width (set tab control width according to window width)

Share this post


Link to post
Share on other sites
Srex

Would you mind throwing a simple example?
This could be usefull :)

_SendEx("{A}")

Would just send the key a?

Edited by Srex

Share this post


Link to post
Share on other sites
guinness

It's quite clear it's an extension of Send(). I therefore see no reason to have an example if people are familiar with Send(). If not then they can look in the help file.


UDF List:

 
_AdapterConnections()_AlwaysRun()_AppMon()_AppMonEx()_ArrayFilter/_ArrayReduce_BinaryBin()_CheckMsgBox()_CmdLineRaw()_ContextMenu()_ConvertLHWebColor()/_ConvertSHWebColor()_DesktopDimensions()_DisplayPassword()_DotNet_Load()/_DotNet_Unload()_Fibonacci()_FileCompare()_FileCompareContents()_FileNameByHandle()_FilePrefix/SRE()_FindInFile()_GetBackgroundColor()/_SetBackgroundColor()_GetConrolID()_GetCtrlClass()_GetDirectoryFormat()_GetDriveMediaType()_GetFilename()/_GetFilenameExt()_GetHardwareID()_GetIP()_GetIP_Country()_GetOSLanguage()_GetSavedSource()_GetStringSize()_GetSystemPaths()_GetURLImage()_GIFImage()_GoogleWeather()_GUICtrlCreateGroup()_GUICtrlListBox_CreateArray()_GUICtrlListView_CreateArray()_GUICtrlListView_SaveCSV()_GUICtrlListView_SaveHTML()_GUICtrlListView_SaveTxt()_GUICtrlListView_SaveXML()_GUICtrlMenu_Recent()_GUICtrlMenu_SetItemImage()_GUICtrlTreeView_CreateArray()_GUIDisable()_GUIImageList_SetIconFromHandle()_GUIRegisterMsg()_GUISetIcon()_Icon_Clear()/_Icon_Set()_IdleTime()_InetGet()_InetGetGUI()_InetGetProgress()_IPDetails()_IsFileOlder()_IsGUID()_IsHex()_IsPalindrome()_IsRegKey()_IsStringRegExp()_IsSystemDrive()_IsUPX()_IsValidType()_IsWebColor()_Language()_Log()_MicrosoftInternetConnectivity()_MSDNDataType()_PathFull/GetRelative/Split()_PathSplitEx()_PrintFromArray()_ProgressSetMarquee()_ReDim()_RockPaperScissors()/_RockPaperScissorsLizardSpock()_ScrollingCredits_SelfDelete()_SelfRename()_SelfUpdate()_SendTo()_ShellAll()_ShellFile()_ShellFolder()_SingletonHWID()_SingletonPID()_Startup()_StringCompact()_StringIsValid()_StringRegExpMetaCharacters()_StringReplaceWholeWord()_StringStripChars()_Temperature()_TrialPeriod()_UKToUSDate()/_USToUKDate()_WinAPI_Create_CTL_CODE()_WinAPI_CreateGUID()_WMIDateStringToDate()/_DateToWMIDateString()Au3 script parsingAutoIt SearchAutoIt3 PortableAutoIt3WrapperToPragmaAutoItWinGetTitle()/AutoItWinSetTitle()CodingDirToHTML5FileInstallrFileReadLastChars()GeoIP databaseGUI - Only Close ButtonGUI ExamplesGUICtrlDeleteImage()GUICtrlGetBkColor()GUICtrlGetStyle()GUIEventsGUIGetBkColor()Int_Parse() & Int_TryParse()IsISBN()LockFile()Mapping CtrlIDsOOP in AutoItParseHeadersToSciTE()PasswordValidPasteBinPosts Per DayPreExpandProtect GlobalsQueue()Resource UpdateResourcesExSciTE JumpSettings INISHELLHOOKShunting-YardSignature CreatorStack()Stopwatch()StringAddLF()/StringStripLF()StringEOLToCRLF()VSCROLLWM_COPYDATAMore Examples...

Updated: 22/04/2018

Share this post


Link to post
Share on other sites
Srex

Thanks for the 2 yeses :D
It works very well must say great work :)!

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
Sign in to follow this  

  • Similar Content

    • ternal
      By ternal
      Hi,
      The FAQ proposed as a solution to keys getting stuck  to do this command
      ControlSend("", "", "", "text", 0) However this sends "text" to the last active control if I am not mistaken.I have tried this with
      controlsend("", "", "", "{altup}{shiftup}{ctrlup}", 0) This is already a lot better but still fails 1/10
      I cannot allow the script to send "text" to the last active control, and its a bit silly to let it send to somewhere where its harmless no?
      Are there any other methods of unstucking keys?
    • Fortitude
      By Fortitude
      Hello!
      I have made a program, that opens a folder, and then store the path ("C:\programFiles\etc") in a variable.
      Then i need to open the command panel as administrator, and write  in the stored path, and some other text.

      Can i somehow instantly copypaste the text i needed, instead of writing in the command panel with the Send commands?
       
      #RequireAdmin #include <MsgBoxConstants.au3> Global Const $sMessage = "Select a folder" ; Display an open dialog to select a file. Global $sFileSelectFolder = FileSelectFolder($sMessage, "") If @error Then MsgBox($MB_SYSTEMMODAL, "", "Nem választottál ki mappát!") Exit EndIf Global $Tiff_path = $sFileSelectFolder & "\TiffChecker_2_32" FileCopy( "C:\TiffChecker_2_32.exe" , $sFileSelectFolder) $CMD = $Tiff_path & " " & $sFileSelectFolder & " /s/d" Run("C:\WINDOWS\system32\cmd.exe") WinWaitActive("C:\WINDOWS\system32\cmd.exe") Send($CMD) Sleep(1000) Send("{ENTER}") sleep(1000) MsgBox(0,"Press OK", "Press OK to continue") FileCopy("C:\Tiffchecker.txt", $sFileSelectFolder) Also, how do i close the command panel, when the program is finished? (usually its under 1 second, so after the send i just put a Sleep function there)
    • Docfxit
      By Docfxit
      I would like to know if there is a way to make the send cmd send characters when using
      Graphical AutoIt Debugger By Stumpii,
      If Not WinActive("Setup - UltraVNC","Setup is preparing to install") Then WinActivate("Setup - UltraVNC","Setup is preparing to install") WinWaitActive("Setup - UltraVNC","Setup is preparing to install") Send("{ALTDOWN}f{ALTUP}") ;Finish The debugger when using step mode will execute the Send cmd line but the window doesn't receive the input.
      Thanks,
      Docfxit
    • PoojaKrishna
      By PoojaKrishna
      Hi friends,
      I'm trying to send SHIFTDOWN, to keep the Shift key pressed until I close the program or releases the key.
      Send ( "{SHIFTDOWN}") sleep(100) $text = InputBox("Shift key down", "Shift key is down. Type something: ") $ret = Msgbox(0,"", "Press and release shift key manually and select [Ok] to continue") $text = InputBox("Shift key up", "Shift key is released manually, now type something: ") Send ( "{SHIFTDOWN}") sleep(100) $text = InputBox("Shift key down Agin", "Shift key is pressed again, now type something: ") In the above program sequence,
      1. Send SHIFTDOWN from program, Works as we type in the text are in Capital letters
      2. Manually presses and releases the SHIFT button. SHIFT key is released as when we type, text is in small letters only.
      3. Again send SHIFTDOWN from program, but not working. When we type the letters are in small letters.
       
      If we send a SHIFTUP  before  sending the SHIFTDOWN again, then it is working correctly and the text appears in capital letters only. Can any one explain why this happens?
      Send ( "{SHIFTDOWN}") sleep(100) $text = InputBox("Shift key down", "Shift key is down. Type something: ") $ret = Msgbox(0,"", "Press and release shift key and select [Ok] to continue") $text = InputBox("Shift key up", "Shift key is released manually, now type something: ") Send ( "{SHIFTUP}") sleep(100) Send ( "{SHIFTDOWN}") sleep(100) $text = InputBox("Shift key down", "Shift key is pressed, now type something: ")  
      Thank you.
    • TheDcoder
      By TheDcoder
      Hello everyone, long time since I made a post in the H&S sub-forum
      I am working on a project where in a situation I would have to hold down an arrow key and release it in another statement/command, however when I wrote the code it isn't working exactly like I thought it would. Here is an example:
      Send("{UP down}") Sleep(10000) Send("{UP up}") This should hold the up arrow key for 10 seconds before releasing it again, but when I run this code the cursor in SciTE moves up only once
      If I hold the up arrow manually for 10 seconds it "repeats" and the cursor moves up several times until I release the key... Is this a known limitation of Send? Is there any other way I can accomplish this?
      Thanks for all the help and feedback in advance! TD
×