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

I have a Perl/TK application that works fine with all of the key bindings when it is in Focus, but I need a way to force the same bindings (KeyPress and KeyRelease) to work when the application is minimized or FocusOut. Please tell me there is a way...

Replies are listed 'Best First'.
Re: Perl/TK KeyPress/Release minimized
by Joost (Canon) on Mar 22, 2008 at 01:47 UTC
      That is pretty much what I want to do. I have emulated a similar application that runs on windows and does exactly what I am trying to do. I am running this on Linux (Puppy 3.01 if it matters). I simply want to hijack the arrow keys while my application is minimized.
Re: Perl/TK KeyPress/Release minimized
by zentara (Cardinal) on Mar 22, 2008 at 13:32 UTC
    Tk's grabglobal is pretty "all-or-nothing", meaning it will grab the entire window manager( all virtual desktops). Gtk2 will allow only grabbing a single virtual desktop, so you may want to look at Gtk2 if the following Tk script is too invasive for you.

    Try this(Escape key is the only way to close):

    #!/usr/bin/perl use Tk; $mw = MainWindow->new; # the window must be mapped for a global grab # so make a 1 pixel window in lower right corner $mw->geometry('1x1-1-1'); $mw->bind("<Key>", [ \&process_key_press , Ev('K') ] ); $mw->bind("<KeyRelease>", [ \&process_key_release , Ev('K') ] ); $mw->after(100,sub{$mw->grabGlobal;}); $mw->focusForce; MainLoop; sub process_key_press{ my ($caller, $key) = @_; print "Press $key\n"; if ($key eq 'Escape'){exit} } sub process_key_release{ my ($caller, $key) = @_; print "Release $key\n"; }

    I'm not really a human, but I play one on earth. Cogito ergo sum a bum
      Zentara, I think he was after a global hotkey, which works regardless of which app has focus. AFAIK there is no uniform way of doing this on linux, it depends on the window manager in use.
        Zentara, Thank you for the example. It doesn't quite do the trick though. I need something that allows me to open another application even when this is running and your example does not seem to allow this. Let me explain exactly what I'm trying to do: I have a Pan/Tilt/Zoom camera control application built with Perl/Tk. I need to use it with a web browser opened to a webcam site. Everything works very well except that when the control application looses focus, the bindings to the keys (up, down, left, right arrows) stop working until the application gets focus again. A hotkey won't work either unless I change my application to run as a daemon so it can listen to external commands as far as I can tell. I don't want more that one application to run as I'd like to keep it simple and easily portable to other machines.