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

Hi Monks, I've created a menu using the following command. $file -> command ( -label => '~New', -accelerator => 'Ctrl-n', -command => \&createNew ); But Ctrl+N is not invoking the callback 'createNew'. I don't know if I'm missing something for it to work... Thanks

Replies are listed 'Best First'.
Re: perl tk [menu accelerator]
by zentara (Cardinal) on Jun 29, 2009 at 16:42 UTC
    Show your actual code. You may have to resort to an invoke trick, like the following.
    #!/usr/bin/perl use warnings; use strict; use Tk; my $mw = tkinit; my $t = $mw->Text->pack; my $b = $mw->Button( -text => 'Press Me', -command => \&foo, )->pack; + $mw->bind('<Control-a>', [$b => 'invoke']); MainLoop; sub foo { $t->insert('end', "Thank you\n"); $t->see('end'); }

    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku
Re: perl tk [menu accelerator]
by Marshall (Canon) on Jun 29, 2009 at 17:00 UTC
    I haven't used this -accelerator option, but have used various bindings to get this sort of behavior. Maybe this is obvious, I don't know know, but I've tripped on this "landmine" before!

    The issue is whether or not your GUI application can even get the CTL-N to begin with! This will not happen unless your app window "has mouse focus". When you are testing this, click on the "title bar" of the Tk app before you test entering CTL-N. Also, for debugging, put something like -command => sub{"print ctl-n received\n";}. If you can get this working with mouse focus on your window and want this to work without mouse focus, then a "higher level" of keyboard mapping needs to occur and that is possible, but happens at the overall window manager level rather than at the object level.

      Thanks All. I used something like this $mw->bind($mw, "<Control-n>" => \&createNew); where 'mw' is my main window and createNew is my callback. And it worked fine for me. Thanks Again.
Re: perl tk [menu accelerator]
by Anonymous Monk on Jun 29, 2009 at 16:44 UTC