[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Single character input without pressing 'Enter'

unni.tallman

9/20/2006 3:43:00 AM

how can i read input from stdin, char by char, i want to get the
character as soon as it is entered, without having to press 'Enter'
after each character.

4 Answers

Nguyen Huu Bach

9/20/2006 3:55:00 AM

0

This is a simple way to achieve that. Maybe there are others

def read_char
system "stty raw -echo"
STDIN.getc
ensure
system "stty -raw echo"
end

good luck



unni.tallman wrote:
> how can i read input from stdin, char by char, i want to get the
> character as soon as it is entered, without having to press 'Enter'
> after each character.
>
>
>
>

e

9/20/2006 4:34:00 AM

0

Nguyen Huu Bach wrote:
> This is a simple way to achieve that. Maybe there are others
>
> def read_char
# Save previous state
old = `stty -g`
> system "stty raw -echo"
> STDIN.getc
> ensure
system "stty #{old}"
> end
>
> good luck

Might be better to use Termios or Curses, though.

--
Posted via http://www.ruby-....

James Gray

9/20/2006 12:56:00 PM

0

On Sep 19, 2006, at 11:33 PM, Eero Saynatkari wrote:

> Nguyen Huu Bach wrote:
>> This is a simple way to achieve that. Maybe there are others
>>
>> def read_char
> # Save previous state
> old = `stty -g`
>> system "stty raw -echo"
>> STDIN.getc
>> ensure
> system "stty #{old}"
>> end
>>
>> good luck
>
> Might be better to use Termios or Curses, though.

Or use HighLine which handles all these details for you.

James Edward Gray II

Jeff Schwab

9/20/2006 1:57:00 PM

0

> unni.tallman wrote:
>> how can i read input from stdin, char by char, i want to get the
>> character as soon as it is entered, without having to press 'Enter'
>> after each character.

Nguyen Huu Bach wrote:

> def read_char
> system "stty raw -echo" STDIN.getc ensure system "stty -raw echo"
> end

Won't work on Windows.

The sooner everybody on Earth to switches to *nix, the sooner we can
stop worrying about these silly little differences. :)