【问题标题】:ncurses: why doesn't getch wait until I press a key?ncurses:为什么 getch 不等到我按下一个键?
【发布时间】:2016-03-20 17:02:44
【问题描述】:

来自 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();

【问题讨论】:

  • 这对我来说似乎适用于 ncurses 6 和 perl6 --version 的“这是基于 MoarVM 版本 2016.02 实现 Perl 6.c 的 Rakudo 版本 2016.02”。你运行的是哪个版本的 Perl 6?
  • Coke: perl6 -v: "这是 Rakudo 版本 2016.02-151-gb243a96,基于 MoarVM 版本 2016.02-33-g1e3d2ac 实现 Perl 6.c。"和 ncurses 5.

标签: blocking ncurses raku getch nativecall


【解决方案1】:

C 中的等效项有效 - 您的配置有些奇怪。在任何情况下,我都没有 perl6 设置来调试它。

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

【讨论】:

  • 我将"()" 添加到initscrgetch 仍然没有等待。
  • 省略括号是有效的 Perl 6 语法。来自文档:“参数以逗号分隔列表的形式提供。可以使用括号或状语形式来消除嵌套调用的歧义。” doc.perl6.org/language/functions#Arguments
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2010-10-03
  • 1970-01-01
  • 1970-01-01
  • 2019-02-12
  • 2010-10-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多