• Publicidad

¿ Cómo hacer un Term::ReadKey ?

¿Ya sabes lo que es una referencia? Has progresado, el nível básico es cosa del pasado y ahora estás listo para el siguiente nivel.

¿ Cómo hacer un Term::ReadKey ?

Notapor creating021 » 2007-12-29 19:59 @874

En Perl Cookbook sale un ejemplo de cómo usar Term::ReadKey para saber qué tecla fue usada (leer el STDIN sin tener que esperar el "\n").

Yo sé que usando POSIX y sysread se puede hacer:

Sintáxis: [ Descargar ] [ Ocultar ]
Using perl Syntax Highlighting
use POSIX;
use 5.10.0;

my $termios = new POSIX::Termios;
$termios->setcc( VTIME, 1 );
my $key;
sysread ( STDIN, $key, 1 );
say $key;
$termios->setcc( VTIME, 0 );
Coloreado en 0.004 segundos, usando GeSHi 1.0.8.4


Si a este código se le pone las funciones de noecho y echo, está hecho, pero...

¿Qué pasa si quiero leer las flechas ( up, down, right, left ) y otras teclas ( backspace, tab, F1, F2... ) ?

¿Se puede hace con POSIX o tiene que hacerse con Term::ReadKey?
Expect the worst, is it the least you can do?
Avatar de Usuario
creating021
Perlero frecuente
Perlero frecuente
 
Mensajes: 595
Registrado: 2006-02-23 16:17 @720
Ubicación: Frente al monitor

Publicidad

Notapor explorer » 2007-12-29 22:20 @972

Encontré esta página donde hay una solución: usar IO::Select para leer el estado del STDIN.

http://www.thescripts.com/forum/thread49817.html

Lo que no me creo es que no exista un módulo para eso. ¿Has mirado perlfaq8? Hay un par de respuestas interesantes.
Última edición por explorer el 2007-12-30 10:55 @496, editado 1 vez en total
JF^D Perl programming & Raku programming. Grupo en Telegram: https://t.me/Perl_ES
Avatar de Usuario
explorer
Administrador
Administrador
 
Mensajes: 14477
Registrado: 2005-07-24 18:12 @800
Ubicación: Valladolid, España

Notapor creating021 » 2007-12-30 10:45 @490

Muchas gracias por la respuesta, ahora veo el link y el FAQ
Expect the worst, is it the least you can do?
Avatar de Usuario
creating021
Perlero frecuente
Perlero frecuente
 
Mensajes: 595
Registrado: 2006-02-23 16:17 @720
Ubicación: Frente al monitor

Notapor creating021 » 2007-12-31 16:49 @742

Al final, esta es la solución:
Sintáxis: [ Descargar ] [ Ocultar ]
Using perl Syntax Highlighting
use POSIX;
use 5.10.0;
use strict;

my $t = new POSIX::Termios;
$t->setcc( VTIME, 1 );
my $att = $t->getattr( fileno(STDIN) );
my $flag = $t->getlflag();
$t->setlflag( $flag & ~&POSIX::ECHO );
$t->setattr( fileno(STDIN), TCSANOW );
my $k;
binmode STDIN;
sysread ( STDIN, $k, 5 );
say ( join " ", ( unpack "c*", $k ) );
if ( $k eq "\e[A" ) { say "up"; }
$t->setlflag(&POSIX::ECHO);
$t->setattr( fileno(STDIN), TCSANOW );
$t->setcc( VTIME, 0 );
 
Coloreado en 0.002 segundos, usando GeSHi 1.0.8.4


Leo 5 bytes y no 1 ya que teclas como F13 ( en darwin ) usa 5 bytes, las teclas up, down, left y right usan 3 bytes, así que 5 bytes el el máximo usado.
Expect the worst, is it the least you can do?
Avatar de Usuario
creating021
Perlero frecuente
Perlero frecuente
 
Mensajes: 595
Registrado: 2006-02-23 16:17 @720
Ubicación: Frente al monitor


Volver a Intermedio

¿Quién está conectado?

Usuarios navegando por este Foro: No hay usuarios registrados visitando el Foro y 12 invitados

cron