【问题标题】:Perl6: getc in raw modePerl6:原始模式下的 getc
【发布时间】:2015-11-04 16:32:32
【问题描述】:

我正在使用 Perl 6 模块 Term::termios

#!/usr/bin/env perl6
use v6;
use Term::termios;

my $saved_termios := Term::termios.new(fd => 1).getattr;
my $termios := Term::termios.new(fd => 1).getattr;
$termios.makeraw;
$termios.setattr(:DRAIN);

loop {
   my $c = $*IN.getc;
   print "got: " ~ $c.ord ~ "\r\n";
   last if $c eq 'q';
}

$saved_termios.setattr(:DRAIN);

当我运行这个脚本并按下键 up-arrowdown-arrowright-arrowleft-箭头q 这是输出:

#after arrow-up:
got: 27
got: 91

#after arrow-down:
got: 65
got: 27
got: 91

#after arrow-right:
got: 66
got: 27
got: 91

#after arrow-left:
got: 67
got: 27
got: 91

#after q:
got: 68

#after another q:
got: 113

但我早就料到了:

#after arrow-up:
got: 27
got: 91
got: 65

#after arrow-down:
got: 27
got: 91
got: 66

#after arrow-right:
got: 27
got: 91
got: 67

#after arrow-left:
got: 27
got: 91
got: 68

#after q:
got: 113

如何修改脚本以获得所需的输出?

【问题讨论】:

  • 您是否尝试过使用$*IN.read(1) 而不是$*IN.getc(并修改您的代码以采用Buf 而不是String)?
  • @bb94:使用read(),它起作用了。你能把你的评论改成答案吗?

标签: perl terminal raku getc


【解决方案1】:

my $c = $*IN.getc; 替换为 my $c = $*IN.read(1); 并更改其余代码以处理缓冲区而不是字符串。

【讨论】:

    猜你喜欢
    • 2015-03-01
    • 1970-01-01
    • 2013-04-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-27
    • 1970-01-01
    • 2013-09-06
    相关资源
    最近更新 更多