You can control the files / dev / input / * when the key is pressed / the mouse is moved, it is written to one of these files.
Try this for example:
fh = file('/dev/input/mice') while True: fh.read(3) print 'Mouse moved!'
Now that I think about it, it would be better to use something like xidle to detect inactivity.
source share