luamouse

Lua Script for Controlling WASD Keys via Mouse Movement


I need a lua script that allows me to press keys ("W", "A", "S", "D") through moving my mouse in certain direction.

For example:

I am moving mouse up by x pixels ---> Key "W" is being used for for y ms.

And so on analogically for the other mouse movements.


Solution

  • Try this

    -- turn script on  - click middle mouse button while NumLock LED is ON
    -- turn script off - click middle mouse button
    
    --------------------------------------------------------------------
    --  PARAMETERS
    --------------------------------------------------------------------
    
    local distance_horiz  = 20  -- x, pixels
    local distance_vert   = 25  -- x, pixels
    local key_press_delay = 30  -- y, ms
    
    local screen_width  = 1920   --  set your game screen resolution here
    local screen_height = 1080
    
    --------------------------------------------------------------------
    
    local active
    
    function OnEvent(event, arg)
       if event == "MOUSE_BUTTON_PRESSED" and arg == 3 then  -- Middle mouse button
          active = not active and IsKeyLockOn"NumLock"
          if active then
             local horiz, vert, horiz_key, vert_key, horiz_key_tm, vert_key_tm = 0, 0
             local prev_MMB = true
             repeat
                local prev_x, prev_y = math.floor(screen_width/2), math.floor(screen_height/2)
                local x = math.floor(prev_x * (2^16-1) / (screen_width-1) + 0.5)
                local y = math.floor(prev_y * (2^16-1) / (screen_height-1) + 0.5)
                MoveMouseTo(x, y)
                Sleep(10)
                local MMB = IsMouseButtonPressed(2)
                local exit = MMB and not prev_MMB
                prev_MMB = MMB
                x, y = GetMousePosition()
                x = math.floor((x + (0.5 + 2^-16)) * (screen_width-1) / (2^16-1))
                y = math.floor((y + (0.5 + 2^-16)) * (screen_height-1) / (2^16-1))
                horiz, vert = horiz + x - prev_x, vert + y - prev_y
                local tm = GetRunningTime()
                -- horiz
                if tm >= (horiz_key_tm or 0) then
                   if math.abs(horiz) >= distance_horiz then
                      local new_key
                      if horiz > 0 then
                         horiz, new_key = horiz - distance_horiz, "D"
                      else
                         horiz, new_key = horiz + distance_horiz, "A"
                      end
                      if new_key ~= horiz_key then
                         if horiz_key then
                            ReleaseKey(horiz_key)
                         end
                         PressKey(new_key)
                      end
                      horiz_key, horiz_key_tm = new_key, (horiz_key_tm or tm) + key_press_delay
                   elseif horiz_key then
                      ReleaseKey(horiz_key)
                      horiz_key, horiz_key_tm = nil
                   end
                end
                -- vert
                if tm >= (vert_key_tm or 0) then
                   if math.abs(vert) >= distance_vert then
                      local new_key
                      if vert > 0 then
                         vert, new_key = vert - distance_vert, "S"
                      else
                         vert, new_key = vert + distance_vert, "W"
                      end
                      if new_key ~= vert_key then
                         if vert_key then
                            ReleaseKey(vert_key)
                         end
                         PressKey(new_key)
                      end
                      vert_key, vert_key_tm = new_key, (vert_key_tm or tm) + key_press_delay
                   elseif vert_key then
                      ReleaseKey(vert_key)
                      vert_key, vert_key_tm = nil
                   end
                end
             until exit
             if vert_key then
                ReleaseKey(vert_key)
             end
             if horiz_key then
                ReleaseKey(horiz_key)
             end
          end
       end
    end