TolikPylypchuk / SharpHook

SharpHook provides a cross-platform global keyboard and mouse hook, event simulation, and text entry simulation for .NET
https://sharphook.tolik.io
MIT License
342 stars 32 forks source link

SimulateMouseMovementRelative(0, 0) - Mouse is moving - Windows 11 #92

Closed clFaster closed 7 months ago

clFaster commented 7 months ago

Hi very nice lib! I've been experimenting with it to control mouse movement on Windows 11.

I'm encountering an issue where, when I execute the following code every second using a timer:

var simulator = new EventSimulator();
        simulator.SimulateMouseMovementRelative(0, 0);

The mouse consistently moves to the top-left corner.

Environment:

Operating System: Windows 11 Library Version: 5.3.1

Additional Information:

TolikPylypchuk commented 7 months ago

Hello! Thanks for posting this issue! I will investigate it in the nearest future.

c0nstexpr commented 7 months ago

Same as the SimulateMouseMovement

TolikPylypchuk commented 7 months ago

Sorry about the delay, but I've finally managed to fix the issue.

Here's the code I used to test that the mouse coordinates don't actually change when moving by 0 pixels:

using var hook = new SimpleReactiveGlobalHook();

hook.MouseMoved
    .Select(e => (e.Data.X, e.Data.Y))
    .DistinctUntilChanged()
    .Buffer(2, 1)
    .Subscribe(coords => Console.WriteLine($"Mouse moved by: ({coords[1].X - coords[0].X}, {coords[1].Y - coords[0].Y})"));

_ = hook.RunAsync();

Thread.Sleep(1000);

var simulator = new EventSimulator();

for (int i = 0; i < 10000; i++)
{
    simulator.SimulateMouseMovementRelative(0, 0);
}

With the current version of libuiohook I got the following output in one of the runs (the output is more or less random):

Mouse moved by: (-1, -1)
Mouse moved by: (-1, -1)
Mouse moved by: (-1, -1)
Mouse moved by: (-1, -1)
Mouse moved by: (-1, -1)
Mouse moved by: (-1, -1)
Mouse moved by: (-1, -1)
Mouse moved by: (-1, -1)
Mouse moved by: (-1, -1)

With the new version I've got no output which is expected.

I will release a new version with the fix shortly.

TolikPylypchuk commented 7 months ago

I've just released version 5.3.2 which fixes this bug so this issue can be closed.