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

Hi Monks,

Can you (zantara;) explain to me why the below code does not work?

use Tk; my $mw = new MainWindow; $mw->bind('all' => '<Key-Escape>' => sub {exit;}); # If below line is commented out, program works. $mw->FullScreen(1); MainLoop;

I expect this program to exit when the escape key is pressed, but it does not. No key bindings work in this manner, but if I change it to a mouse binding, say <1> it works.

However if I do this with a window that is not MainWindow, like so:

use Tk; my $mw = new MainWindow; my $tl = $mw->Toplevel; $tl->bind('all' => '<Key-Escape>' => sub {exit;}); $tl->FullScreen(1); MainLoop;

it works! .. What gives? Obviously I can work around this, but is it possible to have the key bindings with MW in fullscreen?.. cheers!

Replies are listed 'Best First'.
Re: Tk fullscreen mainwindow key bindings
by zentara (Cardinal) on Nov 02, 2008 at 14:30 UTC
    I believe it's a bug. I noticed that if I hit control-c, the underlying window would have focus and kill the window. I grepped thru the Tk-804.027_502 distribution for Fullscreen and came up with interesting items
    local $TODO = "-alpha and -fullscreen not yet implemented"; Getting fullscreen attribute is not yet implemented for X11 {setting/unsetting fullscreen does not change the focus} {change -fullscreen after map}); # Query above should not clear fullscreen state and many more such hits
    So..... probably there is a problem with focus changing after mapping the $mw. Interestingly enough, you can't even use Fullscreen after the $mw is mapped
    #!/usr/bin/perl use Tk; my $mw = new MainWindow; $mw->after(10,sub { $mw->FullScreen(1); $mw->bind('all'=> '<Key-Escape>' => sub {exit;}); $mw->focusForce; $mw->update; }); MainLoop;
    Maybe there has been a fix in Tk-804.028, but I see nothing in the changelog. So there are a couple of hacks you can use:
    #!/usr/bin/perl use Tk; my $mw = new MainWindow; $mw->bind('all' => '<Key-Escape>' => sub {exit;}); $mw->FullScreen(1); $mw->grabGlobal; $mw->focusForce; MainLoop;
    or better yet, to avoid the globalgrab, use the old fashioned way of setting geometry for the $mw
    #!/usr/bin/perl use Tk; my $mw = new MainWindow; $mw->geometry($mw->screenwidth . 'x' . $mw->screenheight . '+0+0'); $mw->bind('all' => '<Key-Escape>' => sub {exit;}); MainLoop;
    I guess the lesson is, don't count on Fullscreen working. Use the geometry instead. Maybe Slaven Rezic will see this and chime in, as he is the current owner of Tk and knows the xs for converting from the tcl.

    I'm not really a human, but I play one on earth Remember How Lucky You Are
      grabGlobal did the trick - I want a borderless fullscreen window (as the end-user will never be exposed to the underlying system). Thanks yet again :-)