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

hello, i'm working in perl TK i want to know how do i select a line in a listbox by the right click of the mouse. i would like to know also how do i bind the right click of the mouse to act like the left click. thanks, i'm really stuck in this.
  • Comment on selecting in listbox by right click mouse

Replies are listed 'Best First'.
Re: selecting in listbox by right click mouse
by zentara (Cardinal) on Aug 28, 2007 at 13:01 UTC
    Since your other node may be reaped as a dup, I post this code here also.
    #!/usr/bin/perl use warnings; use strict; use Tk; my $value; my $mw = MainWindow->new; my $entry = $mw->Entry( -state => 'readonly', -textvariable => \$value )->pack; my $lb = $mw->Listbox( -height => 0 )->pack; $lb->insert( 'end', qw/one two three four five six/ ); $lb->bind( '<ButtonPress-3>', [ \&setItem, \$value, Ev('@') ] ); sub setItem { my ( $lb, $valSR, $xy ) = @_; $lb->selectionClear( 0, 'end' ); my $index = $lb->index($xy); if ( defined($index) ) { $lb->selectionSet($index); $$valSR = $lb->get($index); } } MainLoop;

    I'm not really a human, but I play one on earth. Cogito ergo sum a bum
Re: selecting in listbox by right click mouse
by ikegami (Patriarch) on Aug 28, 2007 at 06:23 UTC

    hello, i want to know how do i select a line in a listbox by the right click of the mouse. ...

    What system? (Tk? Wx? ...)

    (For list boxes in HTML forms, you'll need something that runs in the browser such as JavaScript. Perl won't be of use except on the few machines that have PerlScript installed.)

    Update: The root post was changed. The original question made no mention of Tk. I've added a quote from the original version.

Re: selecting in listbox by right click mouse
by sagibeli (Novice) on Aug 28, 2007 at 06:29 UTC
    i'm working in perk TK
      this is how listbox does it
      sub ClassInit { my ($class,$mw) = @_; $class->SUPER::ClassInit($mw); # Standard Motif bindings: $mw->bind($class,'<1>',[sub { my $w = shift; if (Tk::Exists($w)) { $w->BeginSelect(@_); } }, Ev('index',Ev('@'))]); $mw->bind($class, '<Double-1>' => \&Tk::NoOp); $mw->bind($class,'<B1-Motion>',['Motion',Ev('index',Ev('@'))]); $mw->bind($class,'<ButtonRelease-1>','ButtonRelease_1'); ; $mw->bind($class,'<Shift-1>',['BeginExtend',Ev('index',Ev('@'))]); $mw->bind($class,'<Control-1>',['BeginToggle',Ev('index',Ev('@'))]); $mw->bind($class,'<B1-Leave>',['AutoScan',Ev('x'),Ev('y')]); $mw->bind($class,'<B1-Enter>','CancelRepeat'); $mw->bind($class,'<Up>',['UpDown',-1]); $mw->bind($class,'<Shift-Up>',['ExtendUpDown',-1]); $mw->bind($class,'<Down>',['UpDown',1]); $mw->bind($class,'<Shift-Down>',['ExtendUpDown',1]); $mw->XscrollBind($class); $mw->bind($class,'<Prior>', sub { my $w = shift; $w->yview('scroll',-1,'pages'); $w->activate('@0,0'); }); $mw->bind($class,'<Next>', sub { my $w = shift; $w->yview('scroll',1,'pages'); $w->activate('@0,0'); }); $mw->bind($class,'<Control-Prior>', ['xview', 'scroll', -1, 'pages']) +; $mw->bind($class,'<Control-Next>', ['xview', 'scroll', 1, 'pages']) +; # <Home> and <End> defined in XscrollBind $mw->bind($class,'<Control-Home>','Cntrl_Home'); ; $mw->bind($class,'<Shift-Control-Home>',['DataExtend',0]); $mw->bind($class,'<Control-End>','Cntrl_End'); ; $mw->bind($class,'<Shift-Control-End>',['DataExtend','end']); # XXX What about <<Copy>>? Already handled in Tk::Clipboard? # $class->clipboardOperations($mw,'Copy'); $mw->bind($class,'<space>',['BeginSelect',Ev('index','active')]); $mw->bind($class,'<Select>',['BeginSelect',Ev('index','active')]); $mw->bind($class,'<Control-Shift-space>',['BeginExtend',Ev('index','a +ctive')]); $mw->bind($class,'<Shift-Select>',['BeginExtend',Ev('index','active') +]); $mw->bind($class,'<Escape>','Cancel'); $mw->bind($class,'<Control-slash>','SelectAll'); $mw->bind($class,'<Control-backslash>','Cntrl_backslash'); ; # Additional Tk bindings that aren't part of the Motif look and feel: $mw->bind($class,'<2>',['scan','mark',Ev('x'),Ev('y')]); $mw->bind($class,'<B2-Motion>',['scan','dragto',Ev('x'),Ev('y')]); $mw->MouseWheelBind($class); # XXX Both needed? $mw->YMouseWheelBind($class); return $class; }
Re: selecting in listbox by right click mouse
by erroneousBollock (Curate) on Aug 28, 2007 at 06:20 UTC
    It would seem that this question bears no relation to perl at all.

    Ah, question looks reasonably put now.

    -David.