AutoHotkey Community

It is currently May 27th, 2012, 6:41 am

All times are UTC [ DST ]




Post new topic Reply to topic  [ 10 posts ] 
Author Message
 Post subject: Loop within a loop
PostPosted: July 29th, 2011, 3:29 am 
Offline

Joined: July 29th, 2011, 3:17 am
Posts: 4
I would like to create a loop for holding a key which simulates pressing "a" at the start of the loop and then repeatedly every 5 seconds while it simultaneously simulates the pressing of "b" every 2 seconds.

Any help would be appreciated.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: July 29th, 2011, 3:47 am 
Offline

Joined: April 8th, 2009, 7:49 pm
Posts: 6073
Location: San Diego, California
Hi Pharco, welcome to the forum.

It may not seem like it, but you description is kinda vague.
Can you be more explicit?


btw:
Have you worked through the tutorial
http://www.autohotkey.com/docs/Tutorial.htm


Report this post
Top
 Profile  
Reply with quote  
 Post subject: Loop within a loop
PostPosted: July 29th, 2011, 12:14 pm 
Offline

Joined: July 29th, 2011, 3:17 am
Posts: 4
Thanks for responding so quickly. Yes, I have read through the tutorial and I can manage to write a loop but I can't seem to get a second loop within it going.

What I want to do is create a script which sends the key "a" every 5 seconds and the key "b" every 2 seconds. Right now I'm stuck at my scripts sending either "a" every 5 seconds or "b" every 2 seconds.

$g::
while GetKeyState("g","P")
{
send {a 1}
sleep 5000
}
return

while GetKeyState("g","P")
{
send {b 1}
sleep 2000
}
return

Doesn't work and creating a second $g:: gives an error.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: July 29th, 2011, 1:31 pm 
Code:
#MaxThreadsPerHotkey 1
g::
  SetTimer T_a, 5000
  SetTimer T_b, 2000
  KeyWait g
  SetTimer T_a, Off
  SetTimer T_b, Off
  return

T_a:
  Send a
  return

T_b:
  Send b
  return


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: July 29th, 2011, 2:18 pm 
Offline

Joined: July 29th, 2011, 3:17 am
Posts: 4
Gogo wrote:
Code:
#MaxThreadsPerHotkey 1
g::
  SetTimer T_a, 5000
  SetTimer T_b, 2000
  KeyWait g
  SetTimer T_a, Off
  SetTimer T_b, Off
  return

T_a:
  Send a
  return

T_b:
  Send b
  return


Thanks, this helped me out a lot.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: July 29th, 2011, 3:11 pm 
Offline
User avatar

Joined: October 18th, 2008, 2:09 pm
Posts: 429
Pharco wrote:
Thanks, this helped me out a lot.


do you understand what you did wrong?
Hint: it has to do with the return lines...


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: July 29th, 2011, 4:39 pm 
Offline

Joined: April 8th, 2009, 7:49 pm
Posts: 6073
Location: San Diego, California
Maestr0 wrote:
Pharco wrote:
Thanks, this helped me out a lot.

do you understand what you did wrong?
Hint: it has to do with the return lines...


The whole structure of the script has changed.
Your hint is misleading.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: July 29th, 2011, 5:34 pm 
Offline

Joined: July 29th, 2011, 3:17 am
Posts: 4
Maestr0 wrote:
Pharco wrote:
Thanks, this helped me out a lot.


do you understand what you did wrong?
Hint: it has to do with the return lines...


I think I tried every possible combination of "return" in my script and it never worked


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: July 29th, 2011, 6:14 pm 
Offline
User avatar

Joined: October 18th, 2008, 2:09 pm
Posts: 429
Leef_me wrote:
The whole structure of the script has changed.
Your hint is misleading.

well, it illuminated to me what the problem was.
What would your hint be?

Pharco wrote:
I think I tried every possible combination of "return" in my script and it never worked


You had a return between the first and second whiles, that's why the second would never fire. The solution split it up between several sections (subroutines) and called them in the whiles instead.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: July 29th, 2011, 7:15 pm 
Offline

Joined: April 8th, 2009, 7:49 pm
Posts: 6073
Location: San Diego, California
Pharco wrote:
I think I tried every possible combination of "return" in my script and it never worked

Gogo's script is fundamentally different than the script you posted.

The difference is the use of multiple threads http://www.autohotkey.com/docs/misc/Threads.htm

Here's an analogy to hopefully explain:
In your script, you have one "worker" doing every thing sequentially
There is no chance for the worker to "send {b 1}" because the while loop (and everything in between) won't allow for it.

If you have wanted to send a & b at the same time, they could have been in the same loop. But that isn't what you wanted.

In Gogo's script, there is a supervisor with two employees.
These two employees are special and are controlled by "SetTimer" instructions.
One of them is told "sleep, 5000" and "send a" and repeat ... forever

Meanwhile, the supervisor waits for the "g" key to release.
When the key is is released, the supervisor says, "finish your sleep, and your send, and then stop what you are doing".


This is a reasonably correct analogy. It is not perfect, because AHk is not multi-threaded.
The 3 workers are actually played by just one "actor" who keeps switching between the jobs.


Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 10 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: batto, Bing [Bot], JSLover and 62 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group