Detecting Keyboard, Mouse Activity in Linux

How to detect mouse and keyboard inactivity in linux

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.

Recording keyboard, mouse activity in linux

I shall not go into the basics of telling you how to use a tcp socket, that is simple enough.

However the basics of your question is that you will need to open and constantly read the /dev/input/by-id/yourmouseorkeyboardnamehere file. Reading this file will cause your program to block until there is a keyboard/mouse input (depending on if you read the keyboard or mouse file) then you will be able to read data representing what data came from the keyboard or mouse.

It should from there be fairly easy to send this data over a tcp socket to your tablet, you can learn to do that from any sockets tutorial on the Internet.

If you have any questions or need more detail please comment bellow.

Detecting mouse events from entire screen in linux

A "solution" I've seen in some places is to select ButtonPressEvent on every window in the entire tree of X windows, using XSelectInput, and also selecting SubstructureNotifyMask to find out about new windows. This method is known to break some programs (by preventing mouse events from propagating from a window whose client has not selected ButtonPressEvent), so use it at your own risk. Also, it will not work if another client has a pointer grab.

You can maintain a pointer grab with GrabModeSync forever and allow mouse events to go to the window that would normally get them by calling XAllowEvents(event_mode=ReplayPointer). If you keep a full pointer grab all the time, then it will horribly break all your programs because they will expect to be able to grab the pointer. You can grab a single button + modifier combination with XGrabButton, but I suspect your window manager might interfere with this in practice (which may be a good argument for modifying your window manager to notify you of events or directly do whatever it is you need).

If all of the programs you use support AT-SPI (they probably don't) and you have Assistive Technologies enabled, you can get notification of mouse events from AT-SPI. This won't break anything, but it won't work for some programs.

So there really isn't a good solution, but maybe one of these broken solutions will work in the circumstances you need, applied in a limited way or with a huge disclaimer.

how to monitor the event of mouse or keyboard

You must get some property depends on her you can determinate idle state of user.
Write C application and determinate idle state of user if state is longer than 30minutes you can simply shutdown machine.

Linux keyboard event capturing /dev/inputX

Thank you for the clue about ls -l /dev/input/by-id it helped me a lot !.

defenderdz@defenderdz-pc:~$ ls -l /dev/input/by-id | grep kbd
lrwxrwxrwx 1 root root 9 nov. 28 14:04 usb-Logitech_USB_Receiver-event-kbd -> ../event7
lrwxrwxrwx 1 root root 10 nov. 29 00:33 usb-NOVATEK_USB_Keyboard-event-kbd -> ../event26
lrwxrwxrwx 1 root root 9 nov. 28 14:04 usb-SONiX_USB_DEVICE-event-kbd -> ../event3
defenderdz@defenderdz-pc:~$

'kbd' is the suffix used for keyboard devices (I have 3 keyboards connected).

Your error is that you're accessing the wrong folder :

/dev/input/ instead of /dev/input/by-id

In my example the correct path is :

defenderdz@defenderdz-pc:~$ sudo cat /dev/input/by-id/usb-NOVATEK_USB_Keyboard-event-kbd
���]�I���]�I���]�Ia���]�b���]�b���]�b���]�����]�����]��s���]����]����]����]�>
���]�>
���]�>
d���]�8
���]�8
���]�8
���]�����]�����]��s���]H|���]H|���]H|���]�����]�� ���]��d���]Ǵ���]Ǵ ���]Ǵ

In your case

neel@pc1$ sudo cat /dev/input/by-id/usb-Plus_More_Enterprise_LTD._USB-compliant_keyboard-event-kbd

I'm not saying that it's the best solution but it works fine for me.
You can even create an automatic detection of the keyboard by parsing the ls result ...



Related Topics



Leave a reply



Submit