开发者

Linux - Any way to get global KeyPresses without xlib's RECORD? (DBus/HAL/etc?)

开发者 https://www.devze.com 2022-12-14 05:11 出处:网络
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

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 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消