Linux - Any way to get global KeyPresses without xlib's RECORD? (DBus/HAL/etc?)
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 keypres开发者_StackOverflowses on Ubuntu? How about using HAL? DBus?
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.
精彩评论