反向 MoveMouseRelative Lua 编码

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) -- 防止鼠标卡住
  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 脚本,我想知道如何获取鼠标的初始位置并在移动后将其返回到初始位置。

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

我只想在释放鼠标单击后将其返回到中心或最初位置。

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

点赞
stackoverflow用户10391157
stackoverflow用户10391157

为了撤销任何操作,我们需要使用一个 堆栈 来保留你所做的所有操作。但由于我们只有一个位置,因此顺序并不重要,我们只需使用一个简单的数字来存储移动的总 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()

2021-10-05 17:51:20
stackoverflow用户1847592
stackoverflow用户1847592

如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  -- 等待鼠标左键释放
      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  -- 等待鼠标左键释放
      until not IsMouseButtonPressed(1)
      for i = 1, n do
         MoveMouseRelative(0, -2)
      end
      MoveMouseRelative(0, 3)
   end
end
2021-10-10 20:01:16