Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to get a character from keyboard?

56 views
Skip to first unread message

Luo Yong

unread,
Sep 30, 2006, 12:32:36 PM9/30/06
to
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.

David Vallner

unread,
Sep 30, 2006, 12:50:45 PM9/30/06
to

Use curses.

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

David Vallner

signature.asc

Paul Lutus

unread,
Sep 30, 2006, 1:12:47 PM9/30/06
to
Luo Yong wrote:

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

Nebiru

unread,
Sep 30, 2006, 2:47:25 PM9/30/06
to

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

unread,
Sep 30, 2006, 3:15:19 PM9/30/06
to

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

unread,
Sep 30, 2006, 3:49:02 PM9/30/06
to
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

signature.asc

Une bévue

unread,
Sep 30, 2006, 4:18:25 PM9/30/06
to
David Vallner <da...@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

unread,
Sep 30, 2006, 4:38:14 PM9/30/06
to
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


Une bévue

unread,
Sep 30, 2006, 4:58:28 PM9/30/06
to
Daniel Harple <dha...@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

unread,
Sep 30, 2006, 10:17:36 PM9/30/06
to

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

unread,
Sep 30, 2006, 10:39:48 PM9/30/06
to

It works.Thank you very much. :)

And thanks all.

Wilson Bilkovich

unread,
Sep 30, 2006, 10:54:23 PM9/30/06
to
On 9/30/06, Trans <tran...@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.
>

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.

James Edward Gray II

unread,
Sep 30, 2006, 11:45:15 PM9/30/06
to

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


David Vallner

unread,
Oct 1, 2006, 10:43:07 AM10/1/06
to
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

signature.asc

David Vallner

unread,
Oct 1, 2006, 2:51:00 PM10/1/06
to
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

signature.asc
0 new messages