为什么我的罗技 G Lua 脚本会出现语法错误?

我一直在学习 Lua,以编写一个自动点击器,可以在右键保持按下时工作,并可以通过大写锁定键进行切换。 希望能得到任何帮助或见解。

EnablePrimaryMouseButtonEvents(true);
function OnEvent(event,arg)=true
    if IsKeyLockOn("capslock"=true then
        if IsMouseButtonPressed(3)=true then
            repeat
                if IsMouseButtonPressed(1)=true then
                    repeat
                        PressAndReleaseMouseButton(1)
                        Sleep(math.random(20,80))
                    until IsMouseButtonPressed(1)=false
                end
            until IsMouseButtonPressed(3)=false
        end
    end
end

原文链接 https://stackoverflow.com/questions/69565393

点赞
stackoverflow用户2858170
stackoverflow用户2858170

function OnEvent(event,arg) 是一个函数定义的开始。你不能在 function OnEvent(event,arg)=true 中将 true 赋值,这样会导致出现“未预期的符号”错误。Lua 无法理解这一点。

if IsKeyLockOn("capslock"=true then 中,你缺少一个右括号。在添加之后,以下所有行仍然不正确:

if IsKeyLockOn("capslock")=true then
if IsMouseButtonPressed(3)=true then
if IsMouseButtonPressed(1)=true then

你会得到一个错误,“then”需要与“=” 搭配使用。

until IsMouseButtonPressed(1)=false
until IsMouseButtonPressed(3)=false

你会得到一个错误,“until”需要与“” 搭配使用。

你将赋值运算符 = 与相等运算符 == 混淆了。

请参阅 关系运算符赋值运算

作为一个副产品,这些函数已经返回 true 或 false。

你不需要显式检查返回值是否等于 true。只有两种可能的结果。true == true -> truefalse == true -> false。因此,你可以直接使用返回值并简单地写 if IsMouseButtonPressed(1) then

如果你想在 false 情况下做一些事情,通常的做法是直接否定返回值。not false -> true

在这种情况下,你只需写 if not IsMouseButtonPressed(1) then,而不是写 if IsMouseButtonPressed(1) == false then

2021-10-14 06:49:13