More efficient coding?

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
CuriousDad
Posts: 79
Joined: 21 Mar 2023, 11:23

More efficient coding?

Post by CuriousDad » 29 Nov 2023, 14:07

Hi. I'm trying to figure out if there is a more efficient way to inserting a delay after every command. The delay seems to help with the accuracy of the keystroke. The delay functions such as SetKeyDelay or SetControlDelay don't seem to be what I'm looking for.

Current Code to sort items in a folder by type:

Code: Select all

Send {F10}
Sleep 250
Send {Right}
Sleep 250
Send {Enter}
Sleep 250
Send {Down 6}
Sleep 250
Send {Right}
Sleep 250
Send {Down 2}
Sleep 250
Send {Enter}
return
Any thoughts? Thanks.

RussF
Posts: 1377
Joined: 05 Aug 2021, 06:36

Re: More efficient coding?

Post by RussF » 29 Nov 2023, 15:36

Is this your entire script? SetKeyDelay should work since V1 uses SendEvent mode by default. If, however, you have used SendMode Input (inserted automatically if you create a new ahk file with the built-in template), then SetKeyDelay has no effect. Also, 250ms between keys seems like an awful long time just to move a cursor around.

Russ

CuriousDad
Posts: 79
Joined: 21 Mar 2023, 11:23

Re: More efficient coding?

Post by CuriousDad » 30 Nov 2023, 02:39

Thanks. In reading the documentation, I thought that SetKeyDelay created delays between keystrokes (such as typing out a word) and how long a key is held down. If so, does it really space out each command function? I placed 250 ms between each command because sometimes, the commands moves faster than cursor and steps get missed.

Rohwedder
Posts: 7824
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: More efficient coding?

Post by Rohwedder » 30 Nov 2023, 02:58

Hallo,
or with a Timer?:

Code: Select all

q:: ;start/restart
Sends := ["F10","Right","Enter","Down 6","Down 2","Enter"]
Count := 0
Sends:
Send,% "{" Sends[++Count] "}"
SetTimer, Sends,% Count-Sends.Count()?250:"Off" ;every 250 ms
Return
or:

Code: Select all

q:: ;start/restart
Sends := ["F10","Right","Enter","Down 6","Down 2","Enter"]
Sleeps := [250,250,250,250,250,"Off"], Count := 0
Sends:
Send,% "{" Sends[++Count] "}"
SetTimer, Sends,% Sleeps[Count]
Return

User avatar
boiler
Posts: 17710
Joined: 21 Dec 2014, 02:44

Re: More efficient coding?

Post by boiler » 30 Nov 2023, 05:12

CuriousDad wrote: Thanks. In reading the documentation, I thought that SetKeyDelay created delays between keystrokes (such as typing out a word) and how long a key is held down. If so, does it really space out each command function? I placed 250 ms between each command because sometimes, the commands moves faster than cursor and steps get missed.
To use this approach and ensure correct settings as pointed out by RussF:

Code: Select all

SetKeyDelay, 250
SendEvent, {F10}{Right}{Enter}{Down 6}{Right}{Down 2}{Enter}
SetKeyDelay, 10 ; set back to default if there is more to the script

By using SendEvent instead of Send, it doesn’t matter if you have set SendMode to Input at the top of the script. That only applies when using Send specifically.

Rohwedder
Posts: 7824
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: More efficient coding?

Post by Rohwedder » 30 Nov 2023, 08:34

But SetKeyDelay has a side effect!
https://www.autohotkey.com/docs/v1/lib/SetKeyDelay.htm#Remarks
During the delay (sleep), the current thread is made uninterruptible.
Try to trigger with s:: SoundBeeps while the q:: hotkey routine is sending (does not work).
While the w:: hotkey routine is sending this works (and with my timer anyway!).

Code: Select all

s::SoundBeep, 4000, 20
q::
SetKeyDelay, 250
SendEvent, {F10}{Right}{Enter}{Down 6}{Right}{Down 2}{Enter}
SetKeyDelay, 10 ; set back to default if there is more to the script
Return
w::
Send {F10}
Sleep 250
Send {Right}
Sleep 250
Send {Enter}
Sleep 250
Send {Down 6}
Sleep 250
Send {Right}
Sleep 250
Send {Down 2}
Sleep 250
Send {Enter}
return

CuriousDad
Posts: 79
Joined: 21 Mar 2023, 11:23

Re: More efficient coding?

Post by CuriousDad » 30 Nov 2023, 10:12

Thanks to everyone for the response. I'll try it out.

Update: Success! Thank you!

Post Reply

Return to “Ask for Help (v1)”