use strict;
use warnings;
use Win32::Console;
use Time::HiRes qw(sleep);
my $OUT = new Win32::Console(STD_OUTPUT_HANDLE);
my $IN = new Win32::Console(STD_INPUT_HANDLE);
my $save = $IN-> Mode;
END { $IN-> Mode( $save )};
$IN-> Mode(( $save | 0x0010 ) & ~0x0040 ); # +MOUSE, -QUICK_EDIT
$OUT->Write("Perl version $^V \n");
$OUT->Write("more text could go here\n\n");
my $n_buttons = $IN->MouseButtons();
$OUT->Write("Your mouse has $n_buttons buttons\n");
$IN->Mode(ENABLE_MOUSE_INPUT);
my @info = $IN->Info();
$OUT->Write(sprintf "IN info:(%s)\n", "@info");
@info = $OUT->Info();
$OUT->Write(sprintf "OUT info:(%s)\n", "@info");
my $counter = 0;
my %MouseFlags = (
FIRST_CLICK => 0,
MOUSE_MOVED => 1,
DOUBLE_CLICK => 2,
MOUSE_WHEELED => 4,
MOUSE_HWHEELED => 8,
);
@MouseFlags{values %MouseFlags} = keys %MouseFlags; # reverse mapping
while(1) {
if($IN->GetEvents) {
@console_events = $IN->Input();
local $" = ",";
if( 2 == ($console_events[0]//0) ) {
$OUT->Write(sprintf "Mouse (@console_events): (%d,%d) Btn=%d Flag=%d:%s\r", @console_events[1..3], $console_events[5], $MouseFlags{$console_events[5]}); # I switched to \r so that the mouse events wouldn't flood the screen, which was causing the mouse y coordinate reported to be confusing, since it was scrolling the window, and it's mouse-coordinate relative to the TOP of the console history
} elsif ( 1 == ($console_events[0]//0) ) {
$OUT->Write("Keyboard (@console_events)\n", );
exit if 27 == ($console_events[3]//0); # ESC = 27
} else {
$OUT->Write("Unknown (@console_events)\n");
}
}
}
####
... # the code before the while loop is identical to the program listing above
use Time::HiRes qw/sleep/
while(1) {
if($IN->GetEvents) {
@console_events = $IN->Input();
...
} else {
sleep 0.01;
}
}
##
##
...
while(1) {
@console_events = $IN->Input();
...
}
##
##
use strict;
use warnings;
use Win32::Console;
my $OUT = new Win32::Console(STD_OUTPUT_HANDLE);
my $IN = new Win32::Console(STD_INPUT_HANDLE);
my @info = $IN->Info();
$OUT->Write(sprintf "IN info:(%s)\n", "@info");
@info = $OUT->Info();
$OUT->Write(sprintf "OUT info:(%s)\n", "@info");
__END__
IN info:()
OUT info:(150 9001 0 15 7 0 0 149 29 150 71)