从 ncurses(3) linux 手册页:
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();