How to reverse the MoveMouseRelative to exact first position when releasing LMB

梦想的初衷 提交于 2020-05-17 05:57:28

问题


I am trying to make a script for Logitech mouse that:

  1. Step 1: When LMB is pressed -> do recoil pattern
  2. Step 2: When LMB is released -> MoveMouseRelative to exact position before pressed

So is there any way to try out step 2? This is the script:

local Recoil_Pattern = { 
    {x = 0  ,y = 10 }   ,--1st Shot 
    {x = 0  ,y = 10 }   ,--2nd Shot  (hold LMB 100ms)
    {x = 0  ,y = 10 }   ,--3rd Shot (hold LMB 200ms)
    {x = 0  ,y = 10 }   ,--4rd Shot (hold LMB 300ms)
    {x = 0  ,y = 10 }   ,--5rd Shot (hold LMB 400ms)
}
local Shot_Sleep = 100

EnablePrimaryMouseButtonEvents(true);
function OnEvent(event, arg)
        if event == "MOUSE_BUTTON_PRESSED" and arg == 1 then
            if (IsMouseButtonPressed(1)) then 
                for i = 1, #Recoil_Pattern do
                    Sleep(10)
                    if IsMouseButtonPressed(1) then
                        Sleep(Shot_Sleep)
                        MoveMouseRelative( Recoil_Pattern[i].x, Recoil_Pattern[i].y )       
                    end
                end
            end

        elseif event == "MOUSE_BUTTON_RELEASED" and arg == 1 then
            if not (IsMouseButtonPressed(1)) then 
                Sleep(10)
                -- How to reverse the MoveMouseRelative to exact first position when releasing LMB
                    -- Ex.
                        -- If press and release LMB -> move mouse relative (0,-10)
                        -- If press and hold LMB for 100ms then release LMB -> move mouse relative (0,-20)
                        -- If press and hold LMB for 400ms then release LMB -> move mouse relative (0,-50)
            end 
        end
    end

Edit2: Great, it works fine with full auto shooting mode, but with semi-auto (I use the "pause" button as the shot button), after if i == 3 then it doesn't fire (it doesn't execute PressAndReleaseKey ( "pause") more). Please help me to make it perfect, I updated the semi-auto firing mode in Edit 2

local Recoil_Pattern = {
   {x = 0  ,y = 10 }   ,--1st Shot
   {x = 0  ,y = 10 }   ,--2nd Shot (hold LMB 100ms)
   {x = 0  ,y = 10 }   ,--3rd Shot (hold LMB 200ms)
   {x = 0  ,y = 10 }   ,--4th Shot (hold LMB 300ms)
   {x = 0  ,y = 10 }   ,--5th Shot (hold LMB 400ms)
}
local Shot_Sleep = 100

local recoil_sum_x, recoil_sum_y = 0, 0

EnablePrimaryMouseButtonEvents(true);
function OnEvent(event, arg)
   if event == "MOUSE_BUTTON_PRESSED" and arg == 1 then
      for i = 1, #Recoil_Pattern do
         Sleep(10)
         if IsMouseButtonPressed(1) then
            PressAndReleaseKey("pause")
            Sleep(Shot_Sleep)
            local dx, dy = Recoil_Pattern[i].x, Recoil_Pattern[i].y
            recoil_sum_x, recoil_sum_y = recoil_sum_x - dx, recoil_sum_y - dy
            MoveMouseRelative( dx, dy )
            Sleep(Shot_Sleep)
            if i == 3 then
                ctrl_is_down = true
                PressKey("lctrl")
            end
         else
            break
         end
      end
   elseif event == "MOUSE_BUTTON_RELEASED" and arg == 1 then
        if ctrl_is_down then
                ctrl_is_down = false
                ReleaseKey("lctrl")
            end
      while recoil_sum_x ~= 0 or recoil_sum_y ~= 0 do
         local dx, dy = recoil_sum_x, recoil_sum_y
         dx = dx > 127 and 127 or dx < -127 and -127 or dx
         dy = dy > 127 and 127 or dy < -127 and -127 or dy
         MoveMouseRelative( dx, dy )
         recoil_sum_x, recoil_sum_y = recoil_sum_x - dx, recoil_sum_y - dy
         Sleep(10)
      end
   end
end

回答1:


local Recoil_Pattern = {
   {x = 0  ,y = 10 }   ,--1st Shot
   {x = 0  ,y = 10 }   ,--2nd Shot  (hold LMB 100ms)
   {x = 0  ,y = 10 }   ,--3rd Shot (hold LMB 200ms)
   {x = 0  ,y = 10 }   ,--4rd Shot (hold LMB 300ms)
   {x = 0  ,y = 10 }   ,--5rd Shot (hold LMB 400ms)
}
local Shot_Sleep = 100

local recoil_sum_x, recoil_sum_y = 0, 0
local ctrl_is_down

EnablePrimaryMouseButtonEvents(true);
function OnEvent(event, arg)
   if event == "MOUSE_BUTTON_PRESSED" and arg == 1 then
      for i = 1, #Recoil_Pattern do
         Sleep(Shot_Sleep)
         if IsMouseButtonPressed(1) then
            local dx, dy = Recoil_Pattern[i].x, Recoil_Pattern[i].y
            recoil_sum_x, recoil_sum_y = recoil_sum_x - dx, recoil_sum_y - dy
            MoveMouseRelative( dx, dy )
            if i == 3 then
               ctrl_is_down = true
               PressKey("lctrl")
            end
         else
            break
         end
      end
   elseif event == "MOUSE_BUTTON_RELEASED" and arg == 1 then
      if ctrl_is_down then
         ctrl_is_down = false
         ReleaseKey("lctrl")
      end
      while recoil_sum_x ~= 0 or recoil_sum_y ~= 0 do
         local dx, dy = recoil_sum_x, recoil_sum_y
         dx = dx > 127 and 127 or dx < -127 and -127 or dx
         dy = dy > 127 and 127 or dy < -127 and -127 or dy
         MoveMouseRelative( dx, dy )
         recoil_sum_x, recoil_sum_y = recoil_sum_x - dx, recoil_sum_y - dy
         Sleep(10)
      end
   end
end


来源:https://stackoverflow.com/questions/61692011/how-to-reverse-the-movemouserelative-to-exact-first-position-when-releasing-lmb

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!