I am developing an app on python which will check for user inactivity. Is there a way to check for key press and mouse move events in linux?
Asked
Active
Viewed 6,254 times
1 Answers
8
You could monitor the /dev/input/* files, when a key is pressed/the mouse is moved it is written to one of those files.
Try this for example:
fh = file('/dev/input/mice')
while True:
fh.read(3)
print 'Mouse moved!'
Now that I think of it, it might be better to use something like xidle to detect inactivity.

Wolph
- 78,177
- 11
- 137
- 148
-
1In Ubuntu Linux, `xautolock` is used instead – kolypto Nov 16 '10 at 08:58
-
@ВасинЮрий There are several solutions, some don't require root rights but I'm not sure if they can be implemented in Python easily: http://stackoverflow.com/questions/222606/detecting-keyboard-mouse-activity-in-linux – Wolph Feb 11 '17 at 13:49
-
@Wolph, I can't find any working, even CLI (bash, shell) solution. – Vasin Yuriy Feb 12 '17 at 17:59
-
2Have you tried the `xprintidle` app? It might work for you. – Wolph Feb 12 '17 at 22:57
-
In Python3, this works: fh = open('/dev/input/mice', "rb") – xerostomus Jun 06 '22 at 19:40