Linux X11 - Global Keyboard Hook?

XSendEvent() probably does send it; but as it's widely regarded as a security hole, most programs ignore UI events with the send_event flag set.

XSendEvent() probably does send it; but as it's widely regarded as a security hole, most programs ignore UI events with the send_event flag set. The standard X11 protocol doesn't allow this. The XInput 2.0 extension might, but I doubt it; while Windows assumes a single event queue that every program listens to, so that a program can intercept an event and prevent it from being sent down the queue to other listeners, every X11 client has its own independent queue and all clients that register interest in an event receive an independent copy of it in their queue.

This means that under normal circumstances it's impossible for an errant program to block other programs from running; but it also means that, for those times when a client must block other clients, it must do a server grab to prevent the server from processing events for any other client.

Use XTestFakeKeyEvent() from the XTest extension library to propagate fake key press / release events.

Instead of doing this on the X11 level I recommend to do it on the input device level. /dev/input/event give you the input events. You can read off the keypresses there and decide if they should propagate further or be consumed.

Unfortunately there's no real documentation for this, but the header file linux/include/input. H is quite self explanatory. Also the evdev maintainer will gladly answer emails.

Not really an option at all if his code is not running as root, and you'd have to re-implement all the tricky keyboard junk the X server handles (layout, keycode mapping, etc), last, you'd have to deal with hotplug and the fact that the X server grabs the devices exclusively. – Spudd86 Jul 22 '11 at 22:39 @Spudd86: The OP wants a global hook. To me global means, that it will take over all further processing.

Yes this requires some dealing with hotplug, but this is not too complicated, either. The X server grabbing the device is no problem either. BTDT: a while back I coded such a "global hook" to catch my keyboard's multimedia keys but pass on the rest.

– datenwolf Jul 23 '11 at 9:39 Depends on what you're doing, if you want to be able to refer to keys by the symbol printed on them you're gonna have to deal with all the madness that xkb does so you can get the layouts right. (Since you'd want it to be the same as whatever the X server is doing) – Spudd86 Aug 2 '11 at 5:39.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions