http://qs1969.pair.com?node_id=11143452


in reply to Re: Unable to capture mouse events in Win32::Console
in thread Unable to capture mouse events in Win32::Console

I feel partly responsible for confusion and code re-writes caused by my words that "GetEvents and sleep are not required". I don't know if you indeed tried to exclude both to find timeout never occurs if user is inactive; then restored just "peeking" into the queue (GetEvents) to detrimental effect, and finally restored the original. Maybe this timeout is crucial to your application. Maybe the above never happened, it's just the (wrong) idea of

GetEvents returned just a single scalar ... would be more efficient

Maybe you tried to timeout through alarm (I think it is somewhere in FAQ) to find it doesn't work with blocking I/O on Windows. Maybe I'm partially or totally wrong, you are not interested in "things async", peeking/blocking, etc. Code finally working as desired is important (which is absolutely right!), goal achieved. Well, so it's to my amusement and sleep-not-really-OK bias I found that timed-out Win32::Console::Input can be done like this:

use strict; use warnings; use feature 'say'; use Win32::Console; use Win32::IPC 'wait_any'; use Time::HiRes 'time'; my $IN = Win32::Console-> new( STD_INPUT_HANDLE ); my $max = 15; my $count = 0; my $t = time + $max; while ( wait_any @{[ $IN ]}, 1000 * ( $t - time )) { my @ev = $IN-> Input; say "Ah ah ah, you didn't press the magic key <--- @ev"; ++ $count } say $count ? "Enough, I'm tired! $count events in $max seconds!" : "You failed to press ANY key in $max seconds! Read my FAX!!!";