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

发布于 2024-12-15 21:10:13 字数 974 浏览 2 评论 0原文

#!/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;
}

当我尝试这个时(在这里找到:如何更新屏幕上的值而不在 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 不适用于我的终端?

#!/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;
}

When I try this (found here: How can I update values on the screen without clearing it in Perl?) I get this output:

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

Does this mean, that key-up doesn't work with my terminal?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

深爱不及久伴 2024-12-22 21:10:13

您误解了 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 手册 有帮助。它解释了所有功能的含义。

You've misunderstood the ku capability. That's the character sequence generated when the user presses the up arrow key on the terminal. To actually move the cursor up on the screen, you print the up capability. (Also, it's best to avoid the indirect object syntax, although that had nothing to do with your problem.)

Here's a corrected version:

#!/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;
}

You may find the Termcap manual helpful. It explains what all the capabilities mean.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文