Advertisement
bystander36

fn_MoveMouseRelative

Apr 7th, 2019
321
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.39 KB | None | 0 0
  1. log = OutputLogMessage
  2. Sleep_Delay = 1
  3.  
  4. function OnEvent(event, arg)
  5.     if event == "PROFILE_ACTIVATED" then
  6.         EnablePrimaryMouseButtonEvents(true)
  7.  
  8.     elseif event == "MOUSE_BUTTON_PRESSED" and arg == 4 then
  9.         x, y = GetMousePosition()
  10.         save_x, save_y = fn_GridToPixel(x, y)
  11.         log("x = %d, y = %d\n", save_x, save_y)
  12.        
  13.  
  14.     elseif event == "MOUSE_BUTTON_PRESSED" and arg == 5 then
  15.         fn_MoveMouseRelative(-1920, -1080)      -- forces mouse to corner
  16.         fn_MoveMouseRelative(save_x, save_y)        -- moves relative to saved location
  17.     end
  18. end
  19.  
  20.  
  21. function fn_GridToPixel(x, y)
  22.     local resolution = { x = 1920, y = 1080 }
  23.     local max_grid_val = 65535
  24.     local conv_x, conv_y
  25.  
  26.     conv_x = x * ((resolution.x-1) / max_grid_val) + 1
  27.     conv_y = y * ((resolution.y-1) / max_grid_val) + 1
  28.  
  29.     return conv_x, conv_y
  30. end
  31.  
  32. function fn_MoveMouseRelative(x, y)
  33.     local max_inc = 32
  34.     local n
  35.     local sign
  36.  
  37.     if x >= 0 then
  38.         sign = 1
  39.     else
  40.         sign = -1
  41.     end
  42.     n = math.floor(math.abs(x / max_inc))
  43.     for i = 1, n do
  44.         MoveMouseRelative(max_inc * sign, 0)
  45.         if Sleep_Delay then
  46.             Sleep(Sleep_Delay)
  47.         end
  48.     end
  49.     MoveMouseRelative( (x % max_inc) * sign, 0)
  50.  
  51.     if y >= 0 then
  52.         sign = 1
  53.     else
  54.         sign = -1
  55.     end
  56.     n = math.floor(math.abs(y / max_inc))
  57.     for i = 1, n do
  58.         MoveMouseRelative(0, max_inc * sign)
  59.         if Sleep_Delay then
  60.             Sleep(Sleep_Delay)
  61.         end
  62.     end
  63.     MoveMouseRelative(0, (y % max_inc) * sign)
  64. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement