I am writing an app that continously loops looking for files to appear
in a certain directory, and when they do, it reads those files and does
some work with them.
...BUT...
...the app also needs to watch for keystrokes in a non-blocking way. If
a key is waiting, it can do something with it and if not, it continues
to look for files.
So, I came up with the code shown below which works great except for one
thing. If I ALT-TAB to another window and then ALT-TAB back, the "while"
loop is still running, but the PeekInput() call does not detect
keystrokes anymore.
Does anyone have an idea on how to fix this?
-Barry Brevik
===========================================
use strict;
use warnings;
use Win32::Gui;
use Win32::Console;
my $STDOUT = new Win32::Console(STD_OUTPUT_HANDLE);
my $STDIN = new Win32::Console(STD_INPUT_HANDLE);
$STDIN->Mode(ENABLE_PROCESSED_INPUT);
my @event = ();
my @input = ();
my $starttime = time();
my $lasttime = time();
$STDOUT->Write("Starting 60 second loop.\n\n");
while (1)
{
Win32::GUI::DoEvents();
Win32::Sleep(0);
unless ($lasttime == time())
{
$lasttime = time();
$STDOUT->Write("...another second has expired.\n");
}
if (time() - $starttime >= 60) {last;}
@event = $STDIN->PeekInput();
if (scalar @event)
{
my @input = $STDIN->Input();
if (defined $input[0] and $input[0] == 1)
{
my $char = '';
my $keyval = $input[5];
if ($input[5] > 31 && $input[5] < 127) {$char = chr($input[5]);}
print "\nUp/Down..: ", $input[1] ? 'down' : 'up', "\n";
print "Count....: $input[2]\n";
print "Key Code.: $input[3]\n";
print "Scan Code: $input[4]\n";
print "Key Value: $input[5]\n";
print "Key Char.: $char\n";
printf "Modifiers flags: %X\n\n", $input[6];
last if $input[5] == 27;
}
}
}
_______________________________________________
Perl-Win32-Users mailing list
[email protected]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs