Search code examples
regexperl

How can I determine if F15 has been pressed?


I have a little console application that among other things checks the status of another operation. Once a second it checks for keypresses using Term::ReadKey. If the 'r' key has been pressed, it refreshes the display:

{   # generate display ...
    print "Press 'r' to refresh, any other key to exit:  ";
    my $resp = readkey();
    print $resp;
    redo if $resp =~ /r/i;
}
exit;

sub readkey {   
    my $key;
    ReadMode('cbreak');
    while (not defined $key) {
        if (defined ($key = ReadKey(-1)) ) {
            exit if $key =~ /\cC/i; # allow Ctrl-C to behave normally
            return $key;
        } else {
            sleep 1;
        }
    }
    ReadMode('normal');
}    

This all works exactly as intended. However, I also use Caffeine to keep my Win 7 display from going to sleep. This utility works by simulating a press of F15 every 59 seconds, thereby never allowing the screensaver to kick in. Although Caffeine's approach is pretty kludgy it has worked very well for me for years. However, like Windows my console app also reads the simulated press of F15 as a real keypress, causing the console app to exit. If I could match against F15, I could filter it out. So, my question:

How can I determine if F15 has been pressed, using Term::ReadKey?

This is on Windows 7 Pro, Strawberry 5.12.3, Term::ReadKey v. 2.30.02.

(I am aware that there may be a significant x-y problem component to my question, and I welcome other solutions. However, I am curious about how one would do this. I can see why I might want to see when a function key has been pressed in other situations.)


Solution

  • You're using a unix-centric module. Use a more appropriate module: Win32::Console, for example.

    You can see how to use it here.