0

I have a problem with Lua mouse script using G hub api. Everything works except for mouse-move functions, for example MoveMouseTo or MoveMouseRelative. Even MoveMouseWheel works fine. Here is my code. Thank you for any feedback.

There are no error logs. Text prints out. My code

2
  • MoveMouseTo(0,0) should immediately move the cursor to the upper left corner of the screen. What behavior do you see instead?
    – ESkri
    Commented May 7, 2023 at 20:49
  • Cursor stays at the same place and console logs are printing
    – German
    Commented May 8, 2023 at 10:42

1 Answer 1

0

Try to use function SmoothMoveMouseTo(x_in_pixels, y_in_pixels) instead of MoveMouseTo()

-- set your game screen resolution here
local width  = 1920
local height = 1080

function SmoothMoveMouseTo(x_target, y_target)
   local speed = 1000  -- mouse cursor speed in pixels per second
   local prev_dist = math.huge
   local attempts = 0
   repeat
      local tm = GetRunningTime()
      Sleep(10)
      local x, y = GetMousePosition()
      tm = (GetRunningTime() - tm) * .001
      x = x_target - math.max(0, math.min(width,  math.floor(x * (width  - 1) / 65535 + .5)))
      y = y_target - math.max(0, math.min(height, math.floor(y * (height - 1) / 65535 + .5)))
      local ax, ay = math.abs(x), math.abs(y)
      if ax < 1 and ay < 1 then
         break
      end
      local dist = (x*x + y*y) ^.5
      local dx = math.floor(math.min(ax, math.max(-ax, speed * x / dist * tm)) + .5)
      local dy = math.floor(math.min(ay, math.max(-ay, speed * y / dist * tm)) + .5)
      MouseMoveRelative(dx, dy)
      if dist < prev_dist then
         prev_dist = dist
         attempts = 0
      else
         attempts = attempts + 1
      end
   until attempts > 10
end

function OnEvent(event, arg)
   .... (your code here)
end

For example, SmoothMoveMouseTo(1920/2, 1080/2) to move to the center of the screen

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.