About ahkFunction

Ask for help, how to use AHK_H, etc.
User avatar
manehscripts
Posts: 126
Joined: 03 May 2019, 16:10

About ahkFunction

10 Jul 2019, 01:05

Hello again!

I'm now studying how to get a function outside the thread so that it works inside the thread. But the example of this link (https://hotkeyit.github.io/v2/docs/commands/ahkFunction.htm) is very confusing, could someone explain me in a simpler way by following the example below? Please!

Code: Select all

#NoEnv
#NoTrayIcon
#SingleInstance, Force

Gui, Add, Button, h50 w200 vstart gstartG, START
Gui, Show, `t
return

toggle := 0
startG:
    toggle := !toggle
    if (toggle) {
        GuiControl,, start, STOP
        startScript := CriticalObject()
        script:="                       
        ("    
            #NoEnv
            #NoTrayIcon
            Loop
            {
                ; 
                PaternThread := AhkExported()
                function := PaternThread.AhkGetVar.function
                a = 5
                b = 8
                c = function(a,b)
                MsgBox % ""a + b: "" c
            }
        )"
        DllThread:=AhkThread(script,&startScript)
    } else {
        DllThread.ahkTerminate()
        GuiControl,, start, START
    }
return

function(x,y){
	return x + y
}

GuiClose:
ExitApp
-----------------------------------------------------------
Stop to think, shut up to resist, and act to win!
HotKeyIt
Posts: 2364
Joined: 29 Sep 2013, 18:35
Contact:

Re: About ahkFunction

10 Jul 2019, 15:56

Try c := PaternThread.ahkFunction(a,b)
User avatar
manehscripts
Posts: 126
Joined: 03 May 2019, 16:10

Re: About ahkFunction

10 Jul 2019, 16:31

HotKeyIt wrote:
10 Jul 2019, 15:56
Try c := PaternThread.ahkFunction(a,b)
Hello master!

Not work, IDK why... I tried 2 types of MsgBox.

Code: Select all

                PaternThread := AhkExported()
				c := PaternThread.ahkFunction(5,8)
                MsgBox, %c%
                MsgBox `% c
-----------------------------------------------------------
Stop to think, shut up to resist, and act to win!
HotKeyIt
Posts: 2364
Joined: 29 Sep 2013, 18:35
Contact:

Re: About ahkFunction

10 Jul 2019, 16:49

Sorry it should be c := PaternThread.ahkFunction("function", 5 "",8 "")
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: About ahkFunction

10 Jul 2019, 16:51

Code: Select all

script = 
(
	#Persistent
	Parent := AhkExported()
	MsgBox `% Parent.AhkFunction("add", "3", "5")
)

DllThread := AhkThread(script)
Esc::ExitApp

add(a, b) {
	return a + b
}
use strings, numbers are treated as pointers and accessing them will crash ur script
User avatar
manehscripts
Posts: 126
Joined: 03 May 2019, 16:10

Re: About ahkFunction

10 Jul 2019, 17:38

Thanks @HotKeyIt and @swagfag, swagfag in your code work very well, but for my code, it throws the error: The following variable name contains an illegal character. (Inside PaternThread.ahkFunction("function", 5 "",8 "")

I have a huge code that works this way that I'm showing below, it follows this pattern, at the moment I can not adjust it all to another standard, and I'm trying to adapt the features the way I've already done. Even because it has worked well so far. I use AhkThread(script,&startScript) because my code is Giant, and I need to concatenate so the program does not exceeds the character size. So I create several script.=" (" )"

Code: Select all

#NoEnv
#NoTrayIcon
#SingleInstance, Force

Gui, Add, Button, h50 w200 vstart gstartG, START
Gui, Show, `t
return

toggle := 0
startG:
    toggle := !toggle
    if (toggle) {
        GuiControl,, start, STOP
        startScript := CriticalObject()
        script:="                        
        ("    
            #NoEnv
            #NoTrayIcon
            Loop
            {
                PaternThread := AhkExported()
				c := PaternThread.ahkFunction("function", 5 "",8 "")
				MsgBox `% c
            }
        )"
        DllThread:=AhkThread(script,&startScript)
    } else {
        DllThread.ahkTerminate()
        GuiControl,, start, START
    }
return

function(x,y){
	return x + y
}

GuiClose:
ExitApp

I've also tried several changes in PaternThread.ahkFunction(function, 5, 8) (for exemple) and it dont work.
-----------------------------------------------------------
Stop to think, shut up to resist, and act to win!
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: About ahkFunction

10 Jul 2019, 17:53

script .= "
("

)"

this quote doesnt do anything there, so idk why ure putting it there
if ure using expression continuation section, then u have to escape the quotes, u double them.

Code: Select all

MsgBox % "
(
""escaped""
""quotes""
)"
User avatar
manehscripts
Posts: 126
Joined: 03 May 2019, 16:10

Re: About ahkFunction

10 Jul 2019, 18:06

swagfag wrote:
10 Jul 2019, 17:53
script .= "
("

)"

this quote doesnt do anything there, so idk why ure putting it there
if ure using expression continuation section, then u have to escape the quotes, u double them.

Code: Select all

MsgBox % "
(
""escaped""
""quotes""
)"
Thanks! :thumbup:
-----------------------------------------------------------
Stop to think, shut up to resist, and act to win!

Return to “Ask for Help”

Who is online

Users browsing this forum: No registered users and 52 guests