how to make the function name as the variable and put in the array?

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
woshichuanqilz72
Posts: 117
Joined: 05 Oct 2015, 21:23

how to make the function name as the variable and put in the array?

11 Jul 2018, 04:39

here is my code I can use 'q' to toggle the timer which is reading the array and execute the corresponding label, it works fine.

Sendkey will iterate the array.

But when the label get complicated, I want to pass parameter into it.

Code: Select all

global start := true
ArrOrMaxNum :=["test1", "test2", "test3"]
global index := 1
q::
global start
global index
if start
{
    SetTimer, SendKey, 1000
}
else
{
    index := 1
    SetTimer, SendKey, Off
}
start := !start
Return

SendKey:
{
    global index
    Mod_base := ArrOrMaxNum.MaxIndex()
    index_tmp := Mod(index, Mod_base)
    if(index_tmp = 0)
    {
        index_tmp := 3
    }
    key := ArrOrMaxNum[index_tmp]
    gosub, %key%
    index := index + 1
    Return
}

test1:
{
    MsgBox %A_ThisLabel%
    Return 
}

test2:
{
    MsgBox %A_ThisLabel%
    Return 
}

test3:
{
    MsgBox %A_ThisLabel%
    Return 
}
User avatar
evilC
Posts: 4824
Joined: 27 Feb 2014, 12:30

Re: how to make the function name as the variable and put in the array?

11 Jul 2018, 07:54

How to store a function as a variable:

Code: Select all

fn := Func("SomeFunc")

fn.Call(1)
return

SomeFunc(blah){
	MsgBox % blah
}
If you need to store a function plus passed parameters as a variable:

Code: Select all

fn := Func("SomeFunc").Bind("My Bound Text")

fn.Call(1)
return

SomeFunc(boundText, blah){
	MsgBox % blah ", Bound Text: " boundText
}
Docs: https://autohotkey.com/docs/objects/Functor.htm

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Bing [Bot], Rohwedder and 230 guests