AHK在这里做什么?我弄错了什么? [英] What is AHK Doing Here? And What did I get Wrong?

查看:162
本文介绍了AHK在这里做什么?我弄错了什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个脚本,该脚本定期读取各种坐标处的像素,并根据给定的文件out.txt将它们与给定的十六进制颜色进行比较.格式为x-cord y-cord color.

I'm trying to make a script that periodically reads the pixels at various coordinates and compares them to a given hex color based on a given file out.txt. The format is x-cord y-cord color.

Loop
{
    if (GetKeyState("Space"))
    {
        Loop, Read, out.txt
        {
            tgt := StrSplit(%A_LoopReadLine%,%A_Space%)
            PixelGetColor hue, tgt[0], tgt[1]
            if (%hue% != tgt[2])
            {
                Click tgt[0], tgt[1]
            }
        }
    }
        Sleep 10
}

产生此错误,因为您无法选择文本,所以截图:

produces this error, screenshot because you cant select the text:

它说变量名包含一个非法字符,后跟应该是文件中的一行的行,在该行中我试图将该值分配给数组,所以我不确定自己在做什么错.我检查了似乎相关的所有部分的ahk语法,但没有发现任何问题.看起来就像只是设置一个变量.我在这里找不到错误.

It says the variable name contains an illegal character, followed by what should be a line in the file, where I'm trying to assign that value to an array so I'm not sure what I'm doing wrong. I reviewed ahk syntax in all the parts that seem relevant and nothing pops out at me. It looks like just setting a variable. I can't find the error here.

推荐答案

您基本上对已过时的旧式语法和现代表达式语法感到困惑.
通过将其包装在%中来引用变量是您在旧版AHK中所做的.
但是,这是遗产,基本上不再应该这样做.
在表达式中,只需输入变量名称即可引用它.

You're basically just confused about the deprecated legacy syntax and the modern expression syntax.
Referring to a variable by wrapping it in % is what you'd do in legacy AHK.
However, that's legacy and basically should never be done anymore.
In an expression, you refer to a variable by just typing its name.

如果您使用的是旧式语句(基本上只是命令中的参数),则可以通过以单个%开头的参数后跟一个空格来切换到表达式语法.您将在下面看到此示例.

If you are in a legacy statement (basically just parameters in commands), you switch over to the expression syntax by starting off the parameter with a single % followed up by a space. You'll see examples of this below.

我将逐行记录更改内容,以修复您的代码:

I'll fix your code line by line documenting the changes:

在这里,我强迫AHK解释filename参数上的表达式,只是用""显式指定一个字符串.
这不是必需的,并且在实践中没有任何区别,但是如果您问我,它看起来要好得多.特别是如果我们要使用更长,更复杂的字符串.将消除例如转义某些字符等.
Loop, Read, % "out.txt"

Here I force AHK to interpret an expression on the filename parameter just to explicitly specify a string with "".
This is not needed and makes no difference in practice, but it looks a lot better if you ask me. Especially if we were to have a longer and more complex string. Would eliminate the need of e.g. escaping certain characters etc.
Loop, Read, % "out.txt"

在这里,我删除了%,因为我们位于函数的中参数.
函数不是旧版AHK,而是使用现代表达式语法,因此我们只需输入变量名称即可引用变量.
tgt := StrSplit(A_LoopReadLine, A_Space)

Here I removed the %s, because we're in function's parameters.
Functions aren't legacy AHK and they use the modern expression syntax, so we refer to variables by just simply typing their name.
tgt := StrSplit(A_LoopReadLine, A_Space)

现在,我们再次使用命令.默认情况下,所有命令在每个参数上都使用旧式语法(除非文档中另有说明).
PixelGetColor 实际上恰好是那些可以接受表达式的命令之一在x和y参数上,因此实际上不需要通过使用% 开始参数来在此处强制表达式.
但这并没有伤害,因此我将这样做以使其更清晰并显示您通常使用命令执行的操作.
PixelGetColor, hue, % tgt[0], % tgt[1]

Now we're again using a command. All commands use the legacy syntax on each parameter by default (unless otherwise specified in the documentation).
PixelGetColor actually happens to be one of those commands which would accept an expression on the x and y parameters, so me forcing an expression here by starting off the parameter with % isn't actually needed.
But it doesn't hurt, so I'll just do it to make it clearer and make it show what you'd normally do with commands.
PixelGetColor, hue, % tgt[0], % tgt[1]

如果不是不使用旧式语法的传统,因此%%被删除.
if (hue != tgt[2])

The non-legacy if doesn't use the legacy syntax, so %% gets removed.
if (hue != tgt[2])

Click 命令不接受表达式,因此我们必须用% 强制它.
Click, % tgt[0] ", " tgt[1]
现在,如果您在上面注意了,您应该想知道为什么我没有这样做:
Click, % tgt[0], % tgt[1]
这将是一个非常好的问题.如果实际上是除Click命令以外的任何其他命令,则将执行此操作.
之所以不使用点击"命令,是因为它很特殊(无论好坏).它实际上没有不同的参数,只有一个字符串参数.该参数包含所有必需的单词,逗号和数字.
一个单独的字符串参数就是tgt[0] ", " tgt[1]创建的.它将这两个值与字符串逗号和空格连接起来.

The Click command doesn't accept expressions, so we have to force it with a % .
Click, % tgt[0] ", " tgt[1]
And now if you were paying attention above, you should wonder why I didn't do this:
Click, % tgt[0], % tgt[1]
And that would be a very good question. You would do exactly that if it was literally any other command but the Click command.
The reason why you don't do that with the Click command, is because it's special (for good or for worse). It doesn't actually have different parameters, it just has one single string parameter. That one parameter contains all the required words, commas, and numbers.
And one single string parameter is what tgt[0] ", " tgt[1] creates. It concatenates those two values with the string comma and space.

所以这是完整的脚本:

Loop
{
    if (GetKeyState("Space"))
    {
        Loop, Read, % "out.txt"
        {
            tgt := StrSplit(A_LoopReadLine, A_Space)
            PixelGetColor, hue, % tgt[0], % tgt[1]
            if (hue != tgt[2])
            {
                Click, % tgt[0] ", " tgt[1]
            }
        }
    }
    Sleep, 10
}

要了解有关旧版vs表达式的更多信息,这是一个非常不错的文档页面,可帮助您入门:
https://www.autohotkey.com/docs/Language.htm

To learn more about legacy vs expression, here's a pretty good documentation page to get you started:
https://www.autohotkey.com/docs/Language.htm

这篇关于AHK在这里做什么?我弄错了什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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