[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

How to get a character from keyboard?

Luo Yong

9/30/2006 4:33:00 PM

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.

14 Answers

David Vallner

9/30/2006 4:51:00 PM

0

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.
>

Use curses.

Curses.getch is the method you're looking for.

David Vallner

Paul Lutus

9/30/2006 5:13:00 PM

0

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?

As it turns out, this is a question that I have seen asked very often in
Usenet groups over the years, for all computer languages. Here is the
answer:

Without exploiting external, OS-specific packages like "curses" or the
features of particular compilers on specific platforms, you cannot do this.
If you don't care whether your application remains portable between
platforms and you are willing to invoke OS-specific external features, then
there is always some way to do it.

To put it another way, there is no Ruby way to do this.

--
Paul Lutus
http://www.ara...

Nebiru

9/30/2006 6:47:00 PM

0


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

Trans

9/30/2006 7:15:00 PM

0


Paul Lutus 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?
>
> As it turns out, this is a question that I have seen asked very often in
> Usenet groups over the years, for all computer languages. Here is the
> answer:
>
> Without exploiting external, OS-specific packages like "curses" or the
> features of particular compilers on specific platforms, you cannot do this.
> If you don't care whether your application remains portable between
> platforms and you are willing to invoke OS-specific external features, then
> there is always some way to do it.
>
> To put it another way, there is no Ruby way to do this.

Let's be honest. That's pretty sad. 30 years into the PC revolution and
it's now harder to poll a keyboard? Something is terribly wrong.

T.

David Vallner

9/30/2006 7:49:00 PM

0

Trans wrote:
> Let's be honest. That's pretty sad. 30 years into the PC revolution and
> it's now harder to poll a keyboard? Something is terribly wrong.
>

I find this to be the same in any area that has problems because of
initial fragmented development or design flaws. You end up getting
sanity layers (like curses) first, and then, when the mess settles,
noone cares enough to do things in a more straightforward way if the
status quo Just Works. Cf. terminal emulators, and my earnest suspicion
is that the whole reason why the convoluted morass that are web services
exists is that putting a computer behind a firewall into the global URL
space is less annoying than having to mess with NAT for everything you
want to listen on a socket for.

David Vallner

pere.noel

9/30/2006 8:18:00 PM

0

David Vallner <david@vallner.net> wrote:

> Use curses.
>
> Curses.getch is the method you're looking for.

i do have about the same kind of prob.

say, i've a ruby script having two to four arguments, after having
parsed them if i do a :

r = gets.chomp

(i'm looking for a full YES)

i get an error :

/Users/yvon/bin/chgext.rb:23:in `gets': No such file or directory - -a
(Errno::ENOENT)

line 23 being :
r = gets.chomp

the "-a" being the first arg of the script called like that :

~%> chgext.rb -a html shtml

could curses solve my prob here also ???
--
une bévue

Daniel Harple

9/30/2006 8:38:00 PM

0

On Sep 30, 2006, at 4:20 PM, Une bévue wrote:

> i do have about the same kind of prob.
>
> say, i've a ruby script having two to four arguments, after having
> parsed them if i do a :
>
> r = gets.chomp
>
> (i'm looking for a full YES)
>
> i get an error :
>
> /Users/yvon/bin/chgext.rb:23:in `gets': No such file or directory - -a
> (Errno::ENOENT)
>
> line 23 being :
> r = gets.chomp
>
> the "-a" being the first arg of the script called like that :
>
> ~%> chgext.rb -a html shtml
>
> could curses solve my prob here also ???

Try using STDIN.gets.

$ ruby -e 'gets()' a b c
-e:1:in `gets': No such file or directory - a (Errno::ENOENT)
from -e:1
$ ruby -e 'STDIN.gets' a b c
...
$

See:
$ ri Kernel#gets
$ ri IO#gets

-- Daniel


pere.noel

9/30/2006 8:58:00 PM

0

Daniel Harple <dharple@generalconsumption.org> wrote:

> Try using STDIN.gets.
>
> $ ruby -e 'gets()' a b c
> -e:1:in `gets': No such file or directory - a (Errno::ENOENT)
> from -e:1
> $ ruby -e 'STDIN.gets' a b c

that's OK, fine thanks !
--
une bévue

Trans

10/1/2006 2:18:00 AM

0


David Vallner wrote:
> Trans wrote:
> > Let's be honest. That's pretty sad. 30 years into the PC revolution and
> > it's now harder to poll a keyboard? Something is terribly wrong.
> >
>
> I find this to be the same in any area that has problems because of
> initial fragmented development or design flaws. You end up getting
> sanity layers (like curses) first, and then, when the mess settles,
> noone cares enough to do things in a more straightforward way if the
> status quo Just Works. Cf. terminal emulators, and my earnest suspicion
> is that the whole reason why the convoluted morass that are web services
> exists is that putting a computer behind a firewall into the global URL
> space is less annoying than having to mess with NAT for everything you
> want to listen on a socket for.

Ah yes, the always handy, Path Less Annoying. Unfotuantely it only gets
harder to do as we continue to take the low roads. The techonology
stack is getting freightfully thick.

T.

Luo Yong

10/1/2006 2:40:00 AM

0

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. :)

And thanks all.