http://qs321.pair.com?node_id=11143297


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

Hi Anonymous, thanks for replying so quickly. Actually, I had seen that sample code previously, and had downloaded and run it, and found that although it displayed amazing graphics, it too did not respond to my mouse clicks. I also noticed that it did not have a $console -> Alloc () instruction anywhere, which I initially thought might be why neither it nor my program responded to mouse events, because I did not have a $console -> Alloc either. However, when I added a $console -> Alloc () instruction to my code, the window in which my program was running would instantly vanish as soon as the $console -> Alloc instruction was reached. So I fiddled with $console -> Free () with it for a while, to no avail, and finally removed the $console -> Alloc instruction again.

I have now revised my code to eliminate the 1-second sleep. By commenting one instruction or not, I can make it sleep only a hundredth of a second or not sleep at all, with the results being the same, viz. no mouse events are detected. This is the revised code:

use strict; use warnings; use Time::HiRes qw (usleep); use Win32; use Win32::Console; my $console; my @console_event; undef $/; unless ($console = new Win32::Console STD_INPUT_HANDLE) { print STDERR "\nSomething has gone wrong with the Win32::Conso +le constructor: $!\n\n"; die; } $console -> Mode (ENABLE_MOUSE_INPUT); print "Perl version $^V running on ", join (" ", Win32::GetOSName), ". +\n\n"; print "Your mouse has ", $console->MouseButtons(), " buttons.\n\n"; print "Go ahead, make my day ...\n\n"; my $start_time = time (); my $when_to_stop_waiting = $start_time + 15; while (time () < $when_to_stop_waiting) { if ($console -> GetEvents ()) { @console_event = $console -> Input (); print "A console event has been detected. Its attribu +tes are the following:\n\n"; print "$_\n" for @console_event; print "\nGood job.\n"; exit; # exit the program, do not fall through to t +hat final print instruction } else { usleep 100; # sleep for 10 milliseconds, i.e. a +hundredth of a second, to prevent # this program from getting into a r +eally tight loop and crowding out # everyone else, but even without th +is usleep instruction the mouse # clicks are still not detected } } print "The time to stop waiting has been reached. Your input was not d +etected.\n"; print "Better luck after seeking the wisdom of the monks.\n";

When I run this revised version and press any key, the following is generated:

Go ahead, make my day ... A console event has been detected. Its attributes are the following: 1 1 1 70 33 102 32 Good job.

But as before, I can click on the mouse all night, and this is what is generated:

H:\sandbox>perl capture_mouse_events.pl Perl version v5.32.0 running on Win10 Build 18363 (64-bit). Your mouse has 16 buttons. Go ahead, make my day ... The time to stop waiting has been reached. Your input was not detected +. Better luck after seeking the wisdom of the monks. H:\sandbox>

I've thought about re-writing my application in Win32::GUI but it would involve writing a lot more code creating and managing the window entirely within the program, not just hooking into a pre-existing console as with Win32::Console.