check if key variable is equal to button

Viewed 36

i wanna click a random button 50 times from a list unless it is 1 only click it once, its always going to the else even if key is 1

!F2::
    breakvar = 1
return

!F1::
    loop                                                    
    {
        Random, var, 1,7
        keyList = {1},{2},{3},{w},{a},{s},{d}
        StringSplit, KeyAry, KeyList, `,,%A_Space%
        key := KeyAry%var%
        loop 50
        {
            ; i've tryied:
              ;(%key% = "1")
              ;(key = 1)
              ;(key = {1})
            ; but with out success

            if (key = "1")
            {
                Send, %key%
                break
            }
            else
            {
                Send, %key%
                Sleep, 100
            }
        }
    if breakvar = 1
        break
}
breakvar = 0
return

also is there a better way to achieve what i am trying to do?

thx

1 Answers

Your main mistake was that you indeed didn't manage to get the if statement corrent. The right one would've been if (key = "{1}").
But really, I don't know why you even had the { }, it's a common mistake I see from nearly every beginner, I wonder where it comes from.
You're only supposed to use { } for escaping or if the special notation is needed for something such as {space}. Read more from the documentation.

Here's a cleaned up script with all other weirdness removed as well:

KeyList := StrSplit("1,2,3,{space},+1,w,", ",") ; +1 is shift + 1 (whatever key that will produce in your keyboard layout)

!F2::BreakVar := true

!F1::
    loop
    {
        Random, index, 1, 7
        key := KeyList[index]
        loop 2
        {
            SendInput, % key
            if (key = "1")
                break
            Sleep, 100
        }

        if (BreakVar)
            break
    }

BreakVar := false
return

To be totally correct, I should say that you shouldn't loop under hotkeys and should use a timer instead, but I wont add that in to confuse you. This should be fine as well, if you have problems with hotkeys after this, you can look into a timer approach yourself or ask help here.

Related