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

I have a gui built using tk perl and it has buttons that do various things.I would like to bind the enter key to certain buttons that already have subroutines attached to them. I know I can focus force certain buttons but lets say I want to press enter to submit after I fill in a field that has the focus. How can I bind the enter key to submit? $Submit->bind('<Enter>'); causes weird repacking issues.

Replies are listed 'Best First'.
Re: Button Binding
by Dumu (Monk) on Mar 18, 2015 at 17:26 UTC
    If I'm reading the POD in Tk::bind correctly, the syntax should / could be:
    $widget->bind('<Enter>', $submit)
    where $submit is a coderef to sub submit { ... }.
      I get the part of binding to a subroutine. My problem is my buttons issue commands and I want to bind a key to invoke the button action. I have tried $widget->bind('<Enter>', $submit->invoke); But it causes issues messing up the frame packing.
         $submit->invoke is not a subroutine reference
Re: Button Binding
by thundergnat (Deacon) on Mar 18, 2015 at 19:29 UTC

    I have to say, I don't ever remember binding events to a widget causing geometry (packing) issues. Would it be possible for you to post a small runnable example of code that exhibits this behavior? It would help a lot with diagnosis.

    On an unrelated note, the '<Enter>' event has nothing to do with the 'Enter' key. Rather it is the notification of when the mouse cursor has 'enter'ed the widget it is bound to. Perhaps you wanted the event '<KeyPress-Return>' instead?

Re: Button Binding (tk code)
by Anonymous Monk on Mar 18, 2015 at 23:16 UTC

    Where is your code?

    If a button has focus, enter already clicks the button, so which widget has the focus, that you want to respond to enter

      #!/usr/bin/perl -- use strict; use warnings; use Tk; my $mw = tkinit; $mw->Label( -text => "yo" )->pack; my $en = $mw->Entry( -text => "yo" )->pack; my $bb = $mw->Button( -command => \&warner )->pack; BuhBind( $mw, '<Enter>' ); ## mouse enter BuhBind( $en, '<Enter>' ); ## mouse enter BuhBind( $mw, '<KeyPress-Return>' ); BuhBind( $en, '<KeyPress-Return>' ); ## $mw->WidgetDump; ## debuggery $mw->MainLoop; sub BuhBind { my( $wig , $tag ) = @_; my $str = "$wig -> bind( $tag "; $wig->bind( $tag => sub { warn " $str ## @_ ## $Tk::widget \n"; return Tk->break ; ## do not propagate, if $en is responding +to return $mw doesn't have to } ); } sub warner { warn "warner ## @_ ## $Tk::widget \n"; }