Search code examples
luamouselogitechlogitech-gaming-software

Having problems with Sleep() func on MouseMoveRelative on LUA


got a new drive and installed a fresh windows.

Before on old OS, i could use "Sleep" to make "MoveMouseRelative" work like it was natural movement.

i have created a function were i can move the mouse calling it with how many times i want it to "move"

and with the milliseconds between each "move" Example:(works well)

function _move(x, range, time, range2)
    for i = 1, x do
    MoveMouseRelative(range,range2)
    Sleep(time)
    end
end 

n

If i set "Sleep(1)" beteween each "MoveMouseRelative" it moves like it has like "Sleep(50)", i couldnt figure why.

if i call it with 50 Moves of 1ms it takes 2,5 seconds to finish instead of 50ms

on my old drive with the old OS install(same PC), i can use it normally takes 50ms

Its like the software wont let me use small ms such as 1 ms.

Tried

LGHUB Reinstall

deactivating AV

disabling things on windows,

copying the LGHUB folder from the OS that is working well

copying the LGHUB folder with the configs(LocalAppdata)

My Mouse is the G502 SE

Help?


Solution

  • TL;DR:
    Sleep(1) behavior is unstable and should not be used.
    Sleep() has a much worse precision than you think it has.


    Long answer:

    Sleep() in LGHUB script internally invokes WinAPI function Sleep.

    An excerpt from the documentations:

    After the sleep interval has passed, the thread is ready to run. Note that a ready thread is not guaranteed to run immediately. Consequently, the thread may not run until some time after the sleep interval elapses.

    The system clock "ticks" at a constant rate. If dwMilliseconds is less than the resolution of the system clock, the thread may sleep for less than the specified length of time. If dwMilliseconds is greater than one tick but less than two, the wait can be anywhere between one and two ticks, and so on.

    The "tick" here is the "time slice" length which is 15ms on my Windows (but it could be 10ms on another Windows versions).

    As you see from the documentation above, the Sleep() function has precision of one "tick", so behavior of Sleep(1) is unstable by design.