Re: FAQ




On Sep 16, 2004, at 2:38 PM, Torsten Schoenfeld wrote:

On Sun, 2004-09-05 at 14:32, Jens Wilke wrote:

Receiving/Emitting  Signals
-------------------------------
* Receiving Keystrokes

This signal handler reacts on keystrokes.

$widget->signal_connect (key_press_event => sub {
                ($widget, $event) = @_;
                $data = $event->keyval ;
}

# $event->keyval receives the code of the stroked key

I think this is too basic to go in the FAQ.  Pretty much all tutorials
cover this.

it's also incomplete, because it omits the fact that if you stop propagation by returning true (whcih is what the example implicitly does), you can break the widget's built-in behavior.


        $event = Gtk2::Gdk::Event::Key->new('key-press');
        $event->keyval('102');
        $button->signal_connect (clicked => sub
{  $widget->signal_emit('key_press_event', $event) } );

widget will receive a keystroke of key 102 'F', if button is clicked.

please don't use magic numbers to refer to key values -- that's what Gtk2::Gdk::Keysyms is for.

this will only send an event to the given widget, which won't suffice in all cases where you want to pretend input happened. the XTest extension exists for true event synthesis; i don't know how to use it, and i'm pretty sure there are no perl bindings for it.

--
"the ternary operator makes it a bit less ugly."
    -- kaffee




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