我正在使用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);
当我运行此脚本并按下向上箭头,向下箭头,向右箭头,向左箭头和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
解决方法
替换我的$c = $* IN.getc;我的$c = $* IN.read(1);并更改代码的其余部分以处理缓冲区而不是字符串.