sid_com
sid_com

Reputation: 25117

Is there something similar to "keypad" without using "Curses"?

Is there something similar to keypad(1) outside from Curses? I would like to write something like this, but without using Curses and without handling the escape sequences myself.

#!/usr/bin/env perl
use warnings;
use 5.012;
use Curses;

initscr();
raw();
printw( qq{Press "Delete"} );
noecho();
keypad(1); 
my $c = getch();
endwin();

if ( $c =~ /\A330\z/ ) {
    say "OK";
} else {
    say qq{You didn't press "Delete"};
}

When I use Term::ReadKey it behaves different:

#!/usr/bin/env perl
use warnings;
use 5.012;
use Term::ReadKey;

ReadMode('raw');

print qq{Press "Delete" };
while ( 1 ) {
    my $c = ReadKey( 0 );
    last if $c eq 'q';
    say "<$c>";
}

ReadMode('normal');

Output after pressing "Delete":

Press "Delete" <
<[>
<3>
<~>

Upvotes: 0

Views: 162

Answers (2)

LeoNerd
LeoNerd

Reputation: 8532

As already mentioned by Borodin, Term::TermKey may help:

use warnings;
use 5.012;
use Term::TermKey;

my $tk = Term::TermKey->new( \*STDIN );

print qq{Press "Delete" };
while( 1 ) {
   $tk->waitkey( my $key );
   say "<", $tk->format_key( $key, 0 ), ">";
}

Gives

Press "Delete" <Delete>

Upvotes: 2

Borodin
Borodin

Reputation: 126742

What is it about Curses that you want to avoid?

You may find Term::TermKey useful. It is a Perl interface to the libtermkey library, which handles keyboard control characters and multi-byte escape sequences and UTF-8 characters.

Upvotes: 3

Related Questions