Can someone help me an AutoHotKey script? - autohotkey

I've read the help and guide for it but I just can't figure it out. All I want to do is create a hotkey that when pressed will move my mouse until the hotkey is pressed again. Can anyone tell me how to do this? It should be really simple but apparently I'm missing something.

This is pretty much the most annoying HotKey ever, but here you go (hotkey is Ctrl+Alt+C);
#MaxThreadsPerHotkey 3
^!c::
#MaxThreadsPerHotkey 1
if DoMouseMove
{
DoMouseMove := false
return
}
DoMouseMove := true
Loop
{
Sleep 100
Random, randX, 1, 1028
Random, randY, 1, 800
MouseMove, randX, randY, 25
Sleep, 100
if not DoMouseMove
break
}
DoMouseMove := false
return

I will throw in my solution.
pause::
If (Toggle := !Toggle) ; Toggle the value True/False
{
ToolTip Mouse Mover,A_ScreenWidth/2,0 ; Show that Mouse Mover is active
SetTimer MoveMouse, 1000 ; 1000 ms = 1 sec. Every minute (60000 ms) is probably enough.
}
Else
{
Tooltip ; Turn Mouse Mover alert window off
SetTimer MoveMouse, Off ; Turn the timer off
}
return
MoveMouse:
MouseMove, 1, 0, 1, R ;Move the mouse one pixel to the right
Sleep, 50 ; Wait 50 ms. Not realy required, but makes the move visible
MouseMove, -1, 0, 1, R ;Move the mouse back one pixel
return

Related

How to use pixelsearch?

