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?
-
1Wait for or help upstream fix https://bugs.freedesktop.org/show_bug.cgi?id=20500 -- then you'll have a working RECORD again. – ephemient Dec 15 '09 at 18:39
1 Answers
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 exampleEV_REL
for relative moment,REL_KEY
for a keypress or release. More types are defined ininclude/linux/input.h
.
code
is event code, for exampleREL_X
orKEY_BACKSPACE
, again a complete list is ininclude/linux/input.h
.
value
is the value the event carries. Either a relative change forEV_REL
, absolute new value forEV_ABS
(joysticks ...), or 0 forEV_KEY
for release, 1 for keypress and 2 for autorepeat.

- 233,326
- 40
- 323
- 462
-
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 Dec 15 '09 at 11:46
-
At the moment I don't care about remote displays or anything fancy like that – Amandasaurus Dec 15 '09 at 12:23
-
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 Dec 15 '09 at 12:49