Function not returning a variable for a autohotkey definition - autohotkey

I've been trying to make an auto-clicker for grinding but it just stops in the middle and I don't understand why. Here's what I have so far:
^!n:: ;Ctr+Alt+n
FindColour(0x4447FB, 338, 491) ; Enter Building
Sleep 1500
FindColour(0x4145F1, 387, 420) ; Left Attack
Battle(1,1,1,1,0)
Sleep 1500
FindColour(0x4447FB, 602, 335) ; Far Attack
; Do Attack
Sleep 1500
FindColour(0x4447FC, 555, 527) ; Bottom Attack
; Do Attack
Sleep 1500
FindColour(0x4347FB, 338, 537) ; Leave Building
Sleep 1500
FindColour(0xF9F9F5, 239, 561) ; Reset Point
Sleep 1500
Return
FindColour(Colour, x, y){ ; Wait until the correct colour appears before clicking
Col = 0x000000
MouseMove, %x%, %y%
Loop
{
PixelGetColor, Col, %x%, %y%
If Col = %Colour%
{
Click %x% %y%
Return
}
Sleep 10
}
Return
}
Battle(a, b, c, d, e){ ; e isn't needed yet
x = 488
y := FindStart()
Col = 0x3F18D0
MsgBox %y%
Loop %a%
{
FindColour(%Col%, %x%, %y%)
Sleep 100
Click 606 276
}
Loop %b%
{
FindColour(%Col%, %x%, %y%)
Sleep 100
Click 547 371
}
Loop %c%
{
FindColour(%Col%, %x%, %y%)
Sleep 100
Click 676 491
}
Loop %d%
{
FindColour(%Col%, %x%, %y%)
Sleep 100
Click 774 309
}
; End
Return
}
FindStart(){
x = 488
y = 200
Loop 300
{
MouseMove, %x%, %y%
Sleep 20
PixelGetColor, Col, %x%, %y%
If Col = 0x3D00FF
{
y += 23
Return %y%
}
Else
{
y += 1
}
}
MsgBox Start not found
}
It's fine until it runs FindStart(). I watch the cursor slowly make its way down the screen to the correct pixel (which moves each battle, thus the function), at which point it stops. I placed the line "MsgBox %y%" after FindStart() is called to check anything happens after the function is called but the msgbox never appears.
What am I missing?

I found the solution. It turns out that variables sent when calling a function should not be enclosed in percentage signs.

Related

Extremely odd behavior when running script

I'm not sure what to title this question because I don't even know what is going on. I'm getting something completely bizarre. The ahk program behaves nothing like I thought I programmed it. Here is my code. What happens is the program does a couple clicks and then starts spamming the click for 10-20ish seconds at incredible speed. Even though I have Esc::ExitApp I can't use it to exit the program. I just have to wait for the clicks to stop before I can use Esc.
Here is my code. I am pulling variables from vars.ahk. If you are wondering what the variables are, they are absolute coordinates to certain areas I would like to click on the screen. I will include both .ahk files in this question.
What could cause this behavior? The program has just simple clicks. Nothing out of the ordinary.
Thanks ahead of time!
My.ahk
#include C:\Users\user\Desktop\vars.ahk
CoordMode, Mouse, Screen
CoordMode, Pixel, Screen
navigateToThing(){
global nwmx, nwmy, dailyx, dailyy, specx, specy
global ntmx, ntmy, safeclickx, safeclicky, dailyxx, dailyyy, nextx, nexty, okx, oky
global gx1, gy1, gx2, gy2 ; green color window
ToolTip, starting...
Sleep, 700
Click, left, %nwmx%, %nwmy%
Sleep, 1000
Click, left, %specx%, %specy%
Sleep, 1000
Click, left, %dailyx%, %daily%
Sleep, 1000
Click, left, %ntmx%, %ntmy%
Sleep 1000
Click, left, %safeclickx%, %safeclicky%
Sleep, 1000
Click, left, %dailyxx%, %dailyyy%
Sleep, 1000
Click, left, %nextx%, %nexty%
ToolTip
;InputBox, arbVal, , , , 100, 100 ; Wait for the okay
Sleep, 1000
Click, left, %okx%, %oky%
Sleep, 5000
Sleep 500
Click, left, %backx%, %backy%
Sleep 1000
Click, left, %evox%, %evoy%
Sleep 600
Click, left, %evoxx%, %evoyy%
Sleep 600
Click, left, %safeclickx%, %safeclicky%
Sleep 600
Click, left, %dailyxx%, %dailyyy%
Sleep 1000
Click, left, %nextx%, %nexty%
ToolTip
;InputBox, arbVal, , , , 100, 100 ; wait for the okay
Sleep 1000
Click, left, %okx%, %oky%
Sleep, 5000
}
;----------------------------------------------------------------------
;MAIN
;----------------------------------------------------------------------
Sleep 1000
navigateToThing()
Esc::ExitApp
vars.ahk
specx = 1563
specy = 373
dailyx = 956
dailyy = 271
ntmx = 1467
ntmy = 406
evox = 1787
evoy = 649
evoxx = 1256
evoyy = 611
safeclickx = 533
safeclicky = 517
dailyxx = 557
dailyyy = 347
nextx = 1234
nexty = 969
okx = 981
oky = 962
nwmx = 396
nwmy = 328
rumx = 948
rumy = 317
solox = 1281
soloy = 957
gx1 = 1081
gy1 = 995
gx2 = 1168
gy2 = 932
You misspelled dailyy here
Click, left, %dailyx%, %daily%.
So the command actually was
Click, left, 956, ,
which is interpreted as sending 956 left clicks.