I have been trying to put together this script that clicks on the pixel with the specified color.
It keeps crashing and clicking unrelated spots.
Is there a better way to implement this? This is what I have (sorry i'm very new to this)
Loop {
PixelSearch, OutputVarX, OutputVarY, 400, 300, 1800, 800, 821A83, Fast
if (ErrorLevel){
MsgBox, no
}
else if (!ErrorLevel)
{
Click, %OutputVarX%, %OutputVarY% Down
Sleep 2000
Click, Up
}
} Until, limit > 0
limit = 0
^w::
limit += 1
return
I answered a similar post a little while ago that might help you (AHK) Can't seem to get a pixel color statement working. Basically using a Timer instead of a Loop would probably be more helpful to you. I edited your code below to what I think might work, but still believe the Timer is a better solution (command SetTimer).
CoordMode, Mouse, Screen ; Or 'Relative' as you desire
CoordMode, Pixel, Screen
; this tells the the pixel search to be relative to the screen
; and the mouse click as well. that might be what is missing here.
Loop {
PixelSearch, OutputVarX, OutputVarY, 400, 300, 1800, 800, 0x821A83, Fast ; decimal or hexadecimal (including "0x" prefix)
if (ErrorLevel) {
MsgBox, no
}
else if (!ErrorLevel)
{
Click, %OutputVarX%, %OutputVarY%, Down
Sleep 2000
Click, Up
}
} Until (limit > 0)
limit = 0
Return
; each block should have a Return unless you want it
; to drop into the code below
^w::
limit += 1
return

How to pause a loop in autohotkey

I need to pause a spacebar spamming macro with a key like f10, here is my code
c::
Loop
{
if not GetKeyState("c", "P")
break
Sleep 25 ; ms
Send {space}
}
return
I tried to add a pause similar to the getkeystate in and out of the loop but to no avail.
I always do something to the extent of this:
c::
Toggle := !Toggle
While Toggle {
; do whatever you need to do here
}
Return
An additional advantage here is that there's only one hotkey to remember. Press once to begin the endless loop. Press again to stop.
q::
Loop
{
Click, right,
Mousemove, 0, 110, 5, Rel
click, left
Mousemove, 350, -473, 5, rel
click, left
Mousemove, -350, 363, 5, rel
}
return
#p::Pause,Toggle
https://autohotkey.com/board/topic/95308-endless-loop-with-hotkey-pause/

Click and drag one pixel in given direction

I am looking for a script that will click and hold the right mouse button down and drag in a given direction 1 pixel every "x" or 2 seconds. Something I can either tell to move in given direction by hitting the corresponding direction key or by adjusting the script manually.
Thank you!
Not entirely sure what you want, but try this:
CoordMode, Pixel, Screen
direction = left
secondsBetweenMoves = 0.1
F1:: ;F1 to start it
SendInput, {LButton Down}
SetTimer, Move, %secondsBetweenMoves%
Return
F2:: ;F2 to end it
SendInput, {LButton Up}
SetTimer, Move, Off
Return
Move:
MouseGetPos, mouseX, mouseY
If (direction = "left") {
MouseMove, mouseX-1, mouseY
}
Else If (direction = "right") {
MouseMove, mouseX+1, mouseY
}
Else If (direction = "up") {
MouseMove, mouseX, mouseY-1
}
Else If (direction = "down") {
MouseMove, mouseX, mouseY+1
}
Return
The send event command might make your life a whole lot easier. For example, what you're trying to do in a simplistic infinite loop:
coordmode, mouse, screen
setmousedelay, 0 # This makes the mouse move extremely fast
loop {
mousegetpos, mx, my
mx := mx+1
sendevent {click, r, down}
sendevent {click, %mx%, %my%, r, up}
sleep 2000
}
This loop will hold down the right button and move one pixel to the right every two seconds. If you want it to move to the left, change
mx := mx+1
to
mx := mx-1
Making it move up or down is the same mechanism, but adding or subtracting to the variable "my" instead.
This loop can be made into a timer, with hotkeys to enable or disable it.

pixelsearch and click right pixel

IF NOT A_IsAdmin ; Runs script as Admin.
{
Run *RunAs "%A_ScriptFullPath%"
ExitApp
}
#MaxThreadsPerHotkey, 2
CoordMode, Pixel, Screen
#singleInstance, Force
toggle = 0
upperLeftX := 750
upperLeftY := 400
lowerRightX := 850
lowerRightY := 500
F8:: ; press F8 to toggle the loop on/off.
SoundBeep
Toggle := !Toggle
While Toggle
{ ;-- Begin of loop.
PixelSearch, X, Y,%upperLeftX%, %upperLeftY%, %lowerRightX%, %lowerRightY%, 0x000000, 0, Fast RGB
IF ErrorLevel = 1 ; IF NOTFound.
{
sleep, 100
}
IF ErrorLevel = 0 ; IF Found.
{
MouseClick, left
sleep, 300
}
} ;-- End of Loop.
return
F8 starts loop and this code checks specific pixel in rectangle and sends left click.
It works with [MouseClick, left, %X%, %Y%].But I want to know how can I use dllcall mouse event to click on specific pixel.
for example
DllCall("mouse_event",uint,1,int,%X%,int,%Y%,uint,0,int,0)
But its not working
I doubt that you actually want to do this via DLL calls. mouse_event doesn't even take coordinates, but values between 0-65535.
If you want to be able to click any pixel on the screen make sure you set it to be relative to the screen: CoordMode, Mouse, Screen
Then use ControlClick/PostMessage/SendMessage if you don't want to affect your mouse pointer by that click. Or use MouseClick/Click. Or MouseMove+Send, {LButton}.

Temporarily Pause Autofire loop on holding button

I wrote a script that sends autofire left clicks and can be triggered on and off. The script works. However, the problem is that holding the right mouse button does not work properly anymore because the left click keeps getting sent. So I want to change the script that it gets temporarily paused while I hold down the right mouse button.
How would I go about doing this? Here is my current code:
#MaxThreadsPerHotkey 3
#z::
#MaxThreadsPerHotkey 1
if keep_winz_running = y
{
keep_winz_running = n
return
}
; Otherwise:
keep_winz_running = y
Loop
{
GetKeyState, rbut, Rbutton
If rbut, = U
{
Loop,
{
MouseClick, left
Sleep, 50 ;This means the script will wait 1.5 secs
if keep_winz_running = n ; The user signaled the loop to stop.
break ; break out of the loop
}
Timers are the best!
sendToggle := false
#z::
if(!sendToggle) {
sendToggle := true
SetTimer, SendClick, 100
} else {
sendToggle := false
SetTimer, SendClick, Off
}
return
#If sendToggle
RButton::
SetTimer, SendClick, Off
KeyWait, RButton
SetTimer, SendClick, 100
return
SendClick:
Click
return
I find the send interval of 50 ms awfully fast, especially since you won't be able to actually reach 50 ms without reducing SetBatchLines and SetKeyDelay. If it really needs to be that fast, consider changing them.