反向 MoveMouseRelative Lua 编码

Reverse MoveMouseRelative Lua Codding

function OnEvent(event, arg)

  OutputLogMessage("event = %s, arg = %d\n", event, arg)
  if event == "PROFILE_ACTIVATED" then
    EnablePrimaryMouseButtonEvents(true)
  elseif event == "PROFILE_DEACTIVATED" then
    ReleaseMouseButton(2) -- to prevent it from being stuck on
  elseif event == "MOUSE_BUTTON_PRESSED" 
               and (arg == 5 or arg == 4) then 
    recoil = recoil ~= arg and arg
  elseif event == "MOUSE_BUTTON_PRESSED" 
               and arg == 1 and recoil == 5 then
    MoveMouseRelative(0, -3) 
    for i = 1, 17 do
      MoveMouseRelative(0, 2)  
      Sleep(15)
      if not IsMouseButtonPressed(1) then return end

    end

  elseif event == "MOUSE_BUTTON_PRESSED" 
               and arg == 1 and recoil == 4 then
    MoveMouseRelative(0, -3) 
    for i = 1, 35 do
      Sleep(15)
      MoveMouseRelative(0, 2)       
      if not IsMouseButtonPressed(1) then return end
    end
    if not IsMouseButtonPressed(1) then return end
  end
end

这是 Lua 脚本,我想知道如何获得鼠标的初始位置,然后 return 到初始位置。

我尝试在脚本底部添加 MoveMousePosition(x,y)- (32767, 32767) 但在游戏中不起作用。仅在桌面上..

我只想在 MoveMouseRelative 之后释放鼠标单击到 return 中心或第一个位置。

要撤消任何操作,我们需要一个 stack,您可以在其中记住您所做的一切。但是由于我们只有一个位置,因此顺序无关紧要,我们使用一个简单的数字来存储移动的总数 x 和 y。

local movedX = 0
local movedY = 0
function move(x, y)
    MoveMouseRelative(x, y)
    movedX = movedX + x
    movedY = movedY + y
end

现在你使用例如仅 move(0, 2)

要撤消,我们反其道而行之;因为我们只有减去一个数字。

function undo()
    MoveMouseRelative(-movedX, -movedY)
    movedX = 0
    movedY = 0
end

无关,但在您的循环中,不要使用 return,而是使用 break。这样你就可以到达事件的结尾并在那里添加一个 undo()

正如Luke100000所说,你需要一个“撤消”运动(相同的距离与相反的符号)。

function OnEvent(event, arg)
   OutputLogMessage("event = %s, arg = %d\n", event, arg)
   if event == "PROFILE_ACTIVATED" then
      EnablePrimaryMouseButtonEvents(true)
   elseif event == "MOUSE_BUTTON_PRESSED" and (arg == 5 or arg == 4) then
      recoil = recoil ~= arg and arg
   elseif event == "MOUSE_BUTTON_PRESSED" and arg == 1 and recoil == 5 then
      MoveMouseRelative(0, -3)
      local n = 0
      for i = 1, 17 do
         n = n + 1
         MoveMouseRelative(0, 2)
         Sleep(15)
         if not IsMouseButtonPressed(1) then break end
      end
      repeat  -- wait for LMB release
      until not IsMouseButtonPressed(1)
      for i = 1, n do
         MoveMouseRelative(0, -2)
      end
      MoveMouseRelative(0, 3)
   elseif event == "MOUSE_BUTTON_PRESSED" and arg == 1 and recoil == 4 then
      MoveMouseRelative(0, -3)
      local n = 0
      for i = 1, 35 do
         Sleep(15)
         n = n + 1
         MoveMouseRelative(0, 2)
         if not IsMouseButtonPressed(1) then break end
      end
      repeat  -- wait for LMB release
      until not IsMouseButtonPressed(1)
      for i = 1, n do
         MoveMouseRelative(0, -2)
      end
      MoveMouseRelative(0, 3)
   end
end