Skip to content

Commit

Permalink
Make accelerated mouse motion more accurate by accumulating fractiona…
Browse files Browse the repository at this point in the history
…l component of the speed.
  • Loading branch information
jdoda committed Aug 5, 2024
1 parent 0cadaf3 commit 31846f6
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 9 deletions.
16 changes: 11 additions & 5 deletions inputremapper/injection/macros/macro.py
Original file line number Diff line number Diff line change
Expand Up @@ -469,11 +469,11 @@ def add_event(self, type_: Union[str, int], code: Union[str, int], value: int):
self.tasks.append(lambda handler: handler(type_, code, value))
self.tasks.append(self._keycode_pause)

def add_mouse(self, direction: str, speed: int, accel: Optional[float] = None):
def add_mouse(self, direction: str, speed: int, acceleration: Optional[float] = None):
"""Move the mouse cursor."""
_type_check(direction, [str], "mouse", 1)
speed = _type_check(speed, [int], "mouse", 2)
accel = _type_check(accel, [float, None], "mouse", 3)
acceleration = _type_check(acceleration, [float, None], "mouse", 3)

code, value = {
"up": (REL_Y, -1),
Expand All @@ -484,18 +484,24 @@ def add_mouse(self, direction: str, speed: int, accel: Optional[float] = None):

async def task(handler: Callable):
resolved_speed = _resolve(speed, [int])
resolved_accel = _resolve(accel, [float, None])
resolved_accel = _resolve(acceleration, [float, None])

if resolved_accel:
current_speed = 0.0
displacement_accumulator = 0.0
displacement = 0
else:
current_speed = resolved_speed
displacement = resolved_speed

while self.is_holding():
if resolved_accel and current_speed < resolved_speed:
current_speed += resolved_accel
current_speed = min(current_speed, resolved_speed)
displacement_accumulator += current_speed
displacement = int(displacement_accumulator)
displacement_accumulator -= displacement

handler(EV_REL, code, value * int(current_speed))
handler(EV_REL, code, value * displacement)
await asyncio.sleep(1 / self.mapping.rel_rate)

self.tasks.append(task)
Expand Down
4 changes: 2 additions & 2 deletions readme/macros.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,10 +125,10 @@ Bear in mind that anti-cheat software might detect macros in games.
> Moves the mouse cursor
>
> If `accel` is provided then the cursor will accelerate from zero to a maximum speed of `speed`.
> If `acceleration` is provided then the cursor will accelerate from zero to a maximum speed of `speed`.
>
> ```c#
> mouse(direction: str, speed: int, accel: float | None)
> mouse(direction: str, speed: int, acceleration: float | None)
> ```
>
> Examples:
Expand Down
3 changes: 1 addition & 2 deletions tests/unit/test_macros.py
Original file line number Diff line number Diff line change
Expand Up @@ -1021,8 +1021,7 @@ async def test_mouse_accel(self):
await asyncio.sleep(sleep)
self.assertTrue(macro_1.is_holding())
macro_1.release_trigger()

self.assertIn((EV_REL, REL_Y, -5), self.result)
self.assertEqual([(2, 1, 0), (2, 1, -2), (2, 1, -3), (2, 1, -4), (2, 1, -4), (2, 1, -5)], self.result)

async def test_event_1(self):
macro = parse("e(EV_KEY, KEY_A, 1)", self.context, DummyMapping)
Expand Down

0 comments on commit 31846f6

Please sign in to comment.