tamaguchi has asked for the wisdom of the Perl Monks concerning the following question:

To illustrate how to bind a key to an event I have written the following:
#!/usr/bin/perl use warnings; use strict; use Tk; my $mw = Tk::MainWindow->new( -title => "TEST"); $mw->bind( '<KeyPress-a>', [sub {print "Button a has been pressed\n"}] + ); $mw->bind( '<KeyRelease-a>', [sub {print "Button a has been released\n +"}] ); MainLoop;
Do you know how to to do to bind two keys to the same event? So that I forexample have to press Crtl+A for the event to happen. I also wonder if you know were to find a complete list of keysyms. Thank you for any help.

Replies are listed 'Best First'.
Re: Tk: Binding Keys to events
by liverpole (Monsignor) on Oct 01, 2006 at 01:10 UTC
    Hi tamaguchi,

    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.


    s''(q.S:$/9=(T1';s;(..)(..);$..=substr+crypt($1,$2),2,3;eg;print$..$/
      Thank you all. What is the rationale for having the syntax '<Control-KeyPress-a>' wouldn´t be more clear to write <KeyPress-Control-a>'?
        It might well be more clear, but it doesn't work:
        #!/usr/bin/perl -w + use Tk; + use strict; use warnings; + my $mw = new MainWindow(); $mw->bind( '<KeyPress-Control-a>', sub {print "Button a has been press +ed\n"} ); $mw->bind( '<KeyPress-Control-a>', sub {print "Button a has been relea +sed\n"} ); MainLoop; # Produces ... bad event type or keysym "Control" at /usr/lib/perl5/site_perl/5.8.0/i386-linux-thread-multi/Tk/Derived.pm l +ine 469. # My version of Perl This is perl, v5.8.0 built for i386-linux-thread-multi

        s''(q.S:$/9=(T1';s;(..)(..);$..=substr+crypt($1,$2),2,3;eg;print$..$/
Re: Tk: Binding Keys to events
by Hue-Bond (Priest) on Sep 30, 2006 at 22:32 UTC

    I don't know anything about Tk. I haven't touched it ever. But I found a solution to this question in less than a minute. So all I have to say is RTFM.

    use Tk; my $mw = Tk::MainWindow->new( -title => "TEST"); $mw->bind( '<Control-KeyPress-a>', [sub {print "Button a has been pres +sed\n"}] ); $mw->bind( '<Control-KeyRelease-a>', [sub {print "Button a has been re +leased\n"}] ); MainLoop;

    Update: Oh, I forgot about the keysym list. Well, doing a locate keysym showed up /usr/X11R6/include/X11/keysymdef.h. You may have to install a development package to get it (in Debian, it's x-dev).

    --
    David Serrano

Re: Tk: Binding Keys to events
by runrig (Abbot) on Sep 30, 2006 at 22:37 UTC
    # Binding two things to the same subroutine my $print_sub = sub {print "Button A event\n"}; $mw->bind( '<KeyPress-a>', [ $print_sub ] ); $mw->bind( '<KeyRelease-a>', [ $print_sub ] );