On Fri, 07 Oct 2011, Barry Brevik wrote:
> I'm writing a program where a process runs in a loop. I want to
> process keyboard input without disturbing the main process in the
> loop. I'm trying to use the Win32::Console module for this task (see
> code below), but the module blocks on the Input statement.
>
> Is there some way to make this non-blocking, or maybe even use a
> different technique entirely that does not block? I tried whipping an
> IOCTL statement on it, but I either did it wrong, or it does not work.
>
> use strict;
> use warnings;
> use Win32::Console;
>
> my $STDIN = new Win32::Console(STD_INPUT_HANDLE);
> $STDIN->Mode(ENABLE_PROCESSED_INPUT);
>
> # Un-buffer STDOUT.
> select((select(STDOUT), $| = 1)[0]);
>
> while (1)
> {
> my @input = $STDIN->Input();
I think you wanted to call PeekInput() instead of Input() here.
But GetEvents() may be even better if you only want to see if there
are waiting keyboard events at all.
> if (defined $input[0] and $input[0] == 1)
> {
> if ($input[1])
> {
> last if $input[5] == 27; # ESC key.
> if ($input[5] == 8) {print "\x08", ' ', "\x08"; next;} #
> Backspace key.
> print chr($input[5]);
> }
> }
> }
Cheers,
-Jan
_______________________________________________
Perl-Win32-Users mailing list
[email protected]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs