in reply to Tk preventing a char in Entry widged to trigger a KeyBinding

#!/usr/bin/perl # http://perlmonks.org/?node_id=1173709 use strict; use warnings; use Tk; # use Tk::WidgetDump; # use Tk::ObjScanner; my $mw = MainWindow->new(); $mw->geometry("100x100+799+336"); $mw->bind('<KeyRelease-question>' => sub{print "A question mark was pr +essed!!\n"} ); my $entry = $mw->Entry( -text => '',-width => 20)->pack(); $entry->bind('<Enter>' => sub { $mw->bind('<KeyRelease-question>' => s +ub {})}); $entry->bind('<Leave>' => sub { $mw->bind('<KeyRelease-question>' => sub{print "A question mark was pressed!!\n"})}); # $mw->WidgetDump(); MainLoop;

You could also try with FocusIn and FocusOut, or some combination of the two types.

Replies are listed 'Best First'.
Re^2: Tk preventing a char in Entry widged to trigger a KeyBinding
by choroba (Cardinal) on Oct 11, 2016 at 22:01 UTC
    It still prints the message when I press ? inside the entry. The following works for me, though:
    #!/usr/bin/perl use warnings; use strict; use Tk; my $mw = MainWindow->new->geometry("100x100+799+336"); my $entry = $mw->Entry( -text => '', -width => 20 )->pack(); $entry->bind('<KeyRelease-question>' => sub { Tk->break }); $mw->bind( '<KeyRelease-question>' => sub { print "? was pressed!\n" + }); $mw->Entry->pack; MainLoop();

    ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,
      Thanks choroba,

      your example works correctly, or well, it works as I desire. But this imply to collect all entries as named instances, ie my $entry_first = $mw->Entry... my $entry_second = $mw->Entry

      It is too much work for me! maybe i'll try to bind all Tk::Entry class to Tk->break for all my defined bindings.

      I was not aware of the possibility to bind to an entire class, nor about Tk->break

      Incidentally my $mw = MainWindow->new->geometry("100x100+799+336"); gives me an undefined value at line 9. On my Tk version i needed a more verbose statement my $mw = MainWindow->new(); $mw->geometry("100x100+799+336");

      L*

      There are no rules, there are no thumbs..
      Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.

      Interesting - it doesn't print the message for me when the cursor is in the entry field.

Re^2: Tk preventing a char in Entry widged to trigger a KeyBinding
by Anonymous Monk on Oct 11, 2016 at 20:16 UTC

    You could also try with FocusIn and FocusOut, or some combination of the two types.

    Not needed, see Tk::bind#DESCRIPTION/Tk::bindtags or  perl -MTk -le " print tkinit->Entry->bindDump "

    Dont bind to keypress when you're wanting to bind to key, don't override "class" bindings when you're wanting to override an instance binding .. bindtags order and break