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

Hey Monks,

I was just wondering, loving tk and all... if there is a way to bind keys so they are GLOBAL? Meaning that the application doesnt even have to have focus (the gui can even be ->withdraw'n), and the keycombination you define works "activating" something you want in the perl application. I would prefer in Tk, but any way is fine. Or many ways! (guess this works doing with the Win32::GUI, although I have no clue how to do it)

Thanks Experts,
Ace

Replies are listed 'Best First'.
Re: Creating keybinds GLOBALY!
by anonymized user 468275 (Curate) on Aug 04, 2005 at 07:47 UTC
    Setting up Tk event callbacks, including key pressing events are explained thoroughly in the chapter 'Anatomy of the main loop' of O'Reilly's 'Mastering perl/Tk'. ISBN 1565927168

    Update: the chapter is available on line: http://www.oreilly.com/catalog/mastperltk/chapter/ch15.html - to find the relevant section keystroke events come under the event class of 'window events'

    One world, one people

      Ah, yes. That binding I know of... However, it doesnt work when window is out of focus... (as I recall)
        If the window is out of focus, Windows will deliver the keystrokes to whichever application it considers to be in focus. That fact has nothing to do at all with whatever perl method you use to capture the keystrokes. In fact that behaviour is user-configurable under linux and windows and is therefore entirely beyond the power of the programmer (unless you hack the O/S and invent your personal GUI paradigm).

        One world, one people

        If you set up a shortcut to the perl/tk app. on the desktop, then you can also setup a shortcut ket that will start the app regardless of what application currently has the focus.


        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
        "Science is about questioning the status quo. Questioning authority".
        The "good enough" maybe good enough for the now, and perfection maybe unobtainable, but that should not preclude us from striving for perfection, when time, circumstance or desire allow.
Re: Creating keybinds GLOBALY!
by Anonymous Monk on Aug 04, 2005 at 05:57 UTC
      Hmm, that is more logging all keys.. I want it to react just on... say.. CTRL+P.
Re: Creating keybinds GLOBALY!
by davis (Vicar) on Aug 04, 2005 at 08:41 UTC
    You want to run a specific task when you press a specific key? I'd use AutoHotkey
    Update: Fixed URL.

    davis
    Kids, you tried your hardest, and you failed miserably. The lesson is: Never try.
      That url gets me to microsoft.com...
      Anyway, I want it in perl... Say we got this simple thing:
      use Tk; my $main = new MainWindow(-title => "Global Testing"); $main->bind("<KeyPress>", sub {print "Hello there!"}); MainLoop;
      Now, the binding doesnt work when window is out of focus... (as someone said is impossible to make working in tk in some other thread here at PerlMonks...)
        Anyway, I want it in perl...
        I really have to ask... why?
        You can make AutoHotkey run any abitrary program. I've used it to launch perl scripts, .bat scripts, etc. And yes, you're correct, you can't use Tk to bind a keypress when the window's out of focus. (At least, not that I know of)

        davis
        Kids, you tried your hardest, and you failed miserably. The lesson is: Never try.
      Looks interesting... Fixed URL is here.

      Update: URL fixed in post by davis above

      -Scott