1
votes

I have a script which does a left click, then moves to the right about 1 cm, then clicks again and moves back to the left.

I would like this script to repeat itself continuously until i press a button, it doesn't really matter much which button it uses(except for MB 1, 2 and 3.

I have been trying for a while, with repeats and loops and the only thing i have achieved is a very complex script that makes the software crash after each run, which is slightly annoying.

I think there is something about the repeat function that i do not understand correctly. Can anyone show me how to get this to work?

greetings

Edit: I have updated the code to what it is now, the original code is below it.

local mb4_status, exit_flag

local function Move(dx, dy, time, is_interruptable)
local t0 = GetRunningTime()
local prev_dx, prev_dy = 0, 0
repeat
  Sleep(15)
  local part = math.min(time, GetRunningTime() - t0) / time
  local current_dx = math.floor(part * dx)
  local current_dy = math.floor(part * dy)
  local x, y = current_dx - prev_dx, current_dy - prev_dy
  if x ~= 0 or y ~= 0 then
     MoveMouseRelative(x, y)
  end
  prev_dx, prev_dy = current_dx, current_dy
  local prev_mb4_status = mb4_status
  mb4_status = IsMouseButtonPressed(4)
  exit_flag = exit_flag or mb4_status and not prev_mb4_status
  until part == 1 or is_interruptable and exit_flag
  end

  function OnEvent(event, arg)
  if event == "MOUSE_BUTTON_PRESSED" and arg == 4 then
  if exit_flag then
     exit_flag = false
  else
     mb4_status = true
     local x = 44
     repeat
        PressMouseButton(1)
        Move(0, 0, 200, false)  -- equivalent to Sleep(200)
        ReleaseMouseButton(1)
        Move(x, 0, 1000, true)  -- mixture of MoveMouseRelative(44,0) +      Sleep(1000)
        x = -x
     until exit_flag
  end
  end
  end

function OnEvent(event, arg)
if (event == "MOUSE_BUTTON_PRESSED" and arg == 8) then
        for i = 1, 1 do
            PressMouseButton(1)
            Sleep(200)
            ReleaseMouseButton(1)
        end
        Sleep(500)
        for i = 5, 15 do
            MoveMouseRelative(4,0)
            Sleep(1)
        end
        Sleep(500)
        for i = 1, 1 do
            PressMouseButton(1)
            Sleep(200)
            ReleaseMouseButton(1)
        end
        Sleep(500)
        for i = 5, 15 do
            MoveMouseRelative(-4,0)
            Sleep(1)
        end
        Sleep(500)
end
end
2
So, you want the following: 1) You press Btn#8, 2) LGS simulates mouse clicks and moves right and left continuously, 3) You press Btn#8 again, 4) LGS stops simulating mouse clicks and moves. Is it what you want?Egor Skriptunoff
Yes! I have tried so many hours, 'repeat' with until not and one time i had it working, sort of, but it wouldn't stop anymore. and if i added a sleep it would crash on save.Angela Van der Hoeven

2 Answers

0
votes

I haven't tested this code but it should at least give you some idea how to approach this. Instead of long blocking sleeps my code checks how much time has passed since start and checks the abort condtion of any button being pressed while waiting.

function OnEvent(event, arg)
if (event == "MOUSE_BUTTON_PRESSED" and arg == 8) then
    local function abortCondition()
      return IsMouseButtonPressed(1) or IsMouseButtonPressed(2) or IsMouseButtonPressed(3)
    end

    local function abortableSleep(delay)
      local startTime = GetRunningTime()
      while GetRunningTime() <= startTime + delay do
        if abortCondition() then return end
        Sleep(5)
      end
      return true
    end

    local function delayedClick(button, delay)
      PressMouseButton(button)
      Sleep(10)
      if not abortableSleep(delay-10) then return end
      ReleaseMouseButton(button)
      return true
    end

    repeat
  
      if not delayedClick(1, 200) then return end
      if not abortableSleep(500) then return end
      for i = 0, 10 do
        MoveMouseRelative(4,0)
        Sleep(1)
      end
      if not abortableSleep(500) then return end
      if not delayedClick(1, 200) then return end
      for i = 0, 10 do
        MoveMouseRelative(-4,0)
        Sleep(1)
      end
      if not abortableSleep(500) then return end
    until releaseCondition()
  end
end
0
votes

Please note Btn#4 is used instead of Btn#8

local mb4_status, exit_flag

local function Move(dx, dy, time, is_interruptable)
   local t0 = GetRunningTime()
   local prev_dx, prev_dy = 0, 0
   repeat
      Sleep(15)
      local part = math.min(time, GetRunningTime() - t0) / time
      local current_dx = math.floor(part * dx)
      local current_dy = math.floor(part * dy)
      local x, y = current_dx - prev_dx, current_dy - prev_dy
      if x ~= 0 or y ~= 0 then
         MoveMouseRelative(x, y)
      end
      prev_dx, prev_dy = current_dx, current_dy
      local prev_mb4_status = mb4_status
      mb4_status = IsMouseButtonPressed(4)
      exit_flag = exit_flag or mb4_status and not prev_mb4_status
   until part == 1 or is_interruptable and exit_flag
end

function OnEvent(event, arg)
   if event == "MOUSE_BUTTON_PRESSED" and arg == 4 then
      if exit_flag then
         exit_flag = false
      else
         mb4_status = true
         local x = 44
         repeat
            PressMouseButton(1)
            Move(0, 0, 200, false)  -- equivalent to Sleep(200)
            ReleaseMouseButton(1)
            Move(0, 0, 300, true)   -- equivalent to Sleep(300)
            Move(x, 0, 1000, true)  -- mixture of MoveMouseRelative(44,0) + Sleep(1000)
            Move(0, 0, 300, true)
            x = -x
         until exit_flag
      end
   end
end

UPDATE:
I've inserted sleep 300ms between move and click.
To change distance modify 44
Timings 200, 1000, 300 can also be modified

To change "start" button modify arg == 4
To change "stop" button modify IsMouseButtonPressed(4) (only 2-5)
Please note that script deliberately ignores every second press on "start" button because it assumes that "start" button is the same as "stop" button