【问题标题】:how do i add an end finction after x amount of time in lua如何在lua中x时间后添加结束函数
【发布时间】:2022-01-23 09:21:05
【问题描述】:

我试图让鼠标继续平滑移动,而不是使用 moverelative。我希望鼠标能够以不同的速度向任何方向移动,并在例如 150 毫秒后停止,然后立即开始向另一个方向移动 150 毫秒,依此类推。所以我不认为睡眠会起作用。这是我到目前为止的lua代码。我从旧帖子中的其他人那里得到了这段代码,但不知道如何根据我的需要修改它

do
   local frac_x, frac_y, prev_time = 0, 0

   function StartMoving()
      prev_time = GetRunningTime()
   end

   function MoveMouseForAWhile(x, y)
      Sleep(1)
      local time = GetRunningTime()
      time, prev_time = time - prev_time, time
      frac_x, frac_y = frac_x + time * x, frac_y + time * y
      x, y = math.floor(frac_x), math.floor(frac_y)
      frac_x, frac_y = frac_x - x, frac_y - y
      while x ~= 0 or y ~= 0 do
         local dx = math.min(127, math.max(-127, x))
         local dy = math.min(127, math.max(-127, y))
         x, y = x - dx, y - dy
         MoveMouseRelative(dx, dy)
      end
   end

end


EnablePrimaryMouseButtonEvents(true)

function OnEvent(event,arg)
   if IsKeyLockOn("scrolllock")then
      if IsMouseButtonPressed(3)then
         repeat
            if IsMouseButtonPressed(1) then
               local speed = 1.5
               StartMoving()
               repeat
                  MoveMouseForAWhile(-0.25 * speed, .35 * speed)
               until not IsMouseButtonPressed(1)
            end
         until not IsMouseButtonPressed(3)
      end
   end
end

【问题讨论】:

    标签: lua logitech-gaming-software


    【解决方案1】:

    我希望鼠标能够以不同的速度向任何方向移动,并在例如 150 毫秒后停止,然后立即开始向另一个方向移动 150 毫秒,依此类推。

    do
       local frac_x, frac_y, start_time, prev_time = 0, 0
    
       function StartMoving()
          prev_time = GetRunningTime()
          start_time = prev_time
       end
    
       function MoveMouseForAWhile(x, y)
          Sleep(1)
          local time = GetRunningTime()
          time, prev_time = time - prev_time, time
          frac_x, frac_y = frac_x + time * x, frac_y + time * y
          x, y = math.floor(frac_x), math.floor(frac_y)
          frac_x, frac_y = frac_x - x, frac_y - y
          while x ~= 0 or y ~= 0 do
             local dx = math.min(127, math.max(-127, x))
             local dy = math.min(127, math.max(-127, y))
             x, y = x - dx, y - dy
             MoveMouseRelative(dx, dy)
          end
          return prev_time - start_time
       end
    end
    
    local sequence = {
       {
          duration = 150,  -- milliseconds
          speed = 1.5,     -- pixels per 1 millisecond
          direction = 30,  -- degrees (0 = right, 90 = up, 180 = left, 270 = down)
       },
       {
          duration = 150,  
          speed = 3,     
          direction = -60, 
       },
    }
    
    function OnEvent(event,arg)
       if event == "PROFILE_ACTIVATED" then
          EnablePrimaryMouseButtonEvents(true)
       elseif event == "MOUSE_BUTTON_PRESSED" and arg == 1 and IsMouseButtonPressed(3) and IsKeyLockOn("scrolllock") then
          for _, seq in ipairs(sequence) do
             local angle = math.rad(seq.direction)
             local speed_x, speed_y = math.cos(angle) * seq.speed, -math.sin(angle) * seq.speed
             StartMoving()
             repeat
                local time = MoveMouseForAWhile(speed_x, speed_y)
             until time >= seq.duration
             if not IsMouseButtonPressed(1) then return end
          end
       end
    end
    

    打开 ScrollLock 并在按住 RMB 的同时按 LMB 以测试脚本。
    释放 LMB 以停止脚本。

    【讨论】:

    • 哇,你的速度太快了,这是我在想哈哈之前找到的代码。谢谢。题。我可以让它只有在按下左右鼠标时才会激活?另外,如果我想要更多的鼠标移动,我是否只需添加更多{sequences}?非常感谢
    • 所以我想我想出了如何添加更多序列。我现在只是想知道如何让它仅在按下鼠标左键和右键时才激活。谢谢
    • 哦,还有一件事我忘记了,对不起。释放鼠标按钮后,有没有办法让序列提前结束?这段代码几乎正是我在寻找这两个异常时所寻找的(仅在按下鼠标右键然后按下鼠标左键时激活,并且仅在两者都被主动按下并在释放左右时提前结束序列时才通过序列鼠标。)谢谢
    • 你真是太棒了!超级快,正是我想要的!非常有天赋,你能帮我解决这个问题真是太好了。再次非常感谢!
    • 嘿,你做的已经够多了。我想问一下它是否没有太多麻烦(如果不用担心的话)我知道我说过一旦鼠标按下释放位置就结束序列。有没有办法完成当前的序列步骤?因此,例如,如果我在指定的持续时间内释放鼠标按钮 10 或 80 或任何 ms 值,它将完成这一步,但不会完成序列列表的其余部分。如果那有意义的话?如果它很麻烦不用担心
    猜你喜欢
    • 2013-03-07
    • 1970-01-01
    • 2015-07-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-31
    • 2018-06-13
    相关资源
    最近更新 更多