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
first hotkey send 1, when pressed again in 3sec send 12
Re: first hotkey send 1, when pressed again in 3sec send 12
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:
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
into
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.
Code: Select all
If (ErrorLevel)
return ; the return tells the script to not go any further.
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
Code: Select all
If (ErrorLevel)
Continue
Re: first hotkey send 1, when pressed again in 3sec send 12
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
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
Re: first hotkey send 1, when pressed again in 3sec send 12
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.
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.
Re: first hotkey send 1, when pressed again in 3sec send 12
Remove the loop so something like
Code: Select all
F1::
Send 1
Keywait,f1,t3
If errorlevel
Return
Send 2
Send 1
Return
Re: first hotkey send 1, when pressed again in 3sec send 12
works now with this:
maybe there is a more elegant solution but i will check it later.
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
Who is online
Users browsing this forum: Google [Bot] and 334 guests