Click here to get back home

FAQ 8.5 How do I read just one key without waiting for a return key?

 HomeNewsGroups | Search | About
 comp.lang.perl.misc    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
FAQ 8.5 How do I read just one key without waiting for a return key? PerlFAQ Server 03-31-2008
Posted by PerlFAQ Server on March 31, 2008, 3:03 am
Please log in for more thread options
This is an excerpt from the latest version perlfaq8.pod, which
comes with the standard Perl distribution. These postings aim to
reduce the number of repeated questions as well as allow the community
to review and update the answers. The latest version of the complete
perlfaq is at http://faq.perl.org .

--------------------------------------------------------------------

8.5: How do I read just one key without waiting for a return key?

Controlling input buffering is a remarkably system-dependent matter. On
many systems, you can just use the stty command as shown in "getc" in
perlfunc, but as you see, that's already getting you into portability
snags.

open(TTY, "+</dev/tty") or die "no tty: $!";
system "stty cbreak </dev/tty >/dev/tty 2>&1";
$key = getc(TTY); # perhaps this works
# OR ELSE
sysread(TTY, $key, 1); # probably this does
system "stty -cbreak </dev/tty >/dev/tty 2>&1";

The Term::ReadKey module from CPAN offers an easy-to-use interface that
should be more efficient than shelling out to stty for each key. It even
includes limited support for Windows.

use Term::ReadKey;
ReadMode('cbreak');
$key = ReadKey(0);
ReadMode('normal');

However, using the code requires that you have a working C compiler and
can use it to build and install a CPAN module. Here's a solution using
the standard POSIX module, which is already on your systems (assuming
your system supports POSIX).

use HotKey;
$key = readkey();

And here's the HotKey module, which hides the somewhat mystifying calls
to manipulate the POSIX termios structures.

# HotKey.pm
package HotKey;

@ISA = qw(Exporter);
@EXPORT = qw(cbreak cooked readkey);

use strict;
use POSIX qw(:termios_h);
my ($term, $oterm, $echo, $noecho, $fd_stdin);

$fd_stdin = fileno(STDIN);
$term = POSIX::Termios->new();
$term->getattr($fd_stdin);
$oterm = $term->getlflag();

$echo = ECHO | ECHOK | ICANON;
$noecho = $oterm & ~$echo;

sub cbreak {
$term->setlflag($noecho); # ok, so i don't want echo either
$term->setcc(VTIME, 1);
$term->setattr($fd_stdin, TCSANOW);
}

sub cooked {
$term->setlflag($oterm);
$term->setcc(VTIME, 0);
$term->setattr($fd_stdin, TCSANOW);
}

sub readkey {
my $key = '';
cbreak();
sysread(STDIN, $key, 1);
cooked();
return $key;
}

END { cooked() }

1;



--------------------------------------------------------------------

The perlfaq-workers, a group of volunteers, maintain the perlfaq. They
are not necessarily experts in every domain where Perl might show up,
so please include as much information as possible and relevant in any
corrections. The perlfaq-workers also don't have access to every
operating system or platform, so please include relevant details for
corrections to examples that do not work on particular platforms.
Working code is greatly appreciated.

If you'd like to help maintain the perlfaq, see the details in
perlfaq.pod.

Similar ThreadsPosted
FAQ 8.5: How do I read just one key without waiting for a return key? December 10, 2004, 6:03 pm
FAQ 8.5 How do I read just one key without waiting for a return key? March 11, 2005, 6:03 am
FAQ 8.5 How do I read just one key without waiting for a return key? May 26, 2005, 11:03 am
FAQ 8.5 How do I read just one key without waiting for a return key? October 15, 2005, 4:03 pm
FAQ 8.5 How do I read just one key without waiting for a return key? January 12, 2006, 5:03 am
FAQ 8.5 How do I read just one key without waiting for a return key? September 7, 2006, 9:03 pm
FAQ 8.5 How do I read just one key without waiting for a return key? March 17, 2007, 3:03 pm
FAQ 8.5 How do I read just one key without waiting for a return key? June 1, 2007, 3:03 pm
FAQ 8.5 How do I read just one key without waiting for a return key? October 28, 2007, 3:03 pm
FAQ 8.5 How do I read just one key without waiting for a return key? January 13, 2008, 9:03 am

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap