Help with my basic code

Ask gaming related questions
reecemod22
Posts: 6
Joined: 27 Jan 2016, 08:54

Help with my basic code

27 Jan 2016, 09:02

Im looking to make AHK hold down a key for 3 secs then hold down the next key for the same time then repeat

F1:: ; start
Loop 330
{
Send {w down}
Send {w up}
}
Sleep 2000
Loop 331
{
Send {s down}
Send {s up}
}
Sleep 2000]
User avatar
Exaskryz
Posts: 2876
Joined: 17 Oct 2015, 20:28

Re: Help with my basic code

27 Jan 2016, 11:24

Why do you do two loops of repeated w presses followed by repeated s presses? There may be a very good reason for it such as your program needs multiple key presses, but doing the loops that way won't ensure accuracy for 3 seconds.

I would recommend this as your first step for holding w down for 3 seconds, waiting 2 seconds, and then holding s down for 3 seconds.

Code: Select all

F1::
Send {w down}
Sleep 3000
Send {w up}
Sleep 2000
Send {s down}
Sleep 3000
Send {s up}
return
If you would like the whole thing to keep repeating, you can put a Loop block all around it like you did in your original code. In fact, if you want to keep using your original code and have the whole sequence repeat, you can put a Loop inside of a Loop.

However, if the above code does not work, you may need to do repeated keystrokes like you're doing in your original code. I wouldn't bother using two lines to send one key like you're doing, since you can just use Send w and Send s to do the same thing. However, here's a suggestion for doing a loop for a certain amount of time:

Code: Select all

F1::
Loop
{
starting_time:=A_TickCount
While (A_TickCount-starting_time<3000)
     Send w ; the { and } on the line above and below this line are optional
Sleep 2000
starting_time:=A_TickCount
While (A_TickCount-starting_time<3000)
    Send s
Sleep 2000
}
return
First thing I added was a custom variable called starting_time that copied the value of the built-in variable A_TickCount. The While loop evaluates an expression every time it is about to run. The expression in this case is is the new value of A_TickCount (which is updated every 15ms or however fast; maybe it's always accurate when the variable is called) 3000 units (milliseconds) larger than the value starting_time has? If it is smaller than 3000 ms difference, then it'll send the w key. Otherwise it will break the loop and move onto the next step which is Sleep 2000. Take note that A_TickCount is being dynamic and always changing, while starting_time is static and will stay at whatever value it was set at -- until it is changed again as per line 8 in the code above.

Check out https://autohotkey.com/boards/viewtopic.php?f=7&t=11952 for ways to control your loop so you can stop it when you want.
Shadowpheonix
Posts: 1259
Joined: 16 Apr 2015, 09:41

Re: Help with my basic code

27 Jan 2016, 11:28

For another way to do it, see my answer in your other thread.
reecemod22
Posts: 6
Joined: 27 Jan 2016, 08:54

Re: Help with my basic code

27 Jan 2016, 21:57

i did Shadow phoenix and it worked perfect im just trying to add this to it without interupting the keystring loop
Click:
If (!Toggle)
Return
Click
Send a
return
reecemod22
Posts: 6
Joined: 27 Jan 2016, 08:54

Re: Help with my basic code

27 Jan 2016, 22:11

this what i have but ti interupts the first string
#MaxThreadsPerHotkey 2 ; Allows you to use the same key to turn the loop on & off.
F12:: ; Key to start & stop the loop
Toggle := !Toggle ; Switch the "Toggle" variable between true & false
While Toggle ; Loop while the "Toggle" variable is true
{
Send {w down} ; Hold the W key down.
Sleep 3000 ; Wait 3000 milliseconds.
Send {w up} ; Release the W key.
If !Toggle ; Abort the next key if "Toggle" is now false
Break
Send {s down} ; Hold the S key down.
Sleep 3000 ; Wait 3000 milliseconds
Send {s up} ; Release the S key.
SetTimer Click, 100

F8::Toggle := !Toggle

Click:
If (!Toggle)
Return
Click
Send a
return
}
Return ; Marks the end of the F12 hotkey code.
User avatar
Exaskryz
Posts: 2876
Joined: 17 Oct 2015, 20:28

Re: Help with my basic code

27 Jan 2016, 23:15

You don't want to put a hotkey within a hotkey (there's few instances where you would, but not in this case). When you pres F12, it will keep going all the way to the return, and in this case, there is an implicit return on the F8 hotkey because it is a single-line hotkey. The return is encountered and now Toggle is in the off state. The Click label called by the timer thus doesn't do anything because the value of Toggle is 0.

Your code may do what you want if you put the F8 hotkey above the F12 hotkey. However, your while loop includes the Click label. You may not want that either. Because what'll happen is the Timer will be set the first time, and it'll wait 100ms before executing the label. But immediately after the timer is set, while the timer is waiting to launch, the while loop keeps reading and will do a Click and Send a.

In fact, yes! When I see your code very closely, you don't want that Click label in there at all, because a "return" is still inside of the While loop. So either that should be removed, or more likely what you want, is to move the entire Click subroutine outside of the While loop.

Return to “Gaming”

Who is online

Users browsing this forum: No registered users and 9 guests