AutoHotKey - 使用变量接收来自数组的内容'整数作为索引

时间:2015-07-18 17:10:12

标签: arrays if-statement autohotkey trayicon

我想使用变量'从数组中接收字符串。整数作为数组索引。但它没有用。

尝试1

; Suspended | 0 = No, 1 = Yes
global Suspended     := 0
global SuspendedMsg  := ["The script has been paused.","The script has been re-activated."]

Pause::
    Suspend
    if suspended = 0 ; If script is not suspended
    {
        TrayTip, Paused, SuspendedMsg[Suspended], 3
        Suspended++
    } else ; If it is suspended
    {
        TrayTip, Activated, SuspendedMsg[Suspended], 3
        Suspended--
    }
return

尝试#1只显示字符串" SuspendedMsg [Suspended]"因为我不知道在哪里设置变量指标%。即使我将其设置为SuspendedMsg[%Suspended%],它也会显示[1]或[0]。

尝试2

; Suspended | 0 = No, 1 = Yes
global Suspended      := 0
global SuspendedMsg   := ["The script has been paused.","The script has been re-activated."]
global SendSuspendMsg := SuspendedMsg[Suspended]

Pause::
    Suspend
    if suspended = 0 ; If script is not suspended
    {
        TrayTip, Paused, %SendSuspendMsg%, 3
        Suspended++
    } else ; If it is suspended
    {
        TrayTip, Activated, %SendSuspendMsg%, 3
        Suspended--
    }
return

尝试#2也不会做,它甚至不会显示任何消息。我尝试在global SendSuspendMsg := SuspendedMsg[Suspended]变量中摆弄了围绕%,但它不会有任何好处。有人在乎帮助我吗?

2 个答案:

答案 0 :(得分:0)

而不是TrayTip, Paused, SuspendedMsg[Suspended], 3TrayTip, Paused, SuspendedMsg[%Suspended%], 3,请尝试

TrayTip, Paused, % SuspendedMsg[Suspended], 3

。 TrayTip要求您提供

  

指定要显示的消息

表示与String一样多。因此,变量名称在此处不作为变量处理,而是作为字符串处理(与命令中的大多数时间一样)。陈述TrayTip, Paused, %SuspendedMsg[%Suspended%]%, 3是有意义的 ,但你不能嵌套变量的百分号。因此,我们必须使用百分号强制表达式:

  

强制表达式:通过在表达式前面加上百分号和空格或制表符,可以在不直接支持它的参数(OutputVar参数除外)中使用表达式。在[v1.1.21 +]中,此前缀可用于除传统IF命令之外的所有命令的InputVar参数(改为使用If(表达式))。这种技术通常用于访问数组。

关于你的第二个问题:我不认为数组可以这样宣布,他们可以......? (但我不确定)。另请参阅this简短文章。所以我猜问题就在你代码的第3行,因为其余部分对我来说很好看

答案 1 :(得分:0)

@Blauhim错过了一个重要的观点,尽管他的回答大多是正确的。首先像你一样创建一个数组中的索引,总是从1开始,然后继续到2等等......所以当你试图使用你的布尔变量来调用索引作为0索引时,你的代码是有缺陷的不存在(更不用说你没有强制和TrayTip命令上的表达式)。

; Set our variable to 1 why? Because we are going to use a Logical switch below.
Suspended     := 1
; This was correct format and I left it, although I removed Global's as they are not needed
SuspendedMsg  := ["The script has been paused.","The script has been re-activated."]

Pause::
    ; Suspend toggles each time it's called
    Suspend

    ; Here we are toggling the value of our variable using !
    ; We started with a 1 so that it would be correctly
    ;Changed to a 0 for the code below.
    suspended := !suspended

    ; Nothing changed here
    if suspended = 0 ; If script is not suspended
    {
        ; In order to pass an Array or Object or Expression to a Command you Force it
        ; using the a Percent Sign with a space on either side.
        ; Also note you were trying to use your Logical True/False 0 or 1 variable to         
        ; itterate. This didn't work because Array's always start with an Index of 1. 
        ; Below I've accounted for this by simply added a 1 to your suspended so it correctly 
        ; points to the Index in our Array.
        TrayTip, Paused, % SuspendedMsg[suspended + 1], 3

    } else ; If it is suspended
    {
        TrayTip, Activated, % SuspendedMsg[suspended + 1], 3        
    }
return