RCR getch

R

Roger Pack

It would be quite convenient to have a getch method in Ruby.
Any opposition to my suggesting its addition to core?
-r
 
N

Nobuyoshi Nakada

Roger said:
It would be quite convenient to have a getch method in Ruby.
Any opposition to my suggesting its addition to core?
-r

$ /usr/local/bin/ruby-1.9.3 -v -rio/console -e 'p STDIN.gets; p
STDIN.noecho{|f|f.gets("!")}; p STDIN.getch'
ruby 1.9.3dev (2010-07-06 trunk 28554) [x86_64-linux]
hello
"hello\n"
"world!"
"\n"
 
B

botp

$ /usr/local/bin/ruby-1.9.3 -v -rio/console -e 'p STDIN.gets; p
STDIN.noecho{|f|f.gets("!")}; p STDIN.getch'
ruby 1.9.3dev (2010-07-06 trunk 28554) [x86_64-linux]
hello
"hello\n"
"world!"
"\n"

uber cool. thank for the tip, nobu.
best regards -botp
 
R

Roger Pack

$ /usr/local/bin/ruby-1.9.3 -v -rio/console -e 'p STDIN.gets; p
STDIN.noecho{|f|f.gets("!")}; p STDIN.getch'
ruby 1.9.3dev (2010-07-06 trunk 28554) [x86_64-linux]
hello
"hello\n"
"world!"
"\n"

Excellent. I'm glad this was added then.

This appears to not work on windows is that expected?
-r
 
R

Roger Pack

Oops I missed a require:
Excellent. I'm glad this was added then.

This appears to not work on windows is that expected?

It does work, and, though it doesn't get arrow keys, it's good enough
for my intents and purposes.

NB if you *do* want arrow keys you'll want to wrap _getch or _getkbhit,
like rb-readline does. See also [1]..

-r

[1] http://betterlogic.com/roger/?p=2930

For google's sake, here's what it does when you *don't* first require
io/console:
NoMethodError: undefined method `getch' for #<IO:<STDIN>>
from (irb):1
from C:/installs/ruby_trunk_installed/bin/irb.bat:20:in `<main>'
 
R

Roger Pack

NB if you *do* want arrow keys you'll want to wrap _getch or _getkbhit,
like rb-readline does. See also [1]..
What does it return when arrow keys are hit?

224 + (some number like 75 for left arrow). It appears to not track when
the shift key is hit and things like that, though. Not sure if those
are accessible...
 

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
474,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top