如何在 lua 中添加一个结束函数一段时间后

我正在尝试让鼠标平稳地运动,而不是使用 moverelative。我希望鼠标能够以不同的速度在任意方向上移动,并在运动 150 毫秒后停止,然后立即开始在另一个方向上移动另外 150 毫秒,以此类推。所以我认为 sleep 不起作用。这是我目前为止的 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

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

点赞
stackoverflow用户1847592
stackoverflow用户1847592

我希望鼠标能以不同的速度向任意方向移动,并在例如150ms后停止,然后立即按另一个方向移动另外150ms,依此类推。

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,  -- 毫秒
      speed = 1.5,     -- 每毫秒的像素数
      direction = 30,  -- 角度(0 = 右,90 = 上,180 = 左,270 = 下)
   },
   {
      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

打开滚动锁定,按住RMB,同时按下LMB以测试脚本。

松开LMB以停止脚本。

2021-12-22 04:59:34