本文介绍了如何使用 Win32::Console 读取特殊键?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我按下 Up 键时,这个脚本 (Term::TermKey) 输出 You press: <Up> .

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 ) . ">";

如何使用 Win32::Console 达到相同的结果?
我试过了,但它只适用于像 lk、...这样的普通"键,但不适用于像 Up、删除, ...

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>";

如何使用 Win32::Console 制作像 UpDelete 等工作键?

How could I make work keys like Up, Delete, ... too with Win32::Console?

推荐答案

向上键不会产生字符.InputChar 无法返回它.您需要使用 Input.

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";
      }
   }
}

参见 KEY_EVENT_RECORD 了解有关键盘事件的更多信息.

See KEY_EVENT_RECORD for more information about keyboard events.

参见 虚拟密钥用于识别密钥的代码.

上述代码的标题和定义:

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;

这篇关于如何使用 Win32::Console 读取特殊键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-11 04:24