什么时候在 AutoHotKey 中使用 % 或 %variable% ? [英] When to use % or %variable% in AutoHotKey?

查看:32
本文介绍了什么时候在 AutoHotKey 中使用 % 或 %variable% ?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经搜索了很多,但没有找到答案.在 AutoHotKey 中,我不确定在行首附近使用单个百分比和两个百分号之间包含变量时的区别.我通常使用反复试验来找出我何时使用其中一个,我希望有人可以阐明其中的区别或解释它实际在做什么.

I have searched around quite a bit and have failed to find an answer. In AutoHotKey, I am not sure the difference when a single percent is used near the beginning of a line, and when a variable is enclosed between two percent signs. I usually use trial and error to find when I use one or the other, I am hoping someone could shed some light on what the difference is or explain what it is actually doing.

这里有一些实际的例子.

Here are some examples of this in action.

示例 1:我注意到,如果您有多个变量和文本,脚本倾向于使用前面的百分比.如:

Example 1: I noticed if you have multiple variables along with text, scripts tend to go with the preceding percent. Such as:

some_val := Clipboard

loop 5
    msgbox % "Index:" . A_Index . ", variable:" . some_val 

示例2:我也经常看到这个,有时似乎必须使用它.这是真的吗?

Example 2: I often see this as well, and sometimes it appears it must be used. Is this true?

some_variable := "test text to send"

Send, %some_variable%

推荐答案

用双百分号包裹是遗留的 AHK,基本上没有必要再使用它了.用双 % 换行的唯一原因是在过去会被卡在后面,或者也许有人会争辩说在某些情况下它也更方便或其他东西,但我不买它.
旧语法被表达式语法取代.
表达式语法更接近于许多其他语言的行为.AHK 遗留语法真的是一团糟.

Wrapping in double percent signs is legacy AHK and basically there is no need to ever use it anymore. Only reason to wrap in double % would be being stuck behind in the old times, or maybe one could argue it also being more convenient, or something, to write in some cases, but I'm not buying it.
The legacy syntax is replaced by expression syntax.
The expression syntax is closer to how many other languages behave. AHK legacy syntax really is a mess.

所有旧命令(例如 MsgBox)在每个参数上都使用旧的旧语法(除非另有说明).
如果在参数开头指定 % 后跟一个空格,则会强制 AHK 计算该参数上的表达式,而不是将其作为旧文本参数读取.
示例:
MsgBox, 5+5我们使用的是遗留命令,我们没有以 % 和空格开始参数,所以我们使用的是遗留语法.MsgBox 将打印文字文本 5+5 而不是 10.
MsgBox, % 5+5​​
同样,遗留命令,但现在我们强制 AHK 在这里评估表达式,5+5.
表达式的计算结果将传递给 MsgBox 命令,MsgBox 将打印 10.
如果我们想让 MsgBox 打印文字文本 5+5,并使用表达式语法来做,我们会做 MsgBox, % "5+5".
表达式语法中的引号表示我们正在指定一个字符串.

All legacy commands (MsgBox for example) use the old legacy syntax on each parameter (unless otherwise specified).
If you specify a % followed up by a space at the start of the parameter, you're forcing AHK to evaluate an expression on that parameter instead of reading it as a legacy text parameter.
Example:
MsgBox, 5+5 We're using a legacy command, we're not starting the parameter off with a % and a space, so we're using the legacy syntax. The MsgBox is going to print the literal text 5+5 instead of 10.
MsgBox, % 5+5
Again, legacy command, but now we're forcing AHK to evaluate an expression here, 5+5.
The result of expression's evaluation is going to be passed onto the MsgBox command and the MsgBox is going to print 10.
If we wanted to MsgBox to print the literal text 5+5, and use the expression syntax to do it, we'd do MsgBox, % "5+5".
Quotation marks in expression syntax mean we're specifying a string.

那么问题来了,知道什么时候使用表达式语法,什么时候使用遗留语法.
默认情况下,您基本上总是处于表达式中.
例如,您可以使用命令或 = 来分配.
如果您不清楚命令和函数之间的区别,这里有一个例子:
命令,% 7+3,% MyCoolArray[4],% SomeOtherNiceFunction(),% false
函数(7+3, MyCoolArray[4], SomeOtherNiceFunction(), false)
在命令中,我们指定了一个 % 后跟一个空格来计算每个参数上的表达式,而在函数中,我们不必这样做,因为我们已经在表达式中了.

Well then there's the problem of knowing when you're in expression syntax, and when you're in the legacy syntax.
By default, you're basically always in an expression.
You leave it by for example using a command or = to assign.
If the difference between a command and a function isn't clear to you, here's an example:
Command, % 7+3, % MyCoolArray[4], % SomeOtherNiceFunction(), % false
Function(7+3, MyCoolArray[4], SomeOtherNiceFunction(), false)
In the command we specified a % followed up by a space to evaluate the expressions on each parameter, and in the function, we didn't have to do that since we're already in an expression.

如果您不清楚 =:=,
= 是旧的并且已弃用,它将纯文本分配给变量
:= 将表达式的结果赋给一个变量.

And if you're not clear on the difference between = and :=,
= is legacy and deprecated, it assigns plain text to a variable
:= assigns the result of an expression to a variable.

所以这就是我可以从头顶上写的东西.
如果你有一些更复杂的例子,我可以尝试展示它们.也许将一些您可能拥有的代码转换为表达式语法,使其 100% 摆脱遗留语法.

So that's what I could write from on top of my head.
If you had some more complex examples, I could try showing on them. Maybe convert some code you may have over to expression syntax, make it 100% free of legacy syntax.

这里有一个关于文档的好页面,您应该阅读一下:
https://www.autohotkey.com/docs/Language.htm

And here's a good page on the documentation you should give a read:
https://www.autohotkey.com/docs/Language.htm

这篇关于什么时候在 AutoHotKey 中使用 % 或 %variable% ?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