English 中文(简体)
如何用温32号的特别钥匙读:议会?
原标题:How to read in special keys with Win32::Console?

当我发布<条码>Up-key时,本字母(。 术语:TermKey输出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>";

我怎么能够把工作钥匙如<代码>Up,Delete ,...也与Win32:Console一起?

最佳回答

关键因素不会产生特性。 <代码>InputChar不能返回。 您需要使用<代码>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,用于了解有关关键板事件的更多信息。

Virtual-Key Codes,以查明关键人物。


以上编码的标题和定义:

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;
问题回答

暂无回答




相关问题
Why does my chdir to a filehandle not work in Perl?

When I try a "chdir" with a filehandle as argument, "chdir" returns 0 and a pwd returns still the same directory. Should that be so? I tried this, because in the documentation to chdir I found: "...

How do I use GetOptions to get the default argument?

I ve read the doc for GetOptions but I can t seem to find what I need... (maybe I am blind) What I want to do is to parse command line like this myperlscript.pl -mode [sth] [inputfile] I can use ...

Object-Oriented Perl constructor syntax and named parameters

I m a little confused about what is going on in Perl constructors. I found these two examples perldoc perlbot. package Foo; #In Perl, the constructor is just a subroutine called new. sub new { #I ...

Where can I find object-oriented Perl tutorials? [closed]

A Google search yields a number of results - but which ones are the best? The Perl site appears to contain two - perlboot and perltoot. I m reading these now, but what else is out there? Note: I ve ...

热门标签