http://qs321.pair.com?node_id=1067184


in reply to Gtk2 X11 GrabKey

X11::Protocol often is just a simple network socket. You can access the deep underlying handle and wait for input via select - and it is pretty simple. I did that in File::KeePass::Agent in the unix.pm module because I wanted to listen for global events as well as have a local gui running at the same time in the same process. Here is the relevant code snippet:

sub _listen { my $self = shift; my $x = $self->x; $x->event_handler('queue'); # listen to both the x protocol events as well as our local term require IO::Select; my $in_fh = ...; # some other handle I'm looking at my $x_fh = $x->{'connection'}->fh; $x_fh->autoflush(1); $in_fh->autoflush(1); my $sel = IO::Select->new($x_fh, $in_fh); # handle events as they occur $self->_init_state(1); my $i; while (1) { my ($fh) = $sel->can_read(10); next if ! $fh; if ($fh == $in_fh) { $self->_handle_term_input($fh) || last; } else { $self->read_x_event; } } } sub read_x_event { my $self = shift; my $cb_map = shift || $self->{'global_cb_map'} || die "No global c +allbacks initialized\n"; my $x = $self->x; my %event = $x->next_event; return if ($event{'name'} || '') ne 'KeyRelease'; my $code = $event{'detail'}; my $mod = $event{'state'}; ... the rest of my code here }

Once you have a select handle, you can can_read for input with a time out as part of the Gtk loop or any AnyEvent loop - but I'll leave that for you to setup :)

my @a=qw(random brilliant braindead); print $a[rand(@a)];