AHK Script to send different button with each successive press - autohotkey

Okay so I wrote this script in AHK to press 2 on the first press of the mouse button and 3 on the second and so on, yet it doesnt work. Any ideas why ?
XButton1::Send, 1;
XButton2::
x:=1
if (x = 1){
Send, 2
x+=1
} else if (x = 2) {
Send, 3
x+=1
} else if (x = 3) {
Send, 4
x+=1
} else if (x = 4) {
Send, 5
x = x - 3
}

Everytime you press XButton2, you reset the value of x to 1....x:=1. So it will obviously only send 2. You need to set the value of x outside of XButton2. This will work:
x := 1
XButton2::
x += 1
SendInput, %x%
return

Related

How to Randomize Text in AHK

I am looking to send three different messages every 10 seconds, currently this is what I have but it is not working. As you can see, every 10 seconds I am getting a random int between 1 and 3 and then using if statements to send the corresponding number. Then from there that should send the text.
{
Sleep, 10000 ;
Random, Rand, 1, 3 ;
}
if (Rand == 1)
{
Send {1}
Send {Enter}
}
if (Rand == 2)
{
Send {2}
Send {Enter}
}
if (Rand == 3)
{
Send {3}
Send {Enter}
}
1::
send, test 1
return
2::
send, test 2
3::
send, test 3
Esc::ExitApp
First I don't see that your script is looping, so it runs once and finishes. Then I don't think the bit at the end is needed unless you are specifically wanting to be able to manually press 1, 2 or 3 and have them do something, but that was not mentioned as being your intention.
Try something like this:
stopit = 0
While stopit = 0
{
Loop, 50
{
if stopit = 1
{
ExitApp
}
Random, Rand, 1, 3
if (Rand == 1)
{
Send {1}
Send {Enter}
}
if (Rand == 2)
{
Send {2}
Send {Enter}
}
if (Rand == 3)
{
Send {3}
Send {Enter}
}
Sleep, 5000
}
}
return
F10::
stopit = 1
return
Esc::
ExitApp
To repeat an action periodically (at a specified time interval) you need a timer or a Loop as in the other answer.
AFAIK SetTimer has higher performance than a Loop and consumes less resources.
#NoEnv
#SingleInstance Force
#UseHook
SetTimer, send_random_text, 10000 ; every 10 seconds
Return
send_random_text:
Random, Rand, 1, 3
if (Rand == 1)
GoSub 1 ; jump to this hotkey label (1::)
if (Rand == 2)
GoSub 2
if (Rand == 3)
GoSub 3
Return
1::send, test 1{Enter}
2::send, test 2{Enter}
3::send, test 3{Enter}
Esc::ExitApp

How to reset a key sequence with a specific key?

So this sequence resets itself after 1.5 sec (t:=1500) which means if i dont hit the left mouse button for 1.5 sec it always sends A. Otherwise it sends the next letter after each click.
I want to further tweak this code with another function which is to be able to reset the sequence with right mouse button too. So if i hit RButton any time it should reset to A.
Thx.
global s:=0, c:=0, t:=1500
*LButton::
Send % Seqkeys("A","B","C")
KeyWait, LButton
Send, R
return
Seqkeys(params*) {
global s, c, t
max := params.MaxIndex()
(A_TickCount-s<=t && (c+=1)<=max) ? c : c:=1
s := A_TickCount
return params[c]
}
Merely reset the current key index 'c', and the last clicked time 's':
*RButton::
c := 1
s := 0
return
I think your script would benefit with more meaningful variable names:
global lastClickedTime:=0, currentKeyIndex:=0, clickThreshold:=1500
*LButton::
Send % Seqkeys("A","B","C")
KeyWait, LButton
Send, R
return
*RButton::
currentKeyIndex := 1
clickThreshold := 0
return
Seqkeys(params*) {
global lastClickedTime, currentKeyIndex, clickThreshold
max := params.MaxIndex()
currentKeyIndex += 1
if((A_TickCount - lastClickedTime) <= clickThreshold && currentKeyIndex <= max) {
; Do nothing
} else {
currentKeyIndex := 1
}
lastClickedTime := A_TickCount
return params[currentKeyIndex]
}

