On Sep 30, 2006, at 9:39 PM, Luo Yong wrote:

> On 10/1/06, Nebiru <Nebiru / gmail.com> wrote:
>>
>> Luo Yong wrote:
>> > Hi all,
>> >
>> > I found that the STDIN.getc seem using a buffered input.It can't
>> > return anything until you enter a "\n".
>> >
>> > Is there any way to get a character directly from keyboard?
>> >
>> > Thanks.
>>
>> Not sure if this is quite what your looking for but I generally do
>> something like
>> def get_keypress
>>     system "stty raw -echo"
>>     STDIN.getc
>> ensure
>>     system "stty -raw echo"
>> end
>>
>> key = get_keypress.chr
>>
>>
>>
>
> It works.Thank you very much.  :)

I strongly recommend using HighLine for this.  It tries to find the  
best solution based on your platform and available libraries.  I've  
separated the character reading code so you don't need to load all of  
HighLine to use it:

#!/usr/bin/env ruby -w

require "highline/system_extensions"
include HighLine::SystemExtensions

print "Enter one character:  "
char = get_character
puts char.chr

__END__

If you don't want the external dependancy, at least consider stealing  
HighLine's code for this:

http://rubyurl.com/T29

People have helped me improve it a lot over the last year.

James Edward Gray II