ncurses: 为什么getch不会等待我按键?

4

从ncurses (3) Linux man页面中:

nodelay选项使getch成为非阻塞调用。如果没有准备好的输入,getch将返回ERR。如果禁用(bf为FALSE),则getch将等待按下键。

为什么在我的示例中getch不会等到我按下一个键?


#!/usr/bin/env perl6
use v6;
use NativeCall;

constant LIB = 'libncursesw.so.5';
constant ERR = -1;
class WINDOW is repr('CPointer') { }

sub initscr()                         returns WINDOW is native(LIB) {*};
sub cbreak()                          returns int32  is native(LIB) {*};
sub nodelay(WINDOW, bool)             returns int32  is native(LIB) {*};
sub getch()                           returns int32  is native(LIB) {*};
sub mvaddstr(int32,int32,str)         returns int32  is native(LIB) {*};
sub nc_refresh() is symbol('refresh') returns int32  is native(LIB) {*};
sub endwin()                          returns int32  is native(LIB) {*};

my $win = initscr();  # added "()"
cbreak();
nodelay( $win, False );

my $c = 0;
loop {
    my $key = getch(); # getch() doesn't wait
    $c++;
    mvaddstr( 2, 0, "$c" );
    nc_refresh();
    next if $key == ERR;
    last if $key.chr eq 'q';
}

endwin();

这对我来说似乎在ncurses 6和perl6上运行良好--版本为"This is Rakudo version 2016.02 built on MoarVM version 2016.02 implementing Perl 6.c." 您正在运行哪个版本的Perl 6? - Coke
Coke: perl6 -v: "这是Rakudo版本2016.02-151-gb243a96,构建于MoarVM版本2016.02-33-g1e3d2ac,实现了Perl 6.c。"和ncurses 5。 - sid_com
1个回答

2
在C语言中,这个等价物是正常的——你的配置有些奇怪。无论如何,我没有一个perl6设置来进行调试。
程序中唯一奇怪的事情是,在initscr之后省略了"()",为了保持一致性,我希望看到它。在C语言中,如果你这样做,后续的函数调用将会导致核心转储(因为"&initscr"是一个有效的指针)。

我在 initscr 中添加了 "()",但 getch 仍然不等待。 - sid_com
在 Perl 6 中,省略括号是有效的语法。根据文档:“参数以逗号分隔的列表形式提供。为了消除嵌套调用的歧义,可以使用括号或副词形式。” https://doc.perl6.org/language/functions#Arguments - donaldh

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接