Why won't my script run when attemping to make a toggle on and off key?

Im trying to make an autohotkey script that loops but only when the script is toggled on, how can i pull this off? this one just doesnt run and idk why
g::
Suspend
Pause
return
Count := 1
Loop {
if (Count = 1){
Send {s down}
Send {a down}
Send {d up}
Count := Count + 1
}else if (Count = 1000){
Send {a up}
Send {d down}
Count := Count + 1
}else if (Count = 2000){
Count := 1
}else{
Count := Count + 1
}
Sleep, 10
}
First of all, I'm not sure why you're using Suspend. Pause should be sufficient to do what you're looking for. Suspend suspends all hotkeys (except the g hotkey in this case because it's the first line in the hotkey), whereas Pause pauses the current thread. And second of all, you have your loop after the hotkey. The auto-execute section of any autohotkey script is only from the top to the first hotkey, so you need to put the loop first, then the hotkey. Here is the revised code:
Count := 1
Loop {
if (Count = 1){
Send {s down}
Send {a down}
Send {d up}
Count := Count + 1
}else if (Count = 1000){
Send {a up}
Send {d down}
Count := Count + 1
}else if (Count = 2000){
Count := 1
}else{
Count := Count + 1
}
Sleep, 10
}
g::
Pause
return

Why when I run application it just skip some ifs

As I said text that is bolded doesn't work, it just skip it (it worked without those ifs) could you help me? Please, I'm doing that few hours but it still doesn't work. I tried to add this if not in this loop and it worked, please give me a hand, I spent so much time
on it ;(
Gui, Add, CheckBox, x405 y317 w15 h15 vwyrzucaniediax gZapisz,
Gui, Show, h500 w800, Skrypt Na Kopanie
Return
Zapisz:
Gui, Submit , NoHide
return
F8::
WinMove, Minecraft, , , , 1280, 720
sleep 1000
Loop
{
if poziome
{
}
if pionowe
{
}
Checker()
}
Checker()
{
x := 503
y := 385
l := 0
i := 0
Send e
loop, 27{
if(i = 9){
i := 0
l++
}
Sleep 100
MouseMove, x +36*i, y + 36*l
**if wyrzucaniediax // all strong text is just skipped idk why
{
Dropdiax()
}
**
i++
}
Send e
Sleep 50
}
dropdiax(){
diaxcolor := 0x80ĄFFE
color:= getColor()
if(color == diaxcolor){
Send {LCtrl down}
Send q
Sleep 50
Send {LCtrl up}
}
}```
Your wyrzucaniediax variable (maybe you could use better variable names when asking for help?) isn't defined in that function's scope.
To tell the function you're using a variable from outside of its scope, you can use the Global keyword.
Checker()
{
global wyrzucaniediax
x := 503
y := 385
l := 0

Detect F key press after holding right mouse button

I am trying to write a script that detects Right Mouse Click being held for at least 1.5 seconds and then the F key being pressed down while Right Mouse Click is still being held.
I tried the following:
Loop
{
Send, {F Down}
Sleep 2000
Send, {F Up}
sleep 1500
}
return
^F::Pause
I realized this is only a timer and does not detect which keys were pressed. How do I write a script that achieves functionality described above?
loop
{
Key := GetKeyState("rbutton")
while(Key = 0)
{
Key := GetKeyState("rbutton")
sleep,100
}
while(Key = 1)
{
sleep,100
loopnmb := A_Index
if(loopnmb = 8) ;;this number changes the length rbutton has to be pressed before F key activates
{
Key := GetKeyState("rbutton")
while(Key = 1)
{
Send, {F Down}
Key := GetKeyState("rbutton")
if(Key = 0)
{
Send, {F Up}
break
}
}
}
}
}
Timer is a little bit off so play around with it.