True In-Line Send Sleep Function

Post your working scripts, libraries and tools for AHK v1.1 and older
User avatar
AfterLemon
Posts: 85
Joined: 30 Sep 2013, 14:27
Location: Ohio, USA

True In-Line Send Sleep Function

23 Oct 2013, 10:46

True In-Line Send Sleep Function

Syntax is as simple as any other key.

Parameters:
STRING = Any string-style Send you wish to preform.
RAW = Parameter to determine if modified keys are sent as modified, or as plain text. e.g. ^v,^c,^{Enter}
RAWKEYS = Parameter to determine if braced keys are sent as keys, or as plain text. e.g. {Enter},+#{Tab}


Supports Milliseconds by default, but with a single character after the number, supports seconds.
e.g.:
{500} = 500ms
{20000} = 20s
{20s} = 20s
{2.5a} = 2.5s (2500ms)

NOTE: Any (ms) sleep below standard Windows minimum of 15 or 17.8 will sleep for the standard time. See: Sleep

Code: Select all

Send(String, Raw:="", RawKeys:=""){
	D:="{",E="}",S:=String D,i=0,T=1,R=(Raw?1:(SubStr(S,1,5)="{RAW}"?1:0)),M="+,!,#,^",K=RawKeys
	While i:=InStr(S,D,V,i+1){
		Send,% (R?"{RAW}":"") SubStr(S,T,InStr(S,D,V,i)-T)
		B:=SubStr(S,InStr(S,D,V,i)+1,InStr(S,E,V,i)-StrLen(S)-1),A=SubStr(B,1,-1)
		If InStr(S,D,V,i+1)
			If(B&1=""){
				If(A&1!="")
					Sleep,% A*1000
				else{
					L:=(!R?(InStr(S,E,V,i)-StrLen(B)-2>4?4:InStr(S,E,V,i)-StrLen(B)-2):0)
					Loop,%L%{
					C:=SubStr(SubStr(S,InStr(S,D,V,i)-L,L),A_Index,1)
					If C in %M%
					{	C:=SubStr(S,InStr(S,D,V,i)-(L+1-A_Index),L+1-A_Index)
						break
					}else C:=""
				}Send,% (K?"{RAW}":"") C "{" B "}"
			}}else Sleep,%B%
		T:=InStr(S,E,V,i+1)+1
}}
Send("Really,{2000} any sleep !fcould{3000}{Enter} go+{Tab 8} here {3.5s}and it {500}should work.",1)

This will output as:

Really, any sleep !fcould
go+ here and it should work.


I thought it was time for a real working function. Still looking for bugs and will keep up to date.


--AfterLemon
Last edited by AfterLemon on 08 Nov 2013, 11:46, edited 4 times in total.
User avatar
Grendahl
Posts: 170
Joined: 30 Sep 2013, 08:21

Re: True In-Line Send Sleep Function

23 Oct 2013, 10:59

Glad to see this has made it over to the new forums!
drdutw
Posts: 5
Joined: 23 Oct 2013, 12:20

Re: True In-Line Send Sleep Function

23 Oct 2013, 12:58

it works!
Verdlin
Posts: 63
Joined: 04 Oct 2013, 08:55
Contact:

Re: True In-Line Send Sleep Function

05 Nov 2013, 14:48

Good function! _H users should note that this is not necessary, though. From the AutoHotkey_H documentation,

"Sleep functionality in Send command (for example Send abc{100}def will send “abc”, sleep 100ms and then send “def”)"
User avatar
AfterLemon
Posts: 85
Joined: 30 Sep 2013, 14:27
Location: Ohio, USA

Re: True In-Line Send Sleep Function

06 Nov 2013, 13:22

While this is technically true, Verdlin, there is one difference between my implementation of the Send Sleep idea and the _H implementation.

My function supports single-number sleeps (e.g. {0},{1}). This allows Sleep,0 to yield it's timeslice to other processes.
AutoHotkey_H sends {0} as the 0 key.

