first hotkey send 1, when pressed again in 3sec send 12

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Aso
Posts: 17
Joined: 10 Nov 2016, 14:00

first hotkey send 1, when pressed again in 3sec send 12

29 May 2017, 12:18

hi mates,

i try to make a makro what does

when i press F1 it sends the key 1
when i press F1 while the next 3 seconds again it sends the key 2 and then sends the key 1

like

F1::
send 1
-------loop starts here:
wait for keypress F1 for 3 seconds
send 2
send 1
------loop end

so if i dont press the F1 again in 3 seconds after the first time it starts from the beginning

can anyone help :D
User avatar
Exaskryz
Posts: 2882
Joined: 17 Oct 2015, 20:28

Re: first hotkey send 1, when pressed again in 3sec send 12

29 May 2017, 13:47

Instead, I would use KeyWait. You can set a time limit (T3). If the time limit is reached, the variable ErrorLevel will be 1; if it is not reached, its value is 0. You can then use something like this:

Code: Select all

If (ErrorLevel)
   return ; the return tells the script to not go any further.
Because the boolean value of ErrorLevel is true when it is not empty or 0 -- a value of 1 is considered true, making such an If statement true.

However, there is a catch... You said you'd like 3 seconds to pass before it does the next Send command, which is where the Loop seemed intuitive to you. I would like to introduce you to A_TickCount.

Store the value of A_TickCount at the time you first press the hotkey (so right after the first send 1), like this: start_time:=A_TickCount. After the KeyWait, If (ErrorLevel), and return section, include Sleep % 3000-(A_TickCount-start_time). The A_TickCount value is ever increasing, and when AHK executes this command what it'll read is the current value of A_TickCount, which is larger than the start_time you stored earlier. It'll find the difference between these. Then it'll subtract from 3000 milliseconds, that difference we calculated, which gives you the rest of the time the script should wait before moving onto the next Send.

(You can also do Send 21 or Send 12 as you please.)

---

Just as I was wrapping up this post, I realized maybe you do want a loop after all. If you want this hotkey to start, and then never stop, but only in every 3 second intervals should it send the 2 and 1 if you pressed a key, here's what I would do. We'll add on to what was talked about above. You can use your Loop to repeat everything from the start_time:=A_TickCount to the last Send(s) you do - wrap that all in a block with { }. However, we would change the

Code: Select all

If (ErrorLevel)
   return
into

Code: Select all

If (ErrorLevel)
   Continue
I know I threw a lot of info at you, and I mean for you to be able to construct this code yourself. Feel free to ask any questions you may have.
Aso
Posts: 17
Joined: 10 Nov 2016, 14:00

Re: first hotkey send 1, when pressed again in 3sec send 12

29 May 2017, 14:37

hm thank you buddy, i made something like this, but only problem now is if i dont put in the sleep 1000 timers its like autofire
but i dont want autofire, just when i press it should do it 1 time for every press

Code: Select all

F1::
send 1
sleep 1000
Loop
{		
KeyWait, F1, D T3
if ErrorLevel 
break
else
send 2
send 1
sleep 1000
}
RETURN
User avatar
Exaskryz
Posts: 2882
Joined: 17 Oct 2015, 20:28

Re: first hotkey send 1, when pressed again in 3sec send 12

29 May 2017, 14:52

So right now as I understand it you're using break so that if you do not press F1 again in 3 seconds from the last time you pressed it, you want to start over from the top.

And without the Sleeps, you are getting an autofire behavior - I imagine this is because without them, the third thing it's encountering is a KeyWait checking to see if that key you just pressed to start the process is down, and given the milliseconds it will take for AHK to reach that point, you probably haven't released the key yet. So, in fact, we may want to use KeyWait, F1 as the first or second line - wait for you to release the key before going into the loop.

Next your else statement is a little funky. Two solutions: 1) Wrap all 3 lines in a block, like you did your loop, or 2) remove the else statement completely. When break[/b] is encountered, AHK jumps out of the loop, ignoring any other lines beneath it inside the loop. That means only when the if statement was false (which matches with what else would do) would the lines below break be executed. Technically, you can the else in your code as is, as even if only send 2 is tied to the else statement, the sleep 1 and sleep 1000 are only going to be run when If ErrorLevel is false anyhow.

Hopefully that fixes it for you.
donovv
Posts: 108
Joined: 15 Apr 2017, 21:06

Re: first hotkey send 1, when pressed again in 3sec send 12

29 May 2017, 15:28

Remove the loop so something like

Code: Select all

F1::
 Send 1
Keywait,f1,t3
If errorlevel 
Return
Send 2
Send 1
Return
Aso
Posts: 17
Joined: 10 Nov 2016, 14:00

Re: first hotkey send 1, when pressed again in 3sec send 12

30 May 2017, 03:36

works now with this:
maybe there is a more elegant solution but i will check it later.

Code: Select all

^F1::
F1::
send 1
Loop
{	
KeyWait, F1, T3
KeyWait, F1, D T3
if ErrorLevel 
break
else
send 21
}
RETURN

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Holarctic, jameswrightesq, Lem2001 and 410 guests