Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > How to read a character at a time from STDIN without needing topress return key?

Reply
Thread Tools

How to read a character at a time from STDIN without needing topress return key?

 
 
Kenneth McDonald
Guest
Posts: n/a
 
      09-03-2008
In the following bit of code:

print STDIN.getc
STDOUT.flush

The code for whatever key I type isn't actually printed out until I go
on to hit the return key. How can I have it printed as soon as I type
a character key?

Thanks,
Ken

 
Reply With Quote
 
 
 
 
Nobuyoshi Nakada
Guest
Posts: n/a
 
      09-05-2008
Hi,

At Thu, 4 Sep 2008 05:40:43 +0900,
Kenneth McDonald wrote in [ruby-talk:313786]:
> In the following bit of code:
>
> print STDIN.getc
> STDOUT.flush
>
> The code for whatever key I type isn't actually printed out until I go
> on to hit the return key. How can I have it printed as soon as I type
> a character key?


With io-console, you can do:

print STDIN.getch

http://www.rubyist.net/~nobu/ruby/io-console.tar.bz2
http://www.rubyist.net/~nobu/ruby/io-console-0.2.gem

--
Nobu Nakada

 
Reply With Quote
 
 
 
 
Bertram Scharpf
Guest
Posts: n/a
 
      09-05-2008
Hi,

Am Donnerstag, 04. Sep 2008, 05:40:43 +0900 schrieb Kenneth McDonald:
> In the following bit of code:
>
> print STDIN.getc
> STDOUT.flush
>
> The code for whatever key I type isn't actually printed out until I go on
> to hit the return key. How can I have it printed as soon as I type a
> character key?


On feasible operating systems you can install the "termios" gem.
Then say something like:

class IO
def nocanon
term = Termios::getattr self
term.c_lflag &= ~Termios::ICANON
Termios::setattr self, Termios::TCSANOW, term
yield
ensure
term.c_lflag |= Termios::ICANON
Termios::setattr self, Termios::TCSANOW, term
end
end

$stdin.nocanon do
c = $stdin.getc
end

Maybe you like to have a look at how I let the user enter
passwords: <http://bertram-scharpf.homelinux.com/src/password.rb>.

Good luck!

Bertram


--
Bertram Scharpf
Stuttgart, Deutschland/Germany
http://www.bertram-scharpf.de

 
Reply With Quote
 
James Gray
Guest
Posts: n/a
 
      09-05-2008
On Sep 3, 2008, at 3:40 PM, Kenneth McDonald wrote:

> In the following bit of code:
>
> print STDIN.getc
> STDOUT.flush
>
> The code for whatever key I type isn't actually printed out until I
> go on to hit the return key. How can I have it printed as soon as I
> type a character key?


http://blog.grayproductions.net/arti..._one_character

Hope that helps.

James Edward Gray II

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
peek at stdin, flush stdin Johnathan Doe C Programming 5 05-17-2013 04:30 PM
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
How to read a character from stdin without the program wait for aEnter key? eap.britzkrieg C Programming 4 06-08-2009 05:10 PM
Reading stdin once confuses second stdin read Charlie Zender C Programming 6 06-21-2004 01:39 PM
question: reading character for character from stdin KwikRick Python 1 08-22-2003 05:56 PM



Advertisments