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

Hi, I have list box, I am able to select the entries using keyboard and mouse, but when i use up and down arrow keys, its not selecting the list. But able to scroll the list with an underline below each entity the arrow key is related. Thanks
  • Comment on LIst box selection using up and down arrow key

Replies are listed 'Best First'.
Re: LIst box selection using up and down arrow key
by Anonymous Monk on Jul 28, 2008 at 08:59 UTC
Re: LIst box selection using up and down arrow key
by zentara (Cardinal) on Jul 28, 2008 at 13:21 UTC
    You are running into one of the limitations of using prebuilt widgets like the listbox. Highlighting selections have predefined meanings, and you just can't override them. The style of the 'active' element can be underlined or a dashed box around it. Color change is reserved for 'selected' items'. Look at this script, and remove the 'if entry press' conditional and see what happens..... the listbox needs a way to differentiate between the active and selected items, especially when the select mode is multiple. Now you can write your subclass of Listbox, and try to change the color definitions, but it is tricky because the mouse is also involved. You may want to look at the Tk::HList, which gives more control over the color schemes, with the 'style' options. In this example below, you may be able to selectionClear everything before setting the new selection, but then you lose multiple selection mode. It's tricky.
    #!/usr/bin/perl use warnings; use strict; use Tk; my $mw=tkinit; my @selects; my $lb1 = $mw->Listbox->pack(); $lb1->insert('end',('A'..'J')); $lb1->bind('<Enter>' => sub{ $lb1->focus }); $mw->bind('<KeyPress>' => sub { $lb1->focus; my $e = $lb1->XEvent; # get reference to X11 event struc +ture my $key = $e->K; # comment out the if {} and see what happens if( $key eq 'Return' ){ push @selects, $lb1->get('active'); $lb1->itemconfigure('active', -background => 'yell +ow'); } $mw->update; print "@selects\n"; } ); MainLoop;

    I'm not really a human, but I play one on earth Remember How Lucky You Are
Re: LIst box selection using up and down arrow key
by zentara (Cardinal) on Jul 28, 2008 at 14:13 UTC
    Just to show you another widget, that will allow you to have full control over colors, look at this simple Tk::HList example. It can get as complex as you want, by using the -browsecmd callback to change styles. Search groups.google.com for examples of "perl tk hlist colors"

    I'm not really a human, but I play one on earth Remember How Lucky You Are