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

I'm running a perl script for text analysis purposes and I want to be able to swap to that script from another application (eg Word). I was hoping I could press a combination of keys like Ctrl+ a letter for the perl script to come back up and get focused. Does anyone know how to write that in Perl ? Is there a special method that makes the perl script read keyboard input even if it's not the active process ?
  • Comment on How can I read keyboard "in the background"

Replies are listed 'Best First'.
Re: How can I read keyboard "in the background"
by Corion (Patriarch) on Jun 10, 2003 at 10:30 UTC

    How about ALT+TAB ?

    perl -MHTTP::Daemon -MHTTP::Response -MLWP::Simple -e ' ; # The $d = new HTTP::Daemon and fork and getprint $d->url and exit;#spider ($c = $d->accept())->get_request(); $c->send_response( new #in the HTTP::Response(200,$_,$_,qq(Just another Perl hacker\n))); ' # web
Re: How can I read keyboard "in the background"
by zentara (Cardinal) on Jun 10, 2003 at 15:23 UTC
    It seems to me you are looking for an "event -driven" script. Tk does this, it will enter an "event loop" and watch for various types of events. A real simple example:
    #!/usr/bin/perl use Tk; my $mw = new Tk::MainWindow; my $btn = $mw->Button(-text => 'Hit Return')->pack(); # # Note that we *usually* bind to the main window, # not the button - it's a focus thing. # $mw->bind('<Return>', \&_on_rtn); MainLoop; exit(0); sub _on_rtn { $mw->messageBox(-message=>'Thanx for using the keyboard!'); }
      Well it's not quite what I'm looking for : I'd like for the window to react (just like in your script) whether it's active ot not (focused or blurred) which isn't the case in your script.
      If for example you're working in MS Word and you press the return key, then MS Word solely deals with it whereas I'd like for the perl script in the background to react as well. Thanks for your help !
        I think you'll want to write a Word macro or some VB or VBA that will call your Perl script, passing it the current application and filename.
        I think you might be looking for a way to bind keys at the x server level. Something like hit F11 and "someapp" jumps into focus. How about xbindkeys?
        I beleive that what you want would require rewriting the Windows keyboard drivers or at least piggybacking the interrupts.
hotkey - with a shortcut
by Foggy Bottoms (Monk) on Jun 12, 2003 at 13:49 UTC
    The only way I found so far is with a windows shorcut configured so that it reacts to a key combination. The only problem is that it'll only work if the shorcut's on the desktop. It isn't really a great way, but for the meantime, it'll have to do.