in reply to Re: Tk preventing a char in Entry widged to trigger a KeyBinding
in thread Tk preventing a char in Entry widged to trigger a KeyBinding

Thanks to his Anonymousity and all other monks who spent some time to help me.

I think the bind qw/Tk::Entry <Key-?>/,sub{Tk->break}); is the laziest solution.

In the case of several TopLevel windows, bindings can be aggregated into a lookup table. Then foreach window any binding is bound to the window and removed for the Tk::Entry class.

It is also useful to bind <Return> to give back the focus for all Tk::Entry widgets.

#!/usr/bin/perl use warnings; use strict; use Tk; my $mw = MainWindow->new(); $mw->geometry("200x100+0+0"); $mw->Entry( -text => '',-width => 20)->pack(); $mw->Checkbutton( -text => 'focus on me and press ? or !' )->pack; my $mwbis = $mw->Toplevel(); $mwbis->geometry("200x100+300+0"); $mwbis->Entry( -text => '',-width => 20)->pack(); $mwbis->Checkbutton( -text => 'focus on me and press ? or !' )->pack; # a lookup table for all global bindings my %bind_table = ( '<KeyRelease-?>' => sub{print "A question mark was pressed!!\n"}, '<KeyRelease-!>' => sub{print "A esclamation mark was pressed!!\n"} +, ); foreach my $window ($mw, $mwbis){ # assign bindings to all TopLevel foreach my $bind (keys %bind_table){ $window->bind($bind => $bind_table{$bind}); # remove bindings for all Tk::Entry in all windows $window->bind('Tk::Entry', $bind,sub{Tk->break}); } # bind Return to for Tk::Entry to give the focus to parent $window->bind('Tk::Entry','<Return>' => sub{$_[0]->parent->focus}) } MainLoop;

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.