Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re: How do I read just one key at a time?

by Crulx (Monk)
on Feb 29, 2000 at 14:24 UTC ( #4558=note: print w/replies, xml ) Need Help??


in reply to How do I read just one key at a time?

Yeah, I probably would use a CPAN module that already has the nonblocking IO and select polling set up for you. But it would be wise anyway to learn how to do nonblocking IO and using select to do your polling. These are very important things in unix and perl has basically the same syntax for them as C. Remember not to confuse the different types of "select"s in perl. W. Steven's Advanced Programming in the Unix Environment is an essential book for anyone programming something on Unix.
---
Crulx
crulx@iaxs.net
  • Comment on Re: How do I read just one key at a time?

Replies are listed 'Best First'.
RE: Re: How do I read just one key at a time?
by plaid (Chaplain) on Mar 01, 2000 at 03:05 UTC
    Getting input one character at a time doesn't involve using select and non-blocking I/O (at least not the easiest way). The way that the input is processed is set by the user's terminal. The default setting is known as 'canonical mode', which processes line-by-line. I also must correct my original statement, for there is indeed a way with the standard perl modules to do what you want. For example:
    use POSIX qw(:termios_h); my $termios = new POSIX::Termios; $termios->setlflag(~ICANON); # Turn off canonical mode $termios->setcc(VMIN, 1); # Read a min of 1 character $termios->setcc(VTIME, 0); # No time-out on reads $termios->setattr(0, TCSANOW); # Apply settings to STDIN while(read(STDIN, $key, 1)) { print "Got: $key\r\n"; }
    or something similar will work. The flags and function calls used by the POSIX::Termios module are nearly identical to the corresponding ones in C.
      My PERL distribution doesn't have Curses or Posix::Term installed. I tried your last example but how do you get the terminal settings back to normal afterwards? I couldn't find documentation on this.
        correction to the last reply, instead of Posix::Term I meant Term::ReadKey

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (4)
As of 2022-10-02 06:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My preferred way to holiday/vacation is:











    Results (7 votes). Check out past polls.

    Notices?