Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

bind up and down keys in a tk listbox

by lepetitalbert (Abbot)
on Mar 02, 2007 at 14:32 UTC ( [id://602882]=perlquestion: print w/replies, xml ) Need Help??

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

Hello dear Monks,

I'm totally unable to bind the up and down keys to move in a tk listbox

I found a few examples but none of them worked.

This is a simple test script :

use Tk; my $mw = MainWindow -> new(); my $lb = $mw -> Scrolled("Listbox", -scrollbars=>'e' , ) -> pack( -side => 'left' , -fill => 'y' ); foreach my $key ("a" , "b" , "c") { $lb -> insert( 'end', $key ); } $lb->activate ( 0 ) ; $lb->selectionSet ( 0 ) ; $lb -> bind ('<Key-Down>', sub {print "haha\n"} ); $mw -> bind ('<Key-Up>', sub {print "\nblabla"} ); MainLoop;

It seems I have to bind the key to the main window and not to the listbox, is this right ?

Thanks for any help or example.

Have a nice day.

"There is only one good, namely knowledge, and only one evil, namely ignorance." Socrates

Replies are listed 'Best First'.
Re: bind up and down keys in a tk listbox
by reasonablekeith (Deacon) on Mar 02, 2007 at 15:01 UTC
    If you want the up/down keys to control the listbox then I think you need to manage the focus. Adding an explict call in your code as follows enables these keys.
    foreach my $key ("a" , "b" , "c") { $lb -> insert( 'end', $key ); } $lb->activate ( 0 ) ; $lb->selectionSet ( 0 ) ; $lb->focus();
    I'm _far_ from being a Tk expert, so I'd advise you to check out Tk::Focus docs for yourself.
    ---
    my name's not Keith, and I'm not reasonable.

      Thank you reasonablekeith, cannot belive I missed this :)

      Works perfect.

      Have a nice day.

      "There is only one good, namely knowledge, and only one evil, namely ignorance." Socrates

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (3)
As of 2025-07-20 12:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.