Mr. B.
Mr. B.

Reputation: 8697

Python / pynput: How to get relative mouse position on change?

I'd like to get the relative mouse position on change.

It's possible to set it absolute:
mouse.position = (10, 20)

or relative:
mouse.move(5, -5)

But I can only get the current, absolute one: mouse.position

Source: https://pythonhosted.org/pynput/mouse.html

Is there anything like mouse.position_relative?

Upvotes: 1

Views: 1755

Answers (1)

Dinko Pehar
Dinko Pehar

Reputation: 6061

Can you get mouse.position, move relative from that point with mouse.move(10, 20) and again retrieve mouse.position and calculate the difference between x1 and x2 and between y1 and y2 ?

For example, lets say mouse is at (100, 150) position:

p1 = mouse.position  # Becomes (100, 150)
mouse.move(10, 20)
p2 = mouse.position  # Becomes (110, 170)
diff = tuple(map(lambda d1, d2: d2 - d1, p1, p2)) # Becomes (10, 20)

Upvotes: 1

Related Questions