views:

1098

answers:

4

Is there a simple way to detect mouse or keyboard activity in Linux or Xorg or Qt4 or Kde4 environment? Obviously not only on a particular window but in the entire Xorg desktop.

Thanks for the help.

A: 

I'm not familiar with the guts of the Qt4 or KDE API's.

However in general terms, on any X11 system keyboard or mouse input is only delivered to the active window, unless you call the underlying XGrabMouse, XGrabKeyboard, etc functions.

Of course, once you've done that, no other window can receive those input events, so you have to make very sure that there's a way for your program to release the mouse and/or keyboard input once you're done.

Alnitak
+1  A: 

You can use the XScreenSaver extension (man Xss). It can provide you with values into this struct using the function XScreenSaverQueryInfo:

   typedef struct {
       Window window;                /∗ screen saver window */
       int state;                    /∗ ScreenSaver{Off,On,Disabled} */
       int kind;                     /∗ ScreenSaver{Blanked,Internal,External} */
       unsigned long til_or_since;   /∗ milliseconds */
       unsigned long idle;           /∗ milliseconds */
       unsigned long event_mask;     /∗ events */
   } XScreenSaverInfo;

The idle field specifies the number of milliseconds since the last input was received from the user on any of the input devices.

I don't know about a Qt only solution for this.

Johannes Schaub - litb
doesn't this imply that you've got to regularly poll for changes to this structure?
Alnitak
yes, indeed. but that's not going to harm :)
Johannes Schaub - litb
you can also seem to register for events regarding the screensaver. but doesn't look like this includes an event for every activity of the user.
Johannes Schaub - litb
A: 

look for expectk, it's open source and will provide examples.

n-alexander
A: 

Try XRecord (grabing key&mouse is another common solution but it has issues with other applications grabbing).

Here is some link I used some time ago: http://mtoader.blogspot.com/2005/02/you-want-recording-x11-event-recording.html

elmarco