【问题标题】:Is there something similar to "keypad" without using "Curses"?有没有类似于“键盘”而不使用“诅咒”的东西?
【发布时间】:2012-03-28 17:54:35
【问题描述】:

Curses之外有没有类似keypad(1)的东西? 我想写这样的东西,但不使用Curses,也不自己处理转义序列。

#!/usr/bin/env perl
use warnings;
use 5.012;
use Curses;

initscr();
raw();
printw( qq{Press "Delete"} );
noecho();
keypad(1); 
my $c = getch();
endwin();

if ( $c =~ /\A330\z/ ) {
    say "OK";
} else {
    say qq{You didn't press "Delete"};
}

当我使用Term::ReadKey 时,它的行为有所不同:

#!/usr/bin/env perl
use warnings;
use 5.012;
use Term::ReadKey;

ReadMode('raw');

print qq{Press "Delete" };
while ( 1 ) {
    my $c = ReadKey( 0 );
    last if $c eq 'q';
    say "<$c>";
}

ReadMode('normal');

按“删除”后的输出:

Press "Delete" <
<[>
<3>
<~>

【问题讨论】:

  • Term::ReadKey 不这样做?
  • 它确实做到了,但工作量更大。

标签: perl console terminal


【解决方案1】:

你想避免什么诅咒?

您可能会发现Term::TermKey 很有用。它是libtermkey 库的 Perl 接口,用于处理键盘控制字符和多字节转义序列以及 UTF-8 字符。

【讨论】:

【解决方案2】:

正如Borodin 已经提到的,Term::TermKey 可能会有所帮助:

use warnings;
use 5.012;
use Term::TermKey;

my $tk = Term::TermKey->new( \*STDIN );

print qq{Press "Delete" };
while( 1 ) {
   $tk->waitkey( my $key );
   say "<", $tk->format_key( $key, 0 ), ">";
}

给予

Press "Delete" <Delete>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-08-02
    • 2016-03-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多