AHK帮忙。反正有没有提高效率?

时间:2017-02-13 08:06:45

标签: autohotkey

稍微有点背景,脚本在窗口中找到一个图像然后按下相应的键以在该活动窗口中执行命令。它做的工作,但让我有点滞后。现在它必须一次一行地进行,每个循环可能需要1.5到2秒。有没有办法让脚本一次查找所有图像?然后当它识别出其中一个图像时,将相应的密钥发送到窗口。我宁愿脚本更快更可靠。组织方面的任何提示都将受到高度赞赏。到目前为止,这是我的代码。

  IfWinActive, xx{
Loop
{
    CoordMode, Pixel, Window
    ImageSearch, FoundX, FoundY, 0, 0, 1920, 1080, C:\retreat_F.png
    If ErrorLevel = 0
    {
        Send, {f}
    }
    CoordMode, Pixel, Window
    ImageSearch, FoundX, FoundY, 0, 0, 1920, 1080, *5 C:\knee_F.png
    If ErrorLevel = 0
    {
        Send, {f}
    }
    CoordMode, Pixel, Window
    ImageSearch, FoundX, FoundY, 0, 0, 1920, 1080, C:mighty_cleave_F.png
    If ErrorLevel = 0
    {
        Send, {f}
    }
    CoordMode, Pixel, Window
    ImageSearch, FoundX, FoundY, 0, 0, 1920, 1080, *7 C:\uppercut_4.png
    If ErrorLevel = 0
    {
        Send, {4}
    }
    CoordMode, Pixel, Window
    ImageSearch, FoundX, FoundY, 0, 0, 1920, 1080, *5 C:\piledrive_F.png
    If ErrorLevel = 0
    {
        Send, {f}
    }
    CoordMode, Pixel, Window
    ImageSearch, FoundX, FoundY, 0, 0, 1920, 1080, C:\moltenkup_C.png
    If ErrorLevel = 0
    {
        Send, {c 2}
    }
}
}

2 个答案:

答案 0 :(得分:0)

你可以使代码更短 - 你每次都做同样的事情,只是使用不同的参数。将参数放在一个数组中并使用循环:
你每次都在调用CoordMode,而你也不需要。{/ p>

#SingleInstance force
#Persistent

mappings := [{imgsearch: "C:\retreat_F.png", send: "{f}"}
            ,{imgsearch: "*5 C:\knee_F.png", send: "{f}"}
            ,{imgsearch: "C:mighty_cleave_F.png", send: "{f}"}
            ,{imgsearch: "C:\moltenkup_C.png", send: "{c 2}"}]

CoordMode, Pixel, Window    ; You only need this once, since you do not change mode
;IfWinActive, xx{
Loop
{
    for i, mapping in mappings {
        ImageSearch, FoundX, FoundY, 0, 0, 1920, 1080, % mapping.imgsearch
        If ErrorLevel = 0
        {
            Send, % mapping.send
        }
    }
    Sleep 10    ; You should probably have a sleep in your loop to avoid high CPU usage
}
;}

答案 1 :(得分:0)

尝试用自定义函数Gdip_ImageSearch替换ImageSearch。

ImageSearch每次都会捕获图像,而Gdip_ImageSearch不会捕获图像。

因此,使用Gdip_ImageSearch可以减少延迟。