Re: Do something clever on a keypress with Gtk2-0.92?



This worked!  Thanks!!

My code:

Attach the 'event':
$window->signal_connect (key_press_event => \&key_press);



## if the user hits enter (65293) or F10 (65479), run the search
##  subroutine
sub key_press ($$)
{
        my ($widget, $event) = @_;
        #print $event->keyval . "\n";

        if (($event->keyval == 65293) || ($event->keyval == 65479))
        {
                search();
        }
}


On Thu, 2003-08-07 at 15:39, muppet wrote:
Tom Cross said:
Either I want to make an accelgroup without the
menu, or just trap certain key-press events.
I'm lost on how to do either.

disclaimer: i have no experience with creating accel groups.  they may be more
appropriate than what i'm about to describe, but this works for me.

connect a callback to either key-press-event or key-release-event, then switch
on the keyval in the event structure you get.  return true if you handle it,
otherwise false lets the rest of the processing happen.  that sounds pretty
basic, but in general i have had a really hard time catching key events for
specific widgets, probably because the widgets i create do a lot of drawing
and charting and are usually plain DrawingAreas, which by default don't take
key events.  so, i tend to have the best results when i hook to the
key-press-event on the *toplevel* *window* containing the widgets i'm using. 
others may have the wisdom to correct me.

here's how i would hook up something to stop loading something from a remote
host when you get bored and hit escape (watch out, untested code!):

   # there's another module which contains all the keycodes!!!
   use Gtk2::Gdk::Keysyms;
   $win->signal_connect (key_press_event => sub {
                  my ($widget, $event) = @_;
                  return  unless $event->keyval ==
$Gdk2::Gdk::Keysyms{Escape};
                  abort_some_long_running_process ();
                  return 1;
                  });

there's also the key snoopers, which i have never used, and which may or may
not be bound to perl.  as i understand it, a key snooper is a function you
install to the event loop to filter key events globally, and catch things
application-wide rather than just for one window.  this may do what you want.



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]