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.

In reply to Re^2: Tk preventing a char in Entry widged to trigger a KeyBinding -- solution by Discipulus
in thread Tk preventing a char in Entry widged to trigger a KeyBinding by Discipulus

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.