in reply to Tk: Binding Keys to events
Note that you don't have to pass the subroutine as an anonymous subroutine within a list reference.
That is to say, although it will work to do:
$mw->bind( '<Control-KeyPress-a>', [sub {print "Button a has been pr +essed\n"}] ); $mw->bind( '<Control-KeyRelease-a>', [sub {print "Button a has been re +leased\n"}] );
One often specifies either a simple anonymous subroutine:
$mw->bind( '<Control-KeyPress-a>', sub {print "Button a has been pre +ssed\n"} ); $mw->bind( '<Control-KeyRelease-a>', sub {print "Button a has been rel +eased\n"} );
or a simple anonymous list:
$mw->bind( '<Control-KeyPress-a>', [ \&say_button_pressed, "a" ]); $mw->bind( '<Control-KeyRelease-a>', [ \&say_button_released, "a" ]); # Unlikely to do it this way unless the following subroutines are # longer, or likely to be called from multiple places... # sub say_button_pressed { printf "Button %s has been pressed\n", $_[0]; } sub say_button_released { printf "Button %s has been released\n", $_[0]; }
I tend to use the former, an anonymous subroutine, when the code is brief and simple, whereas the latter is better when the subroutine is long and complex.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Tk: Binding Keys to events
by tamaguchi (Pilgrim) on Oct 03, 2006 at 10:43 UTC | |
by liverpole (Monsignor) on Oct 03, 2006 at 11:04 UTC | |
by tamaguchi (Pilgrim) on Oct 03, 2006 at 15:46 UTC | |
by rcseege (Pilgrim) on Oct 04, 2006 at 00:05 UTC | |
by liverpole (Monsignor) on Oct 03, 2006 at 16:32 UTC |