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

Randy Kramer <[email protected]>
Newsgroups gmane.comp.xfree86.forum
Message-ID <[email protected]>
On Saturday 10 May 2003 05:25 pm, Jim Peters wrote:
> 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.

Maybe your question should have been over on [email protected].

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

I probably can't help you much, but I have similar desires, so we may be 
able to compare notes.  I am particularly interested in mapping a 
keypress to a sequence of keypresses.  I have vague hopes of trying to 
modify the X server to do so, but I'm a newbie to C and to the X 
codebase.

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

Out of curiosity, how did you map common code fragments to shortcut keys 
in the Linux console?  Can you point me to a HOWTO or the name of a 
utility?

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

After a little bit of investigation, I decided that the right place to 
do it is in the X server.  My reasoning starts with that the X server 
handles the keyboard inputs anyway, I want them to be sent to whichever 
X client window has the focus, and I want them to be appear exactly 
like regular keystrokes.  (I found some X utilities that were a step in 
the right direction, but they sent keystrokes that were somehow 
recognizable by the X clients as faked, or from a different source 
which they could reject.  Hence, that utility didn't work for all X 
clients, notably Mozilla and KDE apps, IIRC.)

I should note that the people who've replied to me on the forum or 
XFree86 lists have stated that the X server is not the right place to 
do that, and pointed me to some other potential solutions.

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

Nope.  If dos and Windows can do it, Linux has a chance ;-)

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

Well, you're certainly several steps ahead of me -- I can tell just 
because you know about passive grabs and XSendEvents.  (I may be wrong, 
but I XSendEvent might be the "synthetic" event that other apps can 
recognize and choose not to receive.  (Perhaps that is even the 
default.)

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

Mine is less than yours, but I am trying to keep sort of a diary of my 
efforts over on WikiLearn.  See:

   * http://twiki.org/cgi-bin/view/Wikilearn/XServerModificationProject
   * http://bugs.xfree86.org/cgi-bin/bugzilla/show_bug.cgi?id=43
   * http://twiki.org/cgi-bin/view/Wikilearn/XAnnotated -- very 
preliminary

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

My opinion is that it belongs in the X server.  That seems to be a 
minority opinion, and a patch might never make it into CVS.  (And I 
might never manage to create a patch.)  Still that is my preference.

regards,
Randy Kramer
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.