1
votes

This is my first attempt at LUA so bear with me!

I'm trying to combine a couple of functions that I want to loop at the same time, but I can only get them to run sequentially. Ideally, the function bound to mouse button 6 would start on press and stop on release (currently it doesn't stop at all!!). Separately the function on mouse button 3 would run when it is pressed and stop on release (this one does stop).

At the moment the button 3 loop works as it should, but the button 6 loop doesn't end.

Ideally, I would like both to work and stop on their respective button press and release, AND for them to BOTH run at the same time when both 3 & 6 are pressed at together.

Can this be done and if so what do I need to change to achieve this?

EnablePrimaryMouseButtonEvents(true);
 
function OnEvent(event, arg)
    if event == "MOUSE_BUTTON_PRESSED" and arg == 6 then
        repeat
            MoveMouseRelative(300,0)
                Sleep(150)
                MoveMouseRelative(-300,0)
                Sleep(1500)
    until event == "MOUSE_BUTTON_RELEASED" and arg == 6
    elseif IsMouseButtonPressed(3) then
        repeat  
            MoveMouseRelative(0,300)
            Sleep(1000)
        until not IsMouseButtonPressed(3)
    end
end

Thanks in advance :D

1

1 Answers

0
votes

Step 1.
Make sure you're not using MB#4 ("back") in the game.
If you don't use MB#4 in the game, just skip "Step 1".
If some action is assigned to MB#4 in the game, do the following:

  • choose keyboard button you don't currently use in the game (for example, F12)
  • goto GHUB (KEYS tab); bind F12 to your physical MB#4
  • goto game options; bind the action to F12 instead of MB#4

Now when you press physical MB#4, the game sees F12 and activates your old action.


Step 2.
Goto GHUB (SYSTEM tab)
Unbind "Back" from MB#4
Bind "Back" to MB#6


Step 3.
The script.

local all_buttons = {
   [4] = {  -- mouse button #6
      {time=0,   x=50 }, -- at time 0 move mouse right 50 pixels
      {time=150, x=-50}, -- at time 150 move mouse left 50 pixels
      {time=150+1500},   -- at time 150+1500 repeat the loop
   },
   [3] = {  -- right mouse button
      {time=0, y=50},  -- at time 0 move mouse down 50 pixels
      {time=1000},     -- at time 1000 repeat the loop
   }
}

function OnEvent(event, arg)
   if event == "PROFILE_ACTIVATED" then
      EnablePrimaryMouseButtonEvents(true)
   elseif event == "MOUSE_BUTTON_PRESSED" then
      repeat
         Sleep(10)
         local tm = GetRunningTime()
         local some_loop_is_active
         for btn_no, info in pairs(all_buttons) do
            if not info.started and IsMouseButtonPressed(btn_no) then
               info.started = tm
               info.next_step = 1
            end
            some_loop_is_active = some_loop_is_active or info.started
            local next_action = info[info.next_step]
            if info.started and tm - info.started >= next_action.time then
               info.next_step = info.next_step + 1
               local x, y = next_action.x, next_action.y
               if x or y then
                  x, y = x or 0, y or 0
                  local sx, sy = x < 0 and -1 or 1, y < 0 and -1 or 1
                  x, y = x*sx, y*sy
                  while x > 0 or y > 0 do
                     local px, py = x < 127 and x or 127, y < 127 and y or 127
                     MoveMouseRelative(px*sx, py*sy)
                     x, y = x-px, y-py
                  end
               else
                  info.started = nil
               end
            end
         end
      until not some_loop_is_active
   end
end

You should know that MoveMouseRelative() is limited by 127 pixels.
To move mouse cursor 300 pixels you should invoke it 3 times:
MoveMouseRelative(0,100);MoveMouseRelative(0,100);MoveMouseRelative(0,100)