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

I'm trying to make a script that can detect keystrokes in Windows without the Tk window having focus.

I would like the be able to press a key - for instance Page Up - while in a game and have that start off the rest of my script. I have the script completed and working, but I can't get it to detect keystrokes unless I minimise the game and click the Tk window to give it focus. I only just started using Tk a few days ago, so if this is simple I apologise (although I couldn't find anything by searching). It doesn't necessarily have to use Tk to capture the keystrokes.

If anyone could tell me if this is possible I would be very grateful.

TIA
Raffles

Replies are listed 'Best First'.
Re: Detecting Keystrokes in Windows
by NetWallah (Canon) on Jul 24, 2004 at 01:24 UTC
    Follow This thread on "Keystoke Logger" for insight into this issue (Not necessarily to solve the problem).

        Earth first! (We'll rob the other planets later)

Re: Detecting Keystrokes in Windows
by davis (Vicar) on Jul 24, 2004 at 10:23 UTC
    I'd use the GPL'd AutoHotkey to program my keyboard buttons to do specific things. Associate "Page Up" with your script, and you're done.

    davis
    It wasn't easy to juggle a pregnant wife and a troubled child, but somehow I managed to fit in eight hours of TV a day.
Re: Detecting Keystrokes in Windows
by ercparker (Hermit) on Jul 24, 2004 at 08:30 UTC
    you can use Term::ReadKey to read characters from STDIN
    use Term::ReadKey; #ctrl-c will exit out ReadMode 'cbreak'; while (1) { my $key = ReadKey(0); print "$key\n"; } ReadMode 'normal';