From: Dilbert on
I have a perl program (under windows vista) that records every
scancode / keystroke I make (it even records when I hit the Ctrl-key,
the Shift-key, F1, F2, etc....

So the program works ok for me, but it has a "do{...}until" loop that
better should be replaced by a sleep command. My concern is that I
waste a lot of CPU cycles in the do{...}until loop, that could be
avoided if I replace that loop by a sleep command

How can I achieve this ? does such a command exist under windows ?
(just in case you might ask: I already tried each and every option in
Term::Readkey, it definitely does not record when I hit the Ctrl-key,
Shift-key, etc...) Fortunately, Win32::Console does the job quite
well, it is just the do{...}until loop that bothers me because it
wastes CPU cycles.

Here is the program:

use strict;
use warnings;

use Win32::Console;

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

while (1) {

# I want to sleep here until a key is pressed...
# How can I achieve this under Windows... ???

my @event;
do {
@event = $CONS_INP->Input() if $CONS_INP->GetEvents();
} until @event;

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

last if $event[5] == 27; # Escape key
}

Is there a perl module for windows that allows me to sleep (or wait ?)
until a key is pressed (even if it is the Ctrl-key, or the Shift-key)
From: C.DeRykus on
On Feb 22, 12:23 pm, Dilbert <dilbert1...(a)gmail.com> wrote:
> I have a perl program (under windows vista) that records every
> scancode / keystroke I make (it even records when I hit the Ctrl-key,
> the Shift-key, F1, F2, etc....
>
> So the program works ok for me, but it has a "do{...}until" loop that
> better should be replaced by a sleep command. My concern is that I
> waste a lot of CPU cycles in the do{...}until loop, that could be
> avoided if I replace that loop by a sleep command
>
> How can I achieve this ? does such a command exist under windows ?
> (just in case you might ask: I already tried each and every option in
> Term::Readkey, it definitely does not record when I hit the Ctrl-key,
> Shift-key, etc...) Fortunately, Win32::Console does the job quite
> well, it is just the do{...}until loop that bothers me because it
> wastes CPU cycles.
>
> Here is the program:
>
> use strict;
> use warnings;
>
> use Win32::Console;
>
> my $CONS_INP = Win32::Console->new(STD_INPUT_HANDLE);
>
> while (1) {
>
>     # I want to sleep here until a key is pressed...
>     # How can I achieve this under Windows... ???
>
>     my @event;
>     do {
>         @event = $CONS_INP->Input() if $CONS_INP->GetEvents();
>     } until @event;
>
>     local $" = "', '";
>     print "event = ('@event')\n";
>
>     last if $event[5] == 27; # Escape key
>
> }
>
> Is there a perl module for windows that allows me to sleep (or wait ?)
> until a key is pressed (even if it is the Ctrl-key, or the Shift-key)

Win32 5.10.1 seems to understand sleep();

do {
@event = ...
sleep 1;
} until @event;

At least, hitting Ctrl-C interrupts the above. Don't know if
this'll do everything you need though.

--
Charles DeRykus

From: Dilbert on
On 22 fév, 22:12, "C.DeRykus" <dery...(a)gmail.com> wrote:
> On Feb 22, 12:23 pm, Dilbert <dilbert1...(a)gmail.com> wrote:
> > So the program works ok for me, but it has a "do{...}until" loop that
> > better should be replaced by a sleep command. My concern is that I
> > waste a lot of CPU cycles in the do{...}until loop, that could be
> > avoided if I replace that loop by a sleep command

[snip]

> Win32 5.10.1 seems to understand sleep();
>
>     do {
>         @event = ...
>         sleep 1;
>     } until @event;
>
> At least, hitting Ctrl-C interrupts the above. Don't know if
> this'll do everything you need though.

Thanks to Charles DeRykus for his solution. This is much better for
the CPU cycles.

However, the sleep command as it works currently has one disadvantage:
it does not respond quickly enough to normal keystrokes -- it takes a
second to recognise my keystrokes (except for Ctrl-C, which is
recognised immediately, but this doesn't help because I don't use Ctrl-
C very much)

Is there an advanced sleep command for Windows that sleeps for 1
second *maximum* ?, i.e. if any key was hit during the sleep of 1
second (let's say, for example, I hit the letter 'A' after 0.5
seconds), then the "advanced" sleep should be aborted immediately
(before the 1 second is over) so that the perl program can resumes its
normal processing after 0.5 seconds.

Something like "advanced_sleep_until_keypress_max_seconds(1)" ???

Is there a Win32 module to that effect ???
From: J�rgen Exner on
"C.DeRykus" <derykus(a)gmail.com> wrote:
>Win32 5.10.1 seems to understand sleep();

Well, older versions do, too. For a long time, actually.

> do {
> @event = ...
> sleep 1;
> } until @event;

Yikes. That is still busy waiting. Maybe not quite as bad as the OP's
attempt, but still bad.

No, sorry, I don't have a better solution myself, unfortunately. That is
an area I haven't ventured in so far. But I could imagine that setting
up a signal handler for a keypress event may be a solution.

jue
From: Tad McClellan on
Dilbert <dilbert1999(a)gmail.com> wrote:

> Is there an advanced sleep command for Windows that sleeps for 1
> second *maximum* ?, i.e. if any key was hit during the sleep


I don't think you understand the definition of "sleep" here...

"sleep" means "do not do _anything_".

Monitoring the keyboard is "something", so it cannot be done while sleeping.

That is, if it could notice that something happened on the keyboard,
then it was not truly sleeping.


--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.liamg\100cm.j.dat/"