Hue-Bond has asked for the wisdom of the Perl Monks concerning the following question:

Wise monks,

I have a copy of perlpanel, a dead project aimed to make a lightweight replacement for gnome-panel or KDE's kicker. It has been discontinued due to some changes in gnome 2 libraries, that prevent perlpanel to display gnome's menus. This isn't a problem for me, so I'll give it a try.

It features a button that displays a menu, with the typical actions "Run program", "Close panel" and so on. Currently the button only works when clicked, and it doesn't seem to react upon any key press. I'd like to implement that. The button should act whether the panel has the focus or not.

My first try was with X11::Protocol. This test programs receives all keyboard events but, unfortunately, prevents the rest of the programs to get any keyboard event:

use warnings; use strict; use X11::Protocol; my $x = X11::Protocol->new; $x->event_handler ('queue'); $x->GrabKeyboard ($x->root, 0, 'Asynchronous', 'Asynchronous', 'Curren +tTime'); my $count = 0; while ($count++ < 30) { my %event = $x->next_event; my $name = $event{'name'}; if ('KeyPress' eq $name) { ## process event print "key pressed!\n"; } } $x->UngrabKeyboard ('CurrentTime');

The man page for XGrabKeyboard states it clearly: "Further key events are reported only to the grabbing client" but I see no mention to some mechanism to let the events pass through to the rest of running programs. How could I achieve the desired behaviour?

--
David Serrano

Replies are listed 'Best First'.
Re: Grabbing keyboard in X11
by pc88mxer (Vicar) on Jun 23, 2008 at 04:46 UTC

      Thanks for your reply. Unfortunately, a Perl module around XEvIE doesn't seem to exist yet, so I'm stuck with X11::Protocol. On the other hand, I've taken a look into the source code of icewm and I've seen that it uses XGrabKey to grab the key combinations it uses. Something like:

      $x->GrabKey (52, 'AnyModifier', $x->root, 0, 'Asynchronous', 'Asynchronous');

      (52 being the keycode for the letter 'z').

      The only obstacle I have to overcome is how to translate 'z' to 52 (in this example) but I think X11::Keyboard's KeysymToKeycode will fit this purpose nicely.

      --
      David Serrano