Script that loops after a random delay within an interval - macros

I want to make a loop that clicks, then sleeps for roughly half of a second (but not the exact same delay per iteration), then clicks again, then sleeps for roughly 50 seconds (same situation as before), then loops.
Here's what I whipped up but it doesn't work at all:
loop
{
Click
Sleep %var1%
Click
Sleep %var2%
}
down::
Pause
Return
Random, var2, 50853, 58179
Return
Random, var1, 412, 942
Return
Also, if you could figure out how to make it so a hotkey (something like alt + n) would stop the loop, then pressing the key again would RESET the loop, not resume it.
This script is supposed to be simulating a person double-clicking something then waiting just under a minute to do it again (For a game with macro detection)

It looks like you're off to a good start! Unfortunately, if this is your full script, it would get stuck in the loop as soon as it gets loaded. In order to get a better understanding of the code below, you may want to read through the "Getting Started" section of the help documentation, specifically Hotkeys.
Please see if this works for you:
!n::
bToggle := !bToggle
If bToggle
SetTimer , label_ClickLoop , -1
Return
label_ClickLoop:
Loop
{
Random , var1 , 412 , 942
Random , var2 , 50853 , 58179
Click
Sleep , var1
Click
Loop , 100
{
Sleep , var2 / 100
If !bToggle
Break , 2
} }
MsgBox ,,, Loop cancelled! , 0.75
Return

Related

Multiple MouseGetPos coordinates

I am trying to figure out how to have an AutoHotKey script save different coordinates through MouseGetPos on different hotkeys at the same time, without one overwriting the other.
I want to make a loop performing some sequence of events. Say at one point I want it to execute the first coordinate that I saved before or during the loop (but before the action), and then I want the script to execute the other coordinate at a later point in the loop.
I want the coordinates to be able to be preset (instead of pressing Hotkey1 before the action of hotkey1 and consequently hotkey2 after hotkey1 to ensure one is not overwritten).
An example of this would be:
If one hotkey saves one coordinate:
hotkey1::
MouseGetPos, posx, posy,
// If inserted into a loop, it would be:
MouseMove, %posx%, %posy%,
One hotkey saves another coordinate:
hotkey2::
MouseGetPos, posx1, posy1,
// In loop:
MouseMove, %posx1%, %posy1%,
If inputted into a random loop
#SingleInstance Force
SendMode Input
#MaxThreadsPerHotkey, 2
#NoEnv
#Persistent
SetWorkingDir %A_ScriptDir%
SetTitleMatchMode, 3
SetMouseDelay, -1
Process, Priority, , H
SetKeyDelay, -1, -1
SetDefaultMouseSpeed, 0
SetKeyDelay, 0, 10, Play
SetMouseDelay, 10, Play
CoordMode, Mouse, Screen
hotkey1::
MouseGetPos, posx, posy,
return
hotkey2::
MouseGetPos, posx1, posy1,
return
Toggle=0
f::
Toggle := !Toggle
Loop
{
If Toggle
{
Send {a}
sleep 100
MouseMove, %posx%, %posy%, // Say I want it to move to bottom left corner
Send {LButton}
sleep 100
If !Toggle
Break
MouseMove, %posx1%, %posy1%, // Say this coordinate would be top-right corner
Send {LButton}
sleep 100
Send {c}
}
}
return
q::ExitApp
If I trigger hotkey1 and hotkey2 in that order, hotkey1 would get saved then overwritten by hotkey2, making hotkey1 the same coordinate as hotkey2. I would want 2 separately saved coordinates.
Do I do that by having one script trigger another script or can this be done in a single script?

How do I hold in a key from the start of a loop, until it finishes in AHK?

I Have a loop I am trying to get to work properly, so space is held throughout the entire looping period, and it doesnt have to be pressed within the loop to be activated.
I tried moving the space function outside of the loop, and release it after, but then it doesnt recognize the action at all.
toggle = 0
#MaxThreadsPerHotkey 20
!Z::
Toggle := !Toggle
While Toggle{
Loop, X
{
Send {Space down}
MouseMove, %X1%, %Y1%, 3,
MouseClick, L
Sleep Y
MouseMove, %X2%, %Y2%, 3
Sleep Y
MouseClick, L
Send {Space up}}
If !Toggle
Break
}
return
The result I want to happen is that for as long as that loop is running, spacebar is pressed down, from before the loop starts, and is released after the loop ends.

Send RButton after holding LButton for time

I'm trying to make a script that will press the RButton after holding the LButton for a specific amount of time and ignore the left click. I don't know how to go about this.
Pseudo code:
timer = 0;
while(LButton down){
add 1 to timer?
if(timer==100){
Send {RButton}
ignore LButton
reset timer
}
}
Use A_TickCount as a quick and easy way of measuring the passing of time.
(TESTED)
LButton::
StartTime := A_Tickcount
WaitTimeInMilliSeconds = 1000
tooltip trying
while(StartTime+WaitTimeInMilliSeconds > A_Tickcount)
{
if(!GetKeyState("LButton","P"))
{
click ;send the click anyway if it's not held.
return
}
}
tooltip this happens after 1000 milli second of continuously holding left mouse button
return
If you put this inside a left mouse button hotkey it will reset itself automatically. If you want it looping all the time, you can put the entire thing in an endless loop and reset the starttime instead of return

Autohotkey loop sequence with idle reset

I cobbled the following together from various posts:
keys = 0,9,8,7
loop, parse, keys, `,
{
Key_%A_Index% := A_LoopField
KeyCount++
}
return
XButton1::
Rotation ++
Send % Key_%Rotation%
if Rotation = %KeyCount%
Rotation = 0
return
#Persistent
SetTimer, Check, 1000 ;check every second
return
Check:
If (A_TimeIdle >= 3000)
Rotation = 0
return
The idea being that I press my mouse4 button and it cycles through the keys and then goes back to start, however I also wanted a loop so that if I don't press the button for 3 seconds, it resets back to the start of the sequence. The key sequence works however the idle reset doesnt and I'm not sure where to go from here to debug it.
1- You must let the #Persistent SetTimer, Check, 1000 part before the first return.
2- A_TimeIdle is sensible to any input, even a simple mouse move (by user or by script) resets it to zero. If you want to get the Idle time of this single hotkey use A_TimeSinceThisHotkey instead:
Check:
if (A_TimeSinceThisHotkey >= 3000)
{
Rotation = 0
}
return

AutoHotkey push a button to move curser to the left

I am struggling to make an AutoHotkey script, if anyone would help I would appreciate it.
the script im trying to make is: if I pressed F my mouse would move to the left as long as im pushing the F button from the current location I had my mouse on. This is what I have right now and it's not working.
Loop {
Sleep 10
MouseGetPos,x
if (MouseMove, -1000, 0, 100, R)
send {C down}
else if {C up}
break
}
return
Esc::ExitApp
Try this:
f::MouseMove -5, 0, 50, R
50 is the speed, and the R letter means that the offset is relative to the current cursor position. You don't need the loop, as the pressed key will generate subsequent events on its own.