My script breaks down at some point, I've eliminated all the syntax errors the debugger was able to spot

This is a script that is supposed to use the windows snipping tool to sequentially screencap pictures from an online gallery. If anyone can spot the problem that would be much appreciated.
#SingleInstance, Force
a := 112
name :=1
x:: Pause, Toggle
y:: ExitApp
Loop, a
{
MouseClickDrag, Left, 1300, 210, 645, 140
Sleep, 100
MouseClick, Left, 1277, 1038, 0, 5
sleep, 100
MouseClick, Left, 838, 64, 0, 5
sleep, 100
SendInput, %name%
name ++
sleep, 100
SendInput, {Enter}
Sleep, 100
MouseClickDrag, Left, 670, 13, 1393, 153
Sleep, 100
MouseClick, Left, 500, 490, 0, 5
Sleep, 300
MouseClick, Left, 500, 490, 0, 5
SendInput, {Right}
}
Two problems in it.
Firstly, your loop is unreachable code.
Code execution stops when your first hotkey label is encountered. This is called the Auto-execute Section.
Secondly, loop doesn't take an expression to the first parameter. It takes a legacy text parameter. So you'd either want to use the legacy way of referring to a variable, which would be %a%, but personally I'd push you towards using the modern expression syntax and forcing an expression to that parameter by starting the parameter off with a % followed up by a space. So Loop, % a.
To read more about legacy syntax vs expression syntax, see this page of the documentation.
Here's your fixed script:
#SingleInstance, Force
a := 112
name := 1
Sleep, 3000
Loop, % a
{
MouseClickDrag, Left, 1300, 210, 645, 140
Sleep, 100
MouseClick, Left, 1277, 1038, 0, 5
Sleep, 100
MouseClick, Left, 838, 64, 0, 5
Sleep, 100
SendInput, % name
name++
Sleep, 100
SendInput, {Enter}
Sleep, 100
MouseClickDrag, Left, 670, 13, 1393, 153
Sleep, 100
MouseClick, Left, 500, 490, 0, 5
Sleep, 300
MouseClick, Left, 500, 490, 0, 5
SendInput, {Right}
}
;this return here ends the auto-execute section
;but of course, in this specific case it's totally
;useless since the next line is a hotkey label
;which would also stop the auto-execute section
return
;even though the code execution gets stuck inside the loop,
;hotkeys can be specified down here
;they're created even before the auto-execute section starts
x::Pause, Toggle
y::ExitApp

Why are spacebar events suppressed when running this script?

