7

I am using the Perl 6 module 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);

When I run this script and press the keys up-arrow, down-arrow, right-arrow, left-arrow and q this is the output:

#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

But I would have expected:

#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

How do I have to modify the script to get the desired output?

Elizabeth Mattijsen
  • 25,654
  • 3
  • 75
  • 105
sid_com
  • 24,137
  • 26
  • 96
  • 187
  • Instead of `$*IN.getc`, have you tried using `$*IN.read(1)` (and modifying your code to take a Buf instead of a String)? – bb94 Oct 16 '15 at 02:11
  • @bb94: With `read()` it worked. Could you change your comment to an answer? – sid_com Oct 16 '15 at 07:42

1 Answers1

3

Replace my $c = $*IN.getc; with my $c = $*IN.read(1); and change the rest of your code to handle a buffer instead of a string.

bb94
  • 1,294
  • 1
  • 11
  • 24