How can I modify a keyboard event before it reaches an app?

Jim Peters <[email protected]>
Newsgroups gmane.comp.xfree86.forum
Message-ID <[email protected]>
I'm not sure this is the right place to ask, but I hope someone can
point me to a better place to ask if there is one, or to example code
if what I hope to achieve has already been implemented somewhere.

Briefly: What I want to do is map a keypress (for example AltGr+P) to
another keypress or sequence of keypresses (for example Up-arrow) so
that all X applications see the mapped keypress and not the original.

The reason for this is that I have the keyboard layout extensively
customised under the Linux console, using three different custom
modifier keys along with shift to access a huge number of shortcuts,
common code fragments ("if ("), virtual console switching, and so on,
and so on.  Also, I have AltGr plus the keys L;'P mapped to cursor
movement keys, with other movement and common emacs functions
clustered around.  Many keys generate sequences of keypresses.  This
environment works very well for me.

Currently I have achieved the same effect in X on one machine by
hacking the Linux kernel to remap the raw keycodes before X gets a
hold of them, and doing further remapping within a hacked version of
rxvt.  However, in moving to a new machine, I have decided to try and
"do it properly" by creating an X Window Manager (based on w9wm
currently) to handle all of this without any modifications to X or the
kernel.

My initial attempts have not been very successful, and I am wondering
if what I hope to achieve is in fact impossible.

My current approach is to set a passive grab on each of my modifier
keys, and collect the events before they reach the application.  I
then remap these and send the replacement events to the target window
with XSendEvent.  However, this isn't working.  I'm wondering whether
this is simply an error on my part, or if the grab is blocking my
events and I am trying to do something that is impossible within X.
(For one thing, the target app appears to lose focus whilst the grab
is active -- i.e. the cursor goes clear.)

My knowledge of the internal workings of X is minimal, so any guidance
here would be much appreciated.

Also, if there is some better approach I could be taking to the whole
problem (e.g. using some X extension designed for this kind of thing),
I would be very glad to hear about it.

Many thanks in advance for the help --

Jim

-- 
 Jim Peters                  (_)/=\~/_(_)                 [email protected]
                          (_)  /=\  ~/_  (_)
 Uazú                  (_)    /=\    ~/_    (_)                http://
 B'ham, UK          (_) ____ /=\ ____ ~/_ ____ (_)            uazu.net

 OpenEEG -- EEG for the rest of us!   For details:  http://uazu.net/OE
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.