I made a script to tile windows with my left hand in windows 10, using a dvorak keyboard layout. It works as expected except, it keeps the spacebar events from getting to any program other than the script itself. This is the keyboard log when running the script:
20 039 s d 0.09 Space
20 039 s u 0.08 Space
20 039 s d 0.08 Space
20 039 s u 0.08 Space
20 039 s d 0.08 Space
20 039 s u 0.09 Space
20 039 s d 0.08 Space
20 039 s u 0.09 Space
20 039 s d 31.14 Space
20 039 s u 0.11 Space
20 039 s d 0.09 Space
20 039 s u 0.11 Space
Both space events are detected correctly, but remain suppressed. Terminating the script manually solves the problem.
#MaxThreads 255 ; for the purpose of troubleshooting
#e::return
#.::return
#o::return
#u::return
Space & u::
Space & .::
Space & o::
Space & e::
send {LWin Down}
spcup()
exit
spcup()
{
while GetKeyState("Space", "P") {
rite := GetKeyState("u", "P")
left := GetKeyState("o", "P")
up := GetKeyState(".", "P")
down := GetKeyState("e", "P")
if (rite=1) {
Sleep 75
Send {Right}
Sleep 75
}
else if (left=1) {
Sleep 75
Send {Left}
Sleep 75
}
else if (up=1) {
Sleep 75
Send {Up}
Sleep 75
}
else if (down=1) {
Sleep 75
Send {Down}
Sleep 75
}
else if !GetKeyState("Space", "P") {
sleep 75
Send {LWinUp}
exit
}
}
}
PS, I used space because my left windows key is physically damaged.
Binding Space to itself adding a "Space::Space" line, works partially, only the up event is registered as a hotkey, so it fires only on release of spacebar producing this:
20 039 s d 0.25 Space
20 039 h u 0.16 Space
20 039 i d 0.00 Space
20 039 i u 0.00 Space
s=suppressed
h=hotkey
i=ignored(sent by autohotkey itself)
"Solved", kind of. No way for a prefix key to maintain it's original function without ~, hence sending it to the active window, which is unwanted.
isWindowFullScreen()
{
;checks if the specified window is full screen
;use WinExist of another means to get the Unique ID (HWND) of the desired window
if WinExist("A") {
WinGet, style, Style, A
WinGetPos ,,,winW,winH, A
; 0x800000 is WS_BORDER.
; 0x20000000 is WS_MINIMIZE.
; no border and not minimized
retVal := ((style & 0x20800000) or winH < A_ScreenHeight or winW < A_ScreenWidth) ? 0 : 1
Return, retVal
}
else {
return
}
}
#if isWindowFullScreen() = 0
#e::return
#.::return
#o::return
#u::return
#Space::return
Space::Space
Space & u::
Space & .::
Space & o::
Space & e::
send {LWin Down}
spcup()
exit
spcup()
{
while GetKeyState("Space", "P") {
loop {
rite := GetKeyState("u", "P")
left := GetKeyState("o", "P")
up := GetKeyState(".", "P")
down := GetKeyState("e", "P")
if (left=1) {
Sleep 75
Send {Left}
Sleep 75
}
else if (up=1) {
Sleep 75
Send {Up}
Sleep 75
}
else if (down=1) {
Sleep 75
Send {Down}
Sleep 75
}
else if (rite=1) {
Sleep 75
Send {Right}
Sleep 75
}
} until !GetKeyState("Space", "P")
Sleep 75
Send {LWinUp}
exit
}
}
From the help documentation:
Numpad0 & Numpad1::MsgBox You pressed Numpad1 while holding down Numpad0.
Numpad0 & Numpad2::Run Notepad
The prefix key loses its native function: In the above example,
Numpad0 becomes a prefix key; but this also causes Numpad0 to lose its
original/native function when it is pressed by itself. To avoid this,
a script may configure Numpad0 to perform a new action such as one of
the following:
Numpad0::WinMaximize A ; Maximize the active/foreground window.
Numpad0::Send {Numpad0} ; Make the release of Numpad0 produce a
Numpad0 keystroke. See comment below.
Fire on release: The presence of one of the above custom combination
hotkeys causes the release of Numpad0 to perform the indicated action,
but only if you did not press any other keys while Numpad0 was being
held down. [v1.1.14+]: This behaviour can be avoided by applying the
tilde prefix to either hotkey.
Adding a tilde prefix ~ to each of your Space & hotkeys should make it behave as you expect. Alternatively, you could add ~Space::Return.
Note that this will always send a space, which may have unintended consequences in different applications. For instance, in a browser it will scroll down similar to pressing the
page down key.
Here is a condensed version of your code that I used for testing:
~Space & u::
~Space & .::
~Space & o::
~Space & e::
aReplace := [[ "u" , "Right" ] , [ "o" , "Left" ] , [ "." , "Up" ] , [ "e" , "Down" ]]
Loop , % aReplace.Length()
sReplace := ( SubStr( A_ThisHotkey , 0 , 1 ) = aReplace[ A_Index , 1 ] ) ? aReplace[ A_Index , 2 ] : sReplace
Send , #{%sReplace%}
Return

My ahk code not working

F1::pause,toggle
F2::
Loop,
{
PixelSearch, Px, Py, 432, 298, 444, 286, 0xFFEB63, 3, Fast
if (errorlevel = 0)
{
Sleep 5000
Click 1040,638
Sleep 1500
Click 1055,288
Sleep 10000
}
else{
sleep 3000
Click 1136, 642
sleep 10000
}
}
When I press F2 It should search for 0xFFEB63 in area.If color is not there then click 1136,642.At least,that was what I wanted to do.It directly jumps to else part.
ErrorLevel is set to 0 if the color was found in the specified region, 1 if the color was not found, or 2 if there was a problem that prevented the command from conducting the search.
https://autohotkey.com/docs/commands/PixelSearch.htm#ErrorLevel
F2::
Loop,
{
PixelSearch, Px, Py, 432, 298, 444, 286, 0xFFEB63, 3, Fast
if (errorlevel = 0) ; If color is found in the first area
{
Sleep 5000
Click 1040,638
Sleep 1500
Click 1055,288
Sleep 10000
}
else
if (errorlevel = 1) ; If color is not in the first area
{
sleep 3000
Click 1136, 642
sleep 10000
}
; else ; if (errorlevel = 2)
; do sth else
}
return
EDIT:
You should use
CoordMode, Pixel, Screen
in the autoexecute-section, unless you want the Coordinates to be relative to the active window.

Can someone help me an AutoHotKey script?

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