如何提高这个Lua脚本的可靠性?

我一直在尝试改进我的Logitech GHUB中的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循环,但它仍然偶尔会在随机时间停止移动鼠标。我尝试将Sleep()函数更改为另一个用户推荐的FastSleep(),但它仍然保持不变,只是加快了。有没有办法使脚本不那么容易失败或者我理解有问题,导致代码混乱?还是我应该尝试一种不同的编程语言?

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

点赞
stackoverflow用户1847592
stackoverflow用户1847592

尝试

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
2022-02-02 19:17:56