Home AutoHotkey: Multiple loops, same timer, asynchronous
Reply: 1

AutoHotkey: Multiple loops, same timer, asynchronous

Arnes
1#
Arnes Published in 2018-01-13 02:29:17Z

I'm trying to make a Keybind for a game. Basically all it does is hold 1 and 2 down for 1000ms and then stop for 1200ms, then repeat. Numpad0 toggles it.

However, i want it to be asynchronous. So i tried adding a sleep between the timers. But it only works one time and then starts to execute both loops at the same time.

I hope someone can help! :)

Numpad0::
autofire := !autofire

if autofire
{
SetTimer, gun1, 1200
sleep 600
SetTimer, gun2, 1200
}
else
{
settimer, gun1, off
settimer, gun2, off
}
Return

gun1:
sendinput {1 down}
sleep 1000
sendinput {1 up}
return

gun2:
sendinput {2 down}
sleep 1000
sendinput {2 up}
return
Jim U
2#
Jim U Reply to 2018-01-16 15:04:07Z

The sleep statements in each timer blocks the other timer from launching on schedule.

Documentation for Autohotkey Threads:

Although AutoHotkey doesn't actually use multiple threads, it simulates some of that behavior: If a second thread is started -- such as by pressing another hotkey while the previous is still running -- the current thread will be interrupted (temporarily halted) to allow the new thread to become current. If a third thread is started while the second is still running, both the second and first will be in a dormant state, and so on.

This is your code with additional timers replacing the sleeps:

Numpad0::
  autofire := !autofire

  if (autofire)
    SetTimer gun1down, -1200
  else
  {
    SetTimer gun1down, off
    SetTimer gun1up, off
    SetTimer gun2down, off
    SetTimer gun2up, off
  }
  return

gun1down:
  sendinput {1 down}
  SetTimer gun2up, -400
  return
gun1up:
  sendinput {1 up}
  SetTimer gun1down, -200
  return
gun2down:
  sendinput {2 down}
  SetTimer gun1up, -400
  return
gun2up:
  sendinput {2 up}
  SetTimer gun2down, -200
  return
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.307543 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO