1

How can I log how far the mouse travels over time, in order to later produce a graph over mouse movement over time?

The goal is to be able to create a utility for monitoring mouse movement.

2
  • It is definitely possible. There is quite a lot of "mouse odometer/speedometer" software.
    – dr_
    Oct 13, 2017 at 7:30
  • Do you have an example of such a piece of software? I can't find any for Linux (found Xodo, that does not seem to be available anymore). I've searched on Github, Google and Arch Linux AUR.
    – Alexander
    Oct 13, 2017 at 7:32

1 Answer 1

1

There are many X11 tools using the XTest or other extensions that can get mouse movements, such as cnee, but you can also just read /dev/input/mice on most systems, and get a stream of 3 bytes as you would from an old PS/2 mouse. This bit of python will decode the x,y values and calculate the pixel distance you move. Note, the default permissions of the dev file do not allow reading to other than group input.

#!/usr/bin/python
# calc mouse distance travelled
# https://unix.stackexchange.com/a/397985/119298
import struct
total = 0.
with open("/dev/input/mice") as fd:
    while True:
        x, y = struct.unpack("xbb", fd.read(3))
        total += (x*x+y*y)**.5
        print("%d" % total)
1

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .