尝试缩短 Lua 代码

Trying to shorten Lua code

有没有办法缩短这段代码?

if (event == "MOUSE_BUTTON_PRESSED" and arg == 1) then
PressKey ("a")
Sleep (50)
if not IsMouseButtonPressed(1) then
ReleaseKey ("a")
return
end
PressKey ("a")
Sleep (200)
if not IsMouseButtonPressed(1) then
ReleaseKey ("a")
return
end
...
next all the same with sleep values only changing

我想使用 repeat-until,但我不能这样做,因为睡眠值正在改变。有没有办法将睡眠值保存在 table(即 50、200、100、75、25、200)中,以便我可以在代码中使用 Repeat-Until?我一直在尝试搜索,但我是 Lua 的新手。感谢任何帮助,谢谢

因为你想在超时范围内循环,所以我不会使用 repeat until 而是 for 循环。最重要的是,您应该在完成列表后通知调用者所有超时都失败了。

if (event == "MOUSE_BUTTON_PRESSED" and arg == 1) then
    for _,duration in ipairs{50, 200, 100, 75, 25, 200} do
        PressKey ("a")
        Sleep (duration)
        if not IsMouseButtonPressed(1) then
            ReleaseKey ("a")
            return
        end
    end
    return "ERROR" -- You should somehow indicate timeout to the caller
end