AutoHotKey 如何向函数传递/评估参数

AutoHotKey 如何向函数传递/评估参数

我无法理解如何在 AutoHotKey 函数中访问参数。

例如,我使用 InputBox 设置 myVar 变量,然后将其传递给函数。如何在 TestFunction 中评估 arg?

#t::
    inputbox myVar, What is your variable?
    myNewVar := TestFunction(%myVar%)
    MsgBox %myNewVar% 
    return

TestFunction(arg)
{
    MsgBox arg
    msgBox %arg%
    return %arg%
}    

我想要做的是设置一个热键,提示输入应用程序的关键字,然后评估在函数中输入的内容并启动与该关键字相对应的应用程序。

谢谢!

克里斯

答案1

我已经修正了你的脚本(按照 Bavi_H 建议的)并添加了一个示例来启动与关键字相对应的应用程序。

#t::
inputbox myVar, What is your variable?
myNewVar := TestFunction(myVar)
MsgBox %myNewVar% 
return

TestFunction(arg)
{
    msgBox %arg%
    if (arg = "calc")
    {
        run, calc.exe
    }
    else if (arg = "word")
    {
        run, winword.exe
    }
    return arg . "bob"
}

答案2

基本上,命令(例如run, %something%)与函数(例如)不同myFunction(something)。以下是基于 qwertzguy 的回答的另一个示例

#t::
    ; get variable from message box
    inputbox myVar, What is your variable?

    ; myVar DOES NOT have percents when passed to function
    myNewVar := TestFunction(myVar)

    ; myNewVar DOES have percents when passed to command
    MsgBox %myNewVar% 

return


TestFunction(arg)
{
    ; command DOES have percents 
    MsgBox Launching: %arg%

    if (arg = "calc")
    {
        ; commands use traditional variable method
        ; traditional method example: Var = The color is %FoundColor%
        ; variables are evaluated inside quotes

        run, "%A_WinDir%\system32\calc.exe"
    }
    else if (arg = "word")
    {

        ; functions need to use expression version since percents are not evaluated
        ; expression method example: Var := "The color is " . FoundColor
        ; variables are not evaluated inside quotes

        EnvGet, ProgramFilesVar, ProgramFiles(x86)
        OfficeVersionVar := "15"

        RunFunction(ProgramFilesVar . "\Microsoft Office\Office" . OfficeVersionVar . "\WINWORD.EXE")

    }

    return "You typed: " . arg

}



RunFunction(arg)
{
    run, %arg%
}

相关内容