阻止 – ncurses:为什么不等到我按下一个键?

从ncurses(3)
linux手册页:

The nodelay option causes getch to be a non-blocking call. If no input is ready, getch returns ERR. If disabled (bf is FALSE), getch waits until a key is pressed.

为什么在我的例子中没有等到我按下一个键?

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

最佳答案 C中的等价物 – 你的配置有点奇怪.在任何情况下,我都没有perl6设置来调试它.

我在程序中看到的唯一奇怪的事情是你在initscr之后省略了“()”,我希望看到它的一致性.在C中,如果你这样做,后续调用将转储核心(因为& initscr是一个有效的指针).

点赞