Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

re-binding Tab in a TK Text field

by wolfger (Deacon)
on Oct 29, 2004 at 16:12 UTC ( [id://403801]=perlquestion: print w/replies, xml ) Need Help??

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

This is driving me crazy. I have a text field that I have absolutely no interest in ever entering an actual tab character into. I want Tab to shift the focus to the next widget, but I cannot get this to work. I have the following code:
my $t1 = $mw->Text( -background=>"navy", -foreground=>"white", -height=>35, -width=>80, -wrap=>"word", -selectbackground=>"blueviolet" ); my $e2 = $mw->Entry( -background=>"navy", -foreground=>"white", -width=>80, -textvariable=>\$description, -selectbackground=>"blueviolet" ); $t1->bind("<Tab>", sub { $e2->focus });

Now I have replaced Tab with Ctrl-q, and then when I Ctrl-q I get a control character printed, and the desired focus shift. But when I go back to Tab, I get a tab, and no focus shift. What do I have to do to rebind Tab to my wishes? If it's not possible to rebind Tab, how do I bind a character without the character printing in my Text field?

--
Believe nothing, no matter where you read it, or who said it - even if I have said it - unless it agrees with your own reason and your own common sense.
(Buddha)

Replies are listed 'Best First'.
Re: re-binding Tab in a TK Text field
by kvale (Monsignor) on Oct 29, 2004 at 17:16 UTC
    As you have found, Tab is special inside Tk::Text and your binding is not being trapped in time. I don't know why the explicit bind is not working. Mastering Perl/Tk mentions that the usual Tab focus method has been remapped to Shift-Tab for Tk::Text, so you might just use that.

    A more involved possibility is to derive a new subclass from Tk::Text that doesn't remap Tab. Look at Tk::ROText for an example of subclassing. It is probably just a matter of overriding a single method.

    -Mark

      I tested, and it was actually remapped to both CTRL-TAB and SHIFT-TAB.

      But I really hate the fact that it was remapped to SHIFT-TAB, as everybody knows that, TAB goes to the next field, and SHIFT-TAB goes back to previous field. Tk has no reason to break that convention.

        As a workaround, chop the last character off the text field variable in the exit field binding method.
        the hardest line to type correctly is: stty erase ^H
      Actually, the normal Tab funciton is mapped to Ctrl-Tab, not Shift-Tab. At least, for Win32. I haven't tried it at home on Linux yet. Shift-Tab is normal Shift-Tab behaviour (backwards tabbing).

      Thanks for the pointer on subclassing. I'll check it out.

      --
      Believe nothing, no matter where you read it, or who said it - even if I have said it - unless it agrees with your own reason and your own common sense.
      (Buddha)
Re: re-binding Tab in a TK Text field
by kelan (Deacon) on Oct 30, 2004 at 06:02 UTC

    Ok, this is kind of tricky, but possible. In Tcl, you could do it like this:

    bind .t1 <Tab> { focus [tk_focusNext .t1]; break }
    So you might think that a straight translation of that would work for Perl/Tk, but it doesn't. (I tried it.)

    The reason that a straight translation doesn't work is because Perl/Tk changed the default order that bindtags uses. To change the binding, either subclass and change it, as suggested above, or simply reorder the bindtags for the widget and then you can do the rebind:

    # fix the bindtags order so that widget events are # processed before class events $t1->bindtags( [ ($t1->bindtags)[1,0,2,3] ] ); # now give the text widget a new Tab binding $t1->bind( '<Tab>', sub { $t1->focusNext; Tk->break; } );
    If you're observant, you'll also notice one other place where it wasn't a straight translation. In Tcl, tk_focusNext just returns the name of the next widget in focus order, then you have to actually call focus on it. In Perl/Tk, it just sets the focus directly.

      Thanks! That does the trick, and is a lot easier than subclassing.

      --
      Believe nothing, no matter where you read it, or who said it - even if I have said it - unless it agrees with your own reason and your own common sense.
      (Buddha)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://403801]
Approved by atcroft
Front-paged by diotalevi
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (2)
As of 2024-04-25 03:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found