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.
In reply to Re: Tk: Binding Keys to events
by liverpole
in thread Tk: Binding Keys to events
by tamaguchi
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |