3
votes

I don't understand the difference between Autohotkey's If and If(...)
According to everything I have found, If(...) behaves as "expected" but there is something not working with my code.

Below does not work. It seems the statement in the If is never evaluated, %TimeString% is never set and nothing is output:

CapsLock & T::
    Input Key, L1
    If (Key=T)
    {
        FormatTime, TimeString,, HHmm
    }
    Send %TimeString%   

Below does work, %TimeString% is set and the time is output.

CapsLock & T::
    Input Key, L1
    If Key=T
        FormatTime, TimeString,, HHmm
    Send %TimeString%   
2

2 Answers

5
votes

Autohotkey has two different syntaxes: legacy and expression. This also affects the if statement.

When you use parenthesis, if (expression) is used and Key is compared to the variable T which doesn't exist and is the same as an empty variable which doesn't equal T. You need to changed it to If (Key="T") and then it will compare the variable Key to the String "T" and it will work.

In the second case you're using the traditional(legacy) If which compares the variable Key to the String T and because they are equal, it works.

The curly braces { } just define a block, they do nothing and change nothing when your block contains only one line.

1
votes

Are you sure this code is identical to your script? Beause

Tjs := T
if (Tjs=T)
{
   MsgBox true
}

works fine for me.