Functions to call previously defined code

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Cran
Posts: 2
Joined: 01 Feb 2023, 14:34

Functions to call previously defined code

Post by Cran » 01 Feb 2023, 14:51

I'm relatively new to AHK, and I've been wanting to call functions previously defined with simple keywords-

for example, currently I use

DllCall("SetCursorPos", "int", 2044, "int", 262)
MouseClick, left

to click a specific space on my screen

I'd like to say
Click1

and it equate to that in the AHK file, so at the top I would define and use it by

Code: Select all

Functions{
Click1 = DllCall("SetCursorPos", "int", 2044, "int", 262)
MouseClick, left
}

Call Click1
I looked at global functions, etc, but I'm having a bit of trouble understanding how I would do something like this, since most of the examples I've found involve saving variables and returning them via messageboxes or similar-
Thanks for your time^^

User avatar
boiler
Posts: 16926
Joined: 21 Dec 2014, 02:44

Re: Functions to call previously defined code

Post by boiler » 01 Feb 2023, 15:27

Just correcting the syntax:

Code: Select all

Click1() {
	DllCall("SetCursorPos", "int", 2044, "int", 262)
	MouseClick, left
}

Click1()

But even better, pass the parameters to the function:

Code: Select all

Click1(x, y) {
	DllCall("SetCursorPos", "int", x, "int", y)
	MouseClick, left
}

Click1(2044, 262)

Cran
Posts: 2
Joined: 01 Feb 2023, 14:34

Re: Functions to call previously defined code

Post by Cran » 01 Feb 2023, 15:43

That's exactly what I needed- Now I understand how to call functions, but not sure how to call them with different information for each one-
for example, I have

Code: Select all

search(x) {
Loop
{
text =(x)
Send, ^a ; select whole site
Send, ^c ; copy whole site
Click ; click to remove the blue text of selection
IfInString, Clipboard, %text% ; compare the clipboard (^c) with your text above
{
	msgbox, found
	break
}
else
{
	msgbox, not found
	break
}
clipboard = ; clears the clipbard
}
  return
}

search(AutoHotkey2)
it always returns found, even if the page doesn't have "Autohotkey2"

I want to be able to have

Code: Select all

search(potato)
and have that return 1 or 0 based off of if it was found or not, and then do something based off of which response it got

Code: Select all

search(potato)
1= Click1
0= Click2
If you have a better way to find if text is on a chrome page, feel free to let me know. Same goes if I'm going about this the hard way- I'd love to learn more and be more efficient.

User avatar
boiler
Posts: 16926
Joined: 21 Dec 2014, 02:44

Re: Functions to call previously defined code

Post by boiler » 01 Feb 2023, 16:15

Literal strings in expressions must be quoted:

Code: Select all

search("AutoHotkey2")

What you passed was the variable named AutoHotkey2, which is blank and the reason why your search always found it.

Especially since you're new to AHK, I recommend going straight to AHK v2 rather and avoid a bunch of pain coming your way with v1 regarding when to use expressions and when to use legacy/command syntax.

Post Reply

Return to “Ask for Help (v1)”