From: Dilbert on
On 23 fév, 23:24, Ben Morrow <b...(a)morrow.me.uk> wrote:
> Quoth "Peter J. Holzer" <hjp-usen...(a)hjp.at>:
> > There is in fact one Unix system call (and corresponding Perl builtin
> > function) which has exactly the semantics Dilbert wants:
>
> >     select
>
> Unfortunately select doesn't generally work on Win32, since it doesn't
> have a proper unified fd model. The perl builtin calls a version that
> only works on sockets.
>
> The native Win32 replacement for select(2) is WaitForMultipleObjects,
> which will wait for any HANDLE. This is wrapped by the Win32::IPC
> module.
>
> Arguably perl's select on Win32 ought to use WFMO to provide semantics
> more like Unix' select(2), but it doesn't.

Hurray !
I have resolved my problem - Thanks to everybody who responded

I am now using wait_any() of Win32::IPC, and it works perfectly.

Here is the complete program:

use strict;
use warnings;

use Win32::Console;
use Win32::IPC qw(wait_any);

my $CONS_INP = Win32::Console->new(STD_INPUT_HANDLE);
my @CONS_INL = ($CONS_INP);

LOOP1: while (1) {
# I want to sleep here until a key is pressed...
# How can I achieve this under Windows... ???
# use Win32::IPC does the trick.
# WaitForMultipleObjects([$CONS_INP]); # this works, but is
deprecated.

wait_any(@CONS_INL); # this works and is not deprecated

while ($CONS_INP->GetEvents) {
my @event = $CONS_INP->Input;

local $" = "', '";
print "event = ('@event')\n";

last LOOP1 if $event[5] and $event[5] == 27; # Escape key
}
}
From: sln on
On Wed, 24 Feb 2010 12:37:47 -0800 (PST), Dilbert <dilbert1999(a)gmail.com> wrote:

[snip]

>Hurray !
>I have resolved my problem - Thanks to everybody who responded
>
>I am now using wait_any() of Win32::IPC, and it works perfectly.
>
>Here is the complete program:
>
>use strict;
>use warnings;
>
>use Win32::Console;
>use Win32::IPC qw(wait_any);
>
>my $CONS_INP = Win32::Console->new(STD_INPUT_HANDLE);
>my @CONS_INL = ($CONS_INP);
>
>LOOP1: while (1) {
> # I want to sleep here until a key is pressed...
> # How can I achieve this under Windows... ???
> # use Win32::IPC does the trick.
> # WaitForMultipleObjects([$CONS_INP]); # this works, but is
>deprecated.
>
> wait_any(@CONS_INL); # this works and is not deprecated
>
> while ($CONS_INP->GetEvents) {
> my @event = $CONS_INP->Input;
>
> local $" = "', '";
> print "event = ('@event')\n";
>
> last LOOP1 if $event[5] and $event[5] == 27; # Escape key
> }
>}

Thats great. I'm suprised they had 'WaitForMultipleObjects'
as a method. Its not depricated in vc 2005, thats for sure.

You can also get/set the input console mode like this:
----------------------
my %val2mode = (
'0004' => 'ENABLE_ECHO_INPUT',
'0032' => 'ENABLE_INSERT_MODE',
'0002' => 'ENABLE_LINE_INPUT',
'0016' => 'ENABLE_MOUSE_INPUT',
'0001' => 'ENABLE_PROCESSED_INPUT',
'0064' => 'ENABLE_QUICK_EDIT_MODE',
'0008' => 'ENABLE_WINDOW_INPUT',
'0128' => 'UNKNOWN',
'0256' => 'UNKNOWN'
);
my %mode2val = reverse %val2mode;
my $curmode = $CONS_INP->Mode();

print "Current modes ($curmode):\n";
for my $val (map {1<<$_} 0 .. 10) {
if ($val & $curmode) {
if (exists $val2mode{ $val=sprintf ("%04s", $val) } ) {
print " $val - $val2mode{ $val }\n";
}
}
}
#$CONS_INP->Mode( $curmode | $mode2bit{ENABLE_MOUSE_INPUT}); ...
------------------

Good job!

-sln

Just a side note .., your getting key-down events.
Key-down events are almost never used except in games.
They generate a constant event stream when keys are held down.
I don't know about Win32::Console, but in the bowels of win32
you can filter out key-down events (not control state) before
it ever gets to the event queue (ie: the win32 input event buffer).

Key-up events is what is of normal interest. Its a little
more meaningfull if they can filtered.
Note the control key (states) with each key-up event as well.

Example:
---------------
while ($CONS_INP->GetEvents) {
my @event = $CONS_INP->Input;
if (@event) # no event mean error
{
# KEYS ...
if ($event[0] == 1) {
next unless $event[1]==0; # Only process key-up events

local $" = "', '";
print "event = ('@event')\n";
last LOOP1 if $event[5] == 27; # Escape key
}
# MOUSE ... (if ENABLE_MOUSE_INPUT)
if ($event[0] == 2) {
#
}
}
}