Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re: How to code Pascal's keypressed function in Perl

by valdez (Monsignor)
on Aug 03, 2002 at 17:04 UTC ( [id://187355]=note: print w/replies, xml ) Need Help??


in reply to How to code Pascal's keypressed function in Perl

Try with Term::ReadKey, the man page says:

Term::ReadKey is a compiled perl module dedicated to providing simple control over terminal driver modes (cbreak, raw, cooked, etc.,) support for non-blocking reads, if the architecture allows, and some generalized handy functions for working with terminals. One of the main goals is to have the functions as portable as possible, so you can just plug in "use Term::ReadKey" on any architecture and have a good likelyhood of it working.

Here is a snippet of code that does what you need:

use Term::ReadKey; ReadMode 4; # Turn off controls keys while (not defined ($key = ReadKey(-1)) { # No key yet } print "Get key $key\n"; ReadMode 0; # Reset tty mode before exiting

Hope this helps. Ciao, Valerio

Replies are listed 'Best First'.
Re^2: How to code Pascal's keypressed function in Perl
by Anonymous Monk on May 19, 2009 at 15:36 UTC
    If you have a while loop that is spinning and you don't want to prompt the user for any key to be pressed such as with <STDIN> and you don't want to use Term:ReadKey module but to silently break out when she hits ENTER key use the following 3 line function called "keystroke()" where $j variable will change from 0 to 1 when the ENTER key is pressed:

    while (true) {
       ...
       if (&keystroke == 1) {
         last;
       }
       ...
    }

    sub keystroke {
       $i = '';
       vec($i, fileno(STDIN), 1) = 1;
       $j = select ($i, undef, undef, 0);
    }

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://187355]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (4)
As of 2024-03-28 17:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found