views:

100

answers:

1

I was looking at how to capture global kepresses on Ubuntu Linux regardless of what window has focus. And people suggested some programmes to look at. But they all use the RECORD thing in XLib, which is broken in Ubuntu. Is there some other way to capture all the keypresses on Ubuntu? How about using HAL? DBus?

A: 

You can open the /dev/input/eventN device corresponding to the keyboard(s) and read the keyboard events from there. You'll even get keyboard events from the non-X consoles. This is the "evdev" interface.

From Documentation/input/input.txt in the kernel source:

You can use blocking and nonblocking reads, also select() on the /dev/input/eventX devices, and you'll always get a whole number of input events on a read. Their layout is:

struct input_event {
    struct timeval time;
    unsigned short type;
    unsigned short code;
    unsigned int value;
};

time is the timestamp, it returns the time at which the event happened. Type is for example EV_REL for relative moment, REL_KEY for a keypress or release. More types are defined in include/linux/input.h.

code is event code, for example REL_X or KEY_BACKSPACE, again a complete list is in include/linux/input.h.

value is the value the event carries. Either a relative change for EV_REL, absolute new value for EV_ABS (joysticks ...), or 0 for EV_KEY for release, 1 for keypress and 2 for autorepeat.

caf
IMHO, this is not a safe idea. If you application is started on a remote display, this way you will monitor the keyboard of the wrong machine. You should not bypass Xlib this way.
andcoz
At the moment I don't care about remote displays or anything fancy like that
Rory
That's worth pointing out, but the OP specifically asked to bypass Xlib (and gave the examples of "DBus" and "HAL" which, while not actually being useable for the question, have nothing to do with Xlib).
caf