From AutoHotkey Sleep Documentation:
"A delay of 0 yields the remainder of the script's current timeslice to any other processes that need it (as long as they are not significantly lower in priority than the script)."

This could be time enough for an Excel document to finish applying any changes/redraw or for a control to accept the previously-sent input before the send continues.

AutoHotkey_H does NOT support Send,{0} as a sleep.
Last edited by AfterLemon on 06 Nov 2013, 14:12, edited 1 time in total.
HotKeyIt
Posts: 2364
Joined: 29 Sep 2013, 18:35
Contact:

Re: True In-Line Send Sleep Function

06 Nov 2013, 13:46

You should put a note that your function does not support {0} as 0 key.
E.g. when user enters 0 - 9 here, nothing will be send so a work around is needed

Code: Select all

Input,var,L1
Send("{" var "}")
I don't think it's worth it, most computers are multi-core now days anyway and any advantage for {0} versus {10} would be really hard to prove.

Also AutoHotkey_H uses Sleep() ( DllCall("Sleep"...) ) rather than internal MsgSleep() so the Send command cannot be interrupted by any timers, gui actions, hotkeys...
User avatar
AfterLemon
Posts: 85
Joined: 30 Sep 2013, 14:27
Location: Ohio, USA

Re: True In-Line Send Sleep Function

06 Nov 2013, 14:04

Your example is flawed, HotKeyIt.

If you intend to send a SLEEP, your example is correct, and the sleep (even a sleep of 0) will be performed.
If you intend to send a key, this example WILL work.

Code: Select all

Input,var,L1
Send(var)
Even on a multi-core machine, Sleep,0 and Sleep,10 are quite different, as per the design of the Sleep command.

The implementation in AutoHotkey_H might also have drawbacks.
If you are managing response-time delays from slow programs (or Guis), interaction at a process-level vs DllCall might be desired.
As well, interruptions by Gui Actions, SetTimers, Hotkeys, etc. are sometimes preferred, rather than a solid un-interruptible send (especially in large Send strings).

To the contrary, _H users will see a higher performance in the send, and sleeps will be more accurate.

In the end, however, both the Built-In Sleep command of AutoHotkey_H and this function can be used in tandem if the user desires the function of both.
For Example:
Send,% "Game starts in 4 seconds.{4000}`nHit x within 3 seconds" Send("{3s}") (A_PriorHotkey="x"?"Good job!":"You Lose")
HotKeyIt
Posts: 2364
Joined: 29 Sep 2013, 18:35
Contact:

Re: True In-Line Send Sleep Function

06 Nov 2013, 14:41

Not exactly.
Try below code, you will see { will not work without brackets and 0 will not work with brackets while both will work with Send command using {}.

Code: Select all

Gui,+AlwaysOnTop
Gui,+OwnDialogs
Gui,Add,Edit,w200
Gui,Show
MsgBox Using Sleep Function:`nClick OK and Press {
Input,v,L1
Send(v)

MsgBox Using Sleep Function:`nClick OK and Press 0
Input,v,L1
Send("{" v "}")

MsgBox Using Sleep Command:`nClick OK and Press {
Input,v,L1
Send {%v%}

MsgBox Using Sleep Command:`nClick OK and Press 0
Input,v,L1
Send {%v%}
Sleep 2000
ExitApp
User avatar
AfterLemon
Posts: 85
Joined: 30 Sep 2013, 14:27
Location: Ohio, USA

Re: True In-Line Send Sleep Function

06 Nov 2013, 15:01

Again, your understanding of the function is flawed. In some ways the { key is a modifier.

In order to see how the script is intended to work, realize that the Raw parameter must be TRUE to send "{" as a key.

Send("{",1) will send "{" as a key rather than as a wrapper for keys.
Send("{100}",,1) will send "{100}" as text rather than as a sleep (impossible with AutoHotkey_H)

To address the Send("{0}") you're creating with the Second example, that is handled as a Sleep,0... not Send,0.
To send a 0, simply write Send(0).

Return to “Scripts and Functions (v1)”

Who is online

Users browsing this forum: Bing [Bot] and 109 guests