Reputation: 25117
When I press the Up
-key, this script (Term::TermKey) outputs You pressed: <Up>
.
#!/usr/bin/env perl
use warnings;
use 5.012;
use Term::TermKey;
my $tk = Term::TermKey->new( \*STDIN );
say "Press any key";
$tk->waitkey( my $key );
say "You pressed: <" . $tk->format_key( $key, 0 ) . ">";
How could I reach the same result with Win32::Console?
I tried this, but it worked only on "normal" keys like l
,k
, ... but not with keys like Up
, Delete
, ...
use Win32::Console;
my $in = Win32::Console->new(STD_INPUT_HANDLE);
$in->Mode(ENABLE_PROCESSED_INPUT);
my $result = $in->InputChar(1);
say "<$result>";
How could I make work keys like Up
, Delete
, ... too with Win32::Console
?
Upvotes: 3
Views: 632
Reputation: 386361
The up key does not result in a character. InputChar
cannot possible return it. You need to use Input
.
my $con_in = Win32::Console->new(STD_INPUT_HANDLE);
for (;;) {
my @event = $con_in->Input();
my $event_type = shift(@event);
next if !defined($event_type) || $event_type != 1; # 1: Keyboard
my ($key_down, $repeat_count, $vkcode, $vscode, $char, $ctrl_key_state) = @event;
if ($vkcode == VK_UP && ($ctrl_key_state & SHIFTED_MASK) == 0) {
if ($key_down) {
say "<Up> pressed/held down" for 1..$repeat_count;
} else {
say "<Up> released";
}
}
}
See KEY_EVENT_RECORD for more information about keyboard events.
See Virtual-Key Codes to identify keys.
Headers and definitions for above code:
use strict;
use warnings;
use feature qw( say );
use Win32::Console qw( STD_INPUT_HANDLE );
use constant {
RIGHT_ALT_PRESSED => 0x0001,
LEFT_ALT_PRESSED => 0x0002,
RIGHT_CTRL_PRESSED => 0x0004,
LEFT_CTRL_PRESSED => 0x0008,
SHIFT_PRESSED => 0x0010,
VK_UP => 0x26,
};
use constant SHIFTED_MASK =>
RIGHT_ALT_PRESSED |
LEFT_ALT_PRESSED |
RIGHT_CTRL_PRESSED |
LEFT_CTRL_PRESSED |
SHIFT_PRESSED;
Upvotes: 4