从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();