Aleksandr Makov
Aleksandr Makov

Reputation: 2938

How to start 2 loops in parallel?

I'm trying to do the following:

~e::
while GetKeyState("e","P")
{
    Send {1 Down}
    Sleep 500
    Send {1 Up}
}

while GetKeyState("e","P")
{
    Send {2 Down}
    Sleep 1000
    Send {2 Up}
}

return

Note the sleep differences. Only key 1 loop is being executed.

I tried both gosub-s and goto-s, but they start only consequently. How can I start and keep 2 loops running at the same time, while a key is being pressed?

Thank you.

Upvotes: 1

Views: 2641

Answers (1)

stevecody
stevecody

Reputation: 676

Autohotkey can not do loops [at the same time] and [in the same script]

what you can do is to use separate two Ahk scripts, together you are able to do loops at the same time simultane (parralel),

you can try this code:

; [+ = Shift] [! = Alt] [^ = Ctrl] [# = Win] 
#notrayicon
#SingleInstance force


ConvertAndRun1("~e:: | while GetKeyState('e','P') | { | Send {1 Down} | Sleep 500 | Send {1 Up} | } | return | ~esc::exitapp")
ConvertAndRun2("~e:: | while GetKeyState('e','P') | { | Send {2 Down} | Sleep 1000 | Send {2 Up} | } | return | ~esc::exitapp")
exitapp

ConvertAndRun1(y)
{
FileDelete, Runscript1.ahk
;a - [Convert String into single codelines with return] - [Char | is the breakline.]
StringReplace,y,y, |, `n, all
StringReplace,y,y, ', ", all
sleep 150

;b - Save String to Ahk file 
x:=";#notrayicon `n #SingleInstance force `n "
z:="`n "
FileAppend, %x%%y%%z%, Runscript1.ahk
sleep 150

;c - Now it can Run all these commands from that Ahk file.
run Runscript1.ahk
sleep 150
}


ConvertAndRun2(y)
{
FileDelete, Runscript2.ahk
;a - [Convert String into single codelines with return] - [Char | is the breakline.]
StringReplace,y,y, |, `n, all
StringReplace,y,y, ', ", all
sleep 150

;b - Save String to Ahk file 
x:=";#notrayicon `n #SingleInstance force `n "
z:="`n "
FileAppend, %x%%y%%z%, Runscript2.ahk
sleep 150

;c - Now it can Run all the commands from that ahk file.
run Runscript2.ahk
sleep 150
exitapp
}

Note:

1 - i did make a script that can convert a single line (Linebreaks=|) with multi commands to single Ahk files.

2 - And in the single line, you will need to use ['] instead of double quotes ["]

3 - With the [Esc] key you can stop both Scripts.

Upvotes: 1

Related Questions