How to get a character from keyboard?

L

Luo Yong

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

David Vallner

--------------enigDF50C2898F616D30E2C7546A
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable

Luo said:
Hi all,
=20
I found that the STDIN.getc seem using a buffered input.It can't
return anything until you enter a "\n".
=20
Is there any way to get a character directly from keyboard?
=20
Thanks.
=20

Use curses.

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

David Vallner


--------------enigDF50C2898F616D30E2C7546A
Content-Type: application/pgp-signature; name="signature.asc"
Content-Description: OpenPGP digital signature
Content-Disposition: attachment; filename="signature.asc"

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.5 (MingW32)

iD8DBQFFHqBly6MhrS8astoRAkJVAJ4yhSFqtBPMaYnGjWGGLsWF3x8/EQCfYNvr
UcihQleIOk0YWpUoi8SqSd4=
=kyhZ
-----END PGP SIGNATURE-----

--------------enigDF50C2898F616D30E2C7546A--
 
N

Nebiru

Luo said:
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
 
T

Trans

Paul said:
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.
 
D

David Vallner

--------------enig60D9FC5C94D48E6097DF6A10
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
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.
=20

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


--------------enig60D9FC5C94D48E6097DF6A10
Content-Type: application/pgp-signature; name="signature.asc"
Content-Description: OpenPGP digital signature
Content-Disposition: attachment; filename="signature.asc"

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.5 (MingW32)

iD8DBQFFHsovy6MhrS8astoRAqS5AJ41HGBLLfEDxby6IrpdWV9TJICTuACfc1P8
3XnKr6zO2CtYJDUAMnAVWZk=
=sEk5
-----END PGP SIGNATURE-----

--------------enig60D9FC5C94D48E6097DF6A10--
 
U

Une bévue

David Vallner said:
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 ???
 
D

Daniel Harple

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 =3D 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 =3D 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
 
U

Une bévue

Daniel Harple said:
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 !
 
T

Trans

David said:
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.
 
L

Luo Yong

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

Wilson Bilkovich

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

James Edward Gray II

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
 
D

David Vallner

--------------enig3FBFC97BA266A3C67BEC1885
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable

Robert said:
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


--------------enig3FBFC97BA266A3C67BEC1885
Content-Type: application/pgp-signature; name="signature.asc"
Content-Description: OpenPGP digital signature
Content-Disposition: attachment; filename="signature.asc"

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.5 (MingW32)

iD8DBQFFH9P7y6MhrS8astoRAq1nAJ9H6srC4fT7UOTWkbHciCuMMYNEygCeKC4w
/FPO+oQ3JBrlEQvUnwqJhu4=
=MCJz
-----END PGP SIGNATURE-----

--------------enig3FBFC97BA266A3C67BEC1885--
 
D

David Vallner

--------------enigA891920C1AAE29C5B4CF3A76
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable

Robert said:

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

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

http://rubyforge.org/cgi-bin/viewvc.cgi/trunk/highline/lib/highline/syste=
m_extensions.rb?revision=3D155&root=3Dhighline&pathrev=3D153http://rubyfo=
rge.org/cgi-bin/viewvc.cgi/trunk/highline/lib/highline/system_extensions.=
rb?revision=3D155&root=3Dhighline&pathrev=3D153

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

http://rubyforge.org/cgi-bin/viewvc.cgi/trunk/highline/lib/highline/syste=
m_extensions.rb?revision=3D155&root=3Dhighline&pathrev=3D153
http://rubyforge.org/cgi-bin/viewvc.cgi/trunk/highline/lib/highline/syste=
m_extensions.rb?revision=3D155&root=3Dhighline&pathrev=3D153

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]
=20
???

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


--------------enigA891920C1AAE29C5B4CF3A76
Content-Type: application/pgp-signature; name="signature.asc"
Content-Description: OpenPGP digital signature
Content-Disposition: attachment; filename="signature.asc"

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.5 (MingW32)

iD8DBQFFIA4Ty6MhrS8astoRAl8uAJ9AVA4qE0rDC1WXh4ti6fdUH0QJ3gCfVy9s
gHgrdzoBuM3k1B7N/aaEUZQ=
=ecxQ
-----END PGP SIGNATURE-----

--------------enigA891920C1AAE29C5B4CF3A76--
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,239
Members
46,827
Latest member
DMUK_Beginner

Latest Threads

Top