Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

autohotkey if statement with and without curly braces

Tags:

autohotkey

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%   
like image 402
LosManos Avatar asked Nov 19 '25 20:11

LosManos


1 Answers

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.

like image 117
Oleg Avatar answered Nov 22 '25 05:11

Oleg



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!