如何在Linux中检测鼠标和键盘不活动

问题描述:

我正在使用python开发一个应用程序,该应用程序将检查用户是否处于非活动状态.有没有一种方法可以检查Linux中的按键和鼠标移动事件?

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?

您可以监视/dev/input/*文件,当按下键/移动鼠标时,会将其写入其中一个文件中.

You could monitor the /dev/input/* files, when a key is pressed/the mouse is moved it is written to one of those files.

尝试例如:

fh = file('/dev/input/mice')
while True:                 
    fh.read(3)
    print 'Mouse moved!'

现在我想到了,最好使用 xidle 之类的东西来检测不活动状态.

Now that I think of it, it might be better to use something like xidle to detect inactivity.