如何解析函数中的变量,以便能够定义要发送的变量?

时间:2015-04-15 07:59:18

标签: autohotkey

这是我的代码

IniRead, custommessage1, whisperconfig.ini, messages, Message1
IniRead, custommessage2, whisperconfig.ini, messages, Message2
^NumPad1::whispermessage(1)

whispermessage(var){
finalwhisper := "custommessage" + var ;this equals custommessage1
Msgbox, %finalwhisper%
BlockInput On
SendInput ^{Enter}%finalwhisper%{Enter} ;<-- problem
BlockInput Off
return
}

所以在第一行我导入了custommessage1的值(它可能是&#34; hi im henrik&#34;)。这就是我希望最终得到的结果。

在函数内部我希望var(在这种情况下为1)与名为custommessage的变量合并,结果为custommessage1

我希望endresult能够执行SendInput%custommessage1%。

这样我可以有一个函数,最多可以包含9个触发器,包括var数。

有人可以帮忙吗?我相信这很简单但是我对这个编码事物不熟悉所以请耐心等待。

1 个答案:

答案 0 :(得分:0)

您的函数只知道它自己的变量,作为参数传递的变量和全局变量。

您正试图访问custommessage1,这不在函数范围内且不是全局的。您需要将所有变量设置为全局变量,或者将它们传递给函数。

我建议后者使用数组。以下是一个示例,请确保您正在运行latest version of AHK以使其正常工作。

IniRead, custommessage1, whisperconfig.ini, messages, Message1
IniRead, custommessage2, whisperconfig.ini, messages, Message2

; Store all messages in an array
messageArray := [custommessage1, custommessage2]

; Pass the array and message you want to print
^NumPad1::whispermessage(messageArray, 1)

whispermessage(array, index){
    ; Store the message at the given index in 'finalwhisper'
    finalwhisper := array[index]

    Msgbox, %finalwhisper% ; Test msgbox

    ; Print the message
    BlockInput On
    SendInput ^{Enter}%finalwhisper%{Enter}
    BlockInput Off
}

或者,这超出了您的问题范围,您可以动态加载.ini文件中的键,这意味着每次添加键/值对时都不必创建新变量。< / p>

以下是如何做到的:

; Read all the key/value pairs for this section
IniRead, keys, whisperconfig.ini, messages
Sort, keys ; Sort the keys so that they are in order

messageArray := [] ; Init array

; Loop over the key/value pairs and store all the values
Loop, Parse, keys, `n
{
    ; Trim of the key part, and only store the value
    messageArray.insert(RegExReplace(A_LoopField, "^.+="))
}

; Pass the array and message you want to print
^NumPad1::whispermessage(messageArray, 1)

whispermessage(array, index){
    ; Store the message at the given index in 'finalwhisper'
    finalwhisper := array[index]

    Msgbox, %finalwhisper% ; Test msgbox

    ; Print the message
    BlockInput On
    SendInput ^{Enter}%finalwhisper%{Enter}
    BlockInput Off
}
相关问题