Term :: Cap:“key up”不起作用

时间:2011-11-15 18:05:52

标签: perl terminal termcap keyup

#!/usr/bin/env perl
use warnings;
use 5.014;
use Term::Cap;
use POSIX;

my $termios = new POSIX::Termios;
$termios->getattr;
my $ospeed = $termios->getospeed;

my $terminal = Tgetent Term::Cap { TERM => undef, OSPEED => $ospeed };
$terminal->Trequire("ku");  # move cursor up
my $UP = $terminal->Tputs("ku");
my $t = 500;
while ($t > 0) {
    printf "Hour: %d    \n", $t/3600;
    printf "Minute: %d    \n", ($t/60)%60;
    printf "Second: %d    \n", $t%60;
    print $UP,$UP,$UP;
    sleep 5;
    $t -= 5;
}

当我尝试这个(在这里找到:How can I update values on the screen without clearing it in Perl?)时,我得到了这个输出:

Hour: 0    
Minute: 8    
Second: 20    
AAAHour: 0    
Minute: 8    
Second: 15    
AAAHour: 0    
Minute: 8    
Second: 10    
AAAHour: 0    
Minute: 8    
Second: 5 

这是否意味着,key-up不适用于我的终端?

1 个答案:

答案 0 :(得分:4)

你误解了ku能力。这是用户按下终端上的向上箭头键时生成的字符序列。要实际在屏幕上向上移动光标,请打印up功能。 (另外,最好避免使用间接对象语法,尽管这与您的问题无关。)

以下是更正后的版本:

#!/usr/bin/env perl
use warnings;
use 5.014;
use Term::Cap;
use POSIX;

my $termios = POSIX::Termios->new;
$termios->getattr;
my $ospeed = $termios->getospeed;

my $terminal = Term::Cap->Tgetent({ TERM => undef, OSPEED => $ospeed });
$terminal->Trequire("up");  # move cursor up
my $UP = $terminal->Tputs("up");

my $t = 500;
while ($t > 0) {
    printf "Hour: %d    \n", $t/3600;
    printf "Minute: %d    \n", ($t/60)%60;
    printf "Second: %d    \n", $t%60;
    print $UP,$UP,$UP;
    sleep 5;
    $t -= 5;
}

您可能会发现Termcap manual有帮助。它解释了all the capabilities的含义。