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

Hi, The following code is from Mastering Perl/Tk. It is slightly modified to suit my code. However accelerators (Ctrl-*) do not work. Did anybody have the same problem before? Thanks.
map {$menubar->cascade( -label => '~' . $_->[0], -menuitems => $_->[1] + )} ['File', file_menuitems], ['Edit', edit_menuitems]; #my $file = $menubar->cascade( # -label => '~File', -menuitems => file_menuitems); # #my $edit = $menubar->cascade( # -label => '~Edit', -menuitems => edit_menuitems); # #my $help = $menubar->cascade( # -label => '~Help', -menuitems => help_menuitems); if ($^O eq 'MSWin32') { my $syst = $menubar->cascade(-label => '~System'); my $dir = 'dir | sort | more'; $syst->command( -label => $dir, -command => sub {system $dir}, ); } my $file_menu = $menubar->entrycget('File', -menu); sub edit_menuitems { [ [qw/command ~Undo -accelerator Ctrl-u -c +ommand/ => \&undo_text], [qw/command ~Redo -accelerator Ctrl-r -c +ommand/ => \&redo_text], '', [qw/command ~Copy -accelerator Ctrl-c +-command/ => \&copy_text], [qw/command ~Cut -accelerator Ctrl-x +-command/ => \&cut_text], [qw/command ~Paste -accelerator Ctrl-p +-command/ => \&paste_text], '', [qw/command/, '~Select All', qw/ -accelerator Ct +rl-s -command/ => \&select_all], [qw/command/, 'S~electToLine ...', qw/ -accelerator +Ctrl-e -command/ => \&select_line], [qw/command ~GoTo -accelerator Ctrl-g - +command/ => \&goto_line], [qw/command/, '~Line Number', qw/ -accelerator Ctrl- +l -command/ => \&line_number], '', [qw/command ~Find -accelerator Ctrl-f - +command/ => \&find], [qw/command/, 'F~ind Replace', qw/ -accelerator Ctrl +-i -command/ => \&find_replace], ]; } sub file_menuitems { # Create the menu items for the File menu. [ [qw/command/, '~Load Files for Sorting', qw/ -accelerator + Ctrl-l -command/ => \&load_file], [qw/command/, '~Unload Files', qw/ -accelerator C +trl-u -command/ => \&unload_file], [qw/command/, '~Open a File to View', qw/ -accele +rator Ctrl-o -command/ => \&open_file], '', [qw/command ~Save -accelerator Ctrl-s + -command/ => \&save_file], [qw/command/, 'S~ave As ...', qw/ -accelerato +r Ctrl-a -command/ => \&save_file_as], '', [qw/command ~Close -accelerator Ctrl-w + -command/ => \&close_file], '', [qw/command ~Quit -accelerator Ctrl-q + -command/ => \&exit], ]; } # end file_menuitems

Replies are listed 'Best First'.
Re: Menubars the slick, sophisticated, New-Fashioned way
by converter (Priest) on Nov 20, 2002 at 00:21 UTC

    Your accelerators should work fine when the menus are posted. If you want the key combinations to work as shortcuts without the menus posted you'll want to bind them to the Toplevel:

    $mw->bind('<Control-l>', \&load_file); # Ctrl-L to load a file
      Thanks, binding accelerators worked. I wish the authors put this information in their book. It would save me a lot of time.
Re: Menubars the slick, sophisticated, New-Fashioned way
by BrowserUk (Patriarch) on Nov 19, 2002 at 23:31 UTC

    You should specify your accelerators like this

    -accelerator => 'Ctrl-w',
    I'm guessing you are not useing strict, else I think you would be getting error msgs indication the problems and the solution.

    Okay you lot, get your wings on the left, halos on the right. It's one size fits all, and "No!", you can't have a different color.
    Pick up your cloud down the end and "Yes" if you get allocated a grey one they are a bit damp under foot, but someone has to get them.
    Get used to the wings fast cos its an 8 hour day...unless the Govenor calls for a cyclone or hurricane, in which case 16 hour shifts are mandatory.
    Just be grateful that you arrived just as the tornado season finished. Them buggers are real work.