How to get a character from keyboard?

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.

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

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

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

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.

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

···

David Vallner <david@vallner.net> wrote:

Use curses.

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

--
une bévue

It works.Thank you very much. :slight_smile:

And thanks all.

···

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

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

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

···

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

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

···

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

that's OK, fine thanks !

···

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

--
une bévue

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.

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

http://rubyurl.com/T29

The link seems to be broken, I get a *Python* stacktrace.
Anyway, the HighLine page itself is perfectly accessible
http://rubyforge.org/frs/?group_id=683&release_id=5680

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

···

On 10/1/06, James Edward Gray II <james@grayproductions.net> wrote:

James Edward Gray II

Robert

--
Deux choses sont infinies : l'univers et la bêtise humaine ; en ce qui
concerne l'univers, je n'en ai pas acquis la certitude absolue.

- Albert Einstein

This month's ACM Queue magazine has a writeup of the trials and
travails of moving from 32bit to 64bit architectures (along with the
history of earlier transitions, such as 16 to 32), and it made me want
to cry.
We seem to be an industry optimized for burning 55-gallon drums full of money.

···

On 9/30/06, Trans <transfire@gmail.com> wrote:

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.

Robert Dober wrote:

The link seems to be broken, I get a *Python* stacktrace.

Yeees. And? James apparently pressed the keycombo for "paste" twice when
creating the small URL.

That's ViewVC - I can't recall one project hosting site off the top of
my head that doesn't use either that or ViewSVN for web-based source
code repository browsing. Sourceforge is mainly written in PHP and still
uses that for the CVS web interface.

David Vallner

Robert Dober wrote:
> The link seems to be broken, I get a *Python* stacktrace.

Yeees. And?

?

James apparently pressed the keycombo for "paste" twice when

creating the small URL.

??

That's ViewVC - I can't recall one project hosting site off the top of

my head that doesn't use either that or ViewSVN for web-based source
code repository browsing. Sourceforge is mainly written in PHP and still
uses that for the CVS web interface.

???
Should one ignore posts that seem completely confused, yes, unless they
come from a regular contributor as you are.
It all seems Chinese though :frowning:

David Vallner

···

On 10/1/06, David Vallner <david@vallner.net> wrote:

--
Deux choses sont infinies : l'univers et la bêtise humaine ; en ce qui
concerne l'univers, je n'en ai pas acquis la certitude absolue.

- Albert Einstein

Robert Dober wrote:

Yeees. And?

?

Why is the fact the stacktrace is from Python worth of emphasis?

James apparently pressed the keycombo for "paste" twice when
creating the small URL.

??

http://rubyurl.com/T29 points to:

http://rubyforge.org/cgi-bin/viewvc.cgi/trunk/highline/lib/highline/system_extensions.rb?revision=155&root=highline&pathrev=153http://rubyforge.org/cgi-bin/viewvc.cgi/trunk/highline/lib/highline/system_extensions.rb?revision=155&root=highline&pathrev=153

Which, if I put a newline before the second occurence of "http://", becomes:

http://rubyforge.org/cgi-bin/viewvc.cgi/trunk/highline/lib/highline/system_extensions.rb?revision=155&root=highline&pathrev=153
http://rubyforge.org/cgi-bin/viewvc.cgi/trunk/highline/lib/highline/system_extensions.rb?revision=155&root=highline&pathrev=153

Striking family resemblance between the links. To further support the
conspiracy theory, using one of the lines (pick which you like better),
points you to system_extensions.rb, a module encapsulating amongst other
thing platform-dependent character reading functions.

RubyURL doesn't tell you the original URL after conversion like TinyURL
does, which makes it easier to oversee a double-paste.

That's ViewVC [snip]

???

I was justifying (of sorts) what code in Python would be doing on
rubyforge.org - mildly baffled why someone would find the fact worthy of
stopping over and emphasising the find.

David Vallner

Ah, I did not realize, that you did not like my post, so please consider
your reponse ignored.
It is perfectly normal that sometimes someone does not like the way I am
putting things, and when that happens and when it is said bets strategy is
to shut up, which I will do now....

···

On 10/1/06, David Vallner <david@vallner.net> wrote:

Robert Dober wrote:

--
Deux choses sont infinies : l'univers et la bêtise humaine ; en ce qui
concerne l'univers, je n'en ai pas acquis la certitude absolue.

- Albert Einstein