带和不带花括号的 autohotkey if 语句

autohotkey if statement with and without curly braces

我不明白 Autohotkey 的 IfIf(...)
之间的区别 根据我发现的所有内容,If(...) 的行为与 "expected" 相同,但我的代码有些问题。

以下无效If 中的语句似乎从未被评估过,%TimeString% 从未被设置并且没有任何输出:

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

下面是否起作用,设置%TimeString%并输出时间

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

Autohotkey 有两种不同的语法:legacy and expression. This also affects the if statement.

当您使用括号时,将使用 if (expression) 并将 Key 变量 T 不存在并且与不等于 T 的空变量相同。您需要将其更改为 If (Key="T"),然后它将变量 Key 与字符串 "T" 进行比较,它会起作用。

在第二种情况下,您使用的是传统(遗留)If,它将变量 Key 与字符串 T 进行比较,因为它们相等,所以它起作用了。

花括号 { } 只是定义一个块,当您的块只包含一行时,它们什么也不做,什么也不改变。

您确定此代码与您的脚本相同吗?因为

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

对我来说很好。