【问题标题】:How can I make this Lua script more reliable?我怎样才能使这个 Lua 脚本更可靠?
【发布时间】:2023-01-05 20:41:20
【问题描述】:

所以我一直在努力让我在 LogitechGHUB 中的 Lua 脚本变得更好,这样它们就不会跳过几个镜头或突然停止工作几秒钟,最初我制作了这段代码但无论我做了什么仍然随机失败

EnablePrimaryMouseButtonEvents(true);
function OnEvent(event,arg)
 if IsKeyLockOn(LockKey)then
            if IsMouseButtonPressed(RC) then 
                repeat 
                           if IsMouseButtonPressed(LC) then 
             repeat                 
                     MoveMouseRelative(0,11)
                     if (coun2<2 and IsMouseButtonPressed(LC))
                     then 
                         MoveMouseRelative(3,13)
                         end
                     if (coun2>10 and coun2<25 and IsMouseButtonPressed(LC))
                     then 
                         MoveMouseRelative(0,1)
                         end
                     if (coun2>35 and coun2<55 and IsMouseButtonPressed(LC))
                     then 
                         MoveMouseRelative(1,0)
                         end
                      if (coun2>65 and coun2<75 and IsMouseButtonPressed(LC))
                     then 
                         MoveMouseRelative(1,1)
                         end
                     if (coun2>85 and IsMouseButtonPressed(LC))
                     then 
                         MoveMouseRelative(1,1)
                         end    
                     Sleep(1)
                     coun2 = coun2+1
             until not IsMouseButtonPressed(LC)
             coun2=0
         end  

                 until not IsMouseButtonPressed(RC)
                end
end
end
LockKey="numlock"                       
coun2 = 0
LC=1                                        
RC=3                                

我改变了使用计数器的想法,使其更可定制,对于这样的循环

EnablePrimaryMouseButtonEvents(true)

function OnEvent(event, arg)

   if event == "MOUSE_BUTTON_PRESSED" and arg == 1 and IsMouseButtonPressed(3) and IsKeyLockOn("numlock") then
      for i = 1, 2 do
         MoveMouseRelative(3,20)
         Sleep(1)
         if not IsMouseButtonPressed(1) then return end
      end
      
      for i = 1, 135 do
         MoveMouseRelative(1,12)
         Sleep(1)
         if not IsMouseButtonPressed(1) then return end
      end
end
end                             

只要我没有使用太多 for 循环,这实际上确实使它更加一致,但它仍然偶尔会在随机时间段内停止移动鼠标。我尝试更改我看到另一个用户推荐的 FastSleep() 的 Sleep() 函数,但它保持不变只是更快。有没有一种方法可以使脚本更少地失败或像我不理解的东西那样弄乱代码?或者我应该简单地尝试一种不同的编码语言?

【问题讨论】:

  • 您可能希望将睡眠时间增加到一个合理的值。 1ms 是废话。您的鼠标按钮应该如何在一毫秒内切换状态?想一想 1/1000 秒对于人类运动意味着什么。特别是在非实时环境中。您还应该解释您的脚本应该做什么以及究竟是什么让您认为它失败了。我的意思是我们在这里谈论几毫秒内发生的事情。
  • 我会尝试改变它,最初我使用了更长的睡眠时间,但我认为这是它失败的原因。在这里失败我指的是在按下左键单击时没有拉下鼠标,就像有时它没有达到预期目的一样。

标签: lua scripting


【解决方案1】:

尝试

local LockKey = "numlock"                       

local moves = {  -- dx/msec, dy/msec, msec
   {0.3, 2.0, 30},
   {0.0, 1.1, 100},
   {0.0, 1.2, 150},
   {0.0, 1.1, 100},
   {0.1, 1.1, 200},
   {0.0, 1.1, 100},
   {0.1, 1.2, 100},
   {0.0, 1.1, 100},
   {0.1, 1.2, math.huge},
}

local function get_distance(t)
   local x, y = 0, 0
   for _, move in ipairs(moves) do
      local vx, vy, mt = move[1], move[2], move[3]
      if t < mt then
         mt = t
      end
      x, y = x + vx*mt, y + vy*mt
      t = t - mt
      if t <= 0 then break end
   end
   return math.floor(x), math.floor(y)
end

function OnEvent(event, arg)
   if event == "PROFILE_ACTIVATED" then
      EnablePrimaryMouseButtonEvents(true)
   elseif event == "MOUSE_BUTTON_PRESSED" and arg == 1 
      and IsMouseButtonPressed(3) and IsKeyLockOn(LockKey) 
   then
      local t0 = GetRunningTime()
      local t = t0
      repeat
         Sleep(10)
         local oldx, oldy = get_distance(t - t0)
         t = GetRunningTime()
         local newx, newy = get_distance(t - t0)
         MoveMouseRelative(newx - oldx, newy - oldy)
      until not IsMouseButtonPressed(1)
   end
end

【讨论】:

  • 嘿,很抱歉再次打扰你,但你知道错误 ` [string "LuaVM"]:18: attempt to index a number value (local 'move') Line Number:1 ` 是什么意思吗?我可能应该编辑一些东西才能让它发挥作用,但当你发送它时我真的迷路了
  • 对不起,是我的错。修复:for _, move in ipairs(moves) do
猜你喜欢
  • 1970-01-01
  • 2021-10-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-28
  • 1970-01-01
  • 2017-12-20
相关资源
最近更新 更多