Detecting Arrow Keys

T

thefed

Hey all,

In short, I'm looking to, through an STDIN/OUT console interface,
detect which arrows keys have been pressed. Here is everything I have
so far...

PRGM A = http://pastie.caboo.se/133393

using PRGM A, I have been able to detect all key combinations
necessary for all of my motives - except, of course, for this one.
Let's go through a basic run-through:
$ ruby single_character.rb
Enter one character: 27 # UP-arrow
$ ruby single_character.rb
Enter one character: 27 # DOWN-arrow
$ ruby single_character.rb
Enter one character: 27 # LEFT-arrow
$ ruby single_character.rb
Enter one character: 27 # RIGHT-arrow

Hmm... Something's not right here... Why are they all the same?

So that is as far as I have been able to get. OH! My goal:
Write a pure ruby Readline alternative.
--> (Actually, just emulate history)

Yes, I understand there is a Readline for windows, but really,
detecting this is harder for the programmer, and thus less work for
the user. Probably me.

Is there some hidden method that I am not realizing?


Thanks,
Ari
--------------------------------------------|
If you're not living on the edge,
then you're just wasting space.
 
T

tho_mica_l

Hmm... Something's not right here... Why are they all the same?

Cursor keys on the terminal are most often sent as a sequence of three
characters:

27, '[', 'A' ... up
27, '[', 'B' ... down
27, '[', 'C' ... right
27, '[', 'D' ... left

27 is escape.

I don't know why you'd want to check for cursor keys but if it's for
line editing you're probably better of with using a library that
already does this. I'm not 100% sure but Highline (http://
highline.rubyforge.org/) might be of help here.

Isn't readline already distributed with ruby by default? Why write a
ruby version of a standard ruby library?
 
J

James Tucker

Isn't readline already distributed with ruby by default? Why write a
ruby version of a standard ruby library?

Well, for one, it's only the binding that's in the stdlib. The one-
click-installer ships with a copy, which I think came from GnuWin32.
The implementations of readline available on windows are broken, though.

Simple replication:

In a command prompt <<EOEXAMPLE
mode con: cols=81
irb
["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n",
"o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"]
EOEXAMPLE

This will not display correctly, starting from around about character
70 in width, IIRC

It can take two or three newlines to clear the display corruption, and
when trying to go up past the wide line in the history will also cause
the same issues, making irb quite unusable.

The more core issue, is that there isn't a way to do a "getc" that
actually doesn't violate POLS. I don't know about others here, but
personally, I would (if I didn't know better) expect "get character"
to just get a single character.

N.B. We're not talking about the argv get[cs] POLS violation, there
are two other problems here, STDIN buffering until "\n" (all
platforms) and readline being broken (Windows).
 
T

thefed

Hmm... Something's not right here... Why are they all the same?

Cursor keys on the terminal are most often sent as a sequence of three
characters:

27, '[', 'A' ... up
27, '[', 'B' ... down
27, '[', 'C' ... right
27, '[', 'D' ... left

27 is escape.

I don't know why you'd want to check for cursor keys but if it's for
line editing you're probably better of with using a library that
already does this. I'm not 100% sure but Highline (http://
highline.rubyforge.org/) might be of help here.

hehe I use highline regularly, and so far, I do not know of any way
to emulate history with it, or detect arrow keys.

But, in short, does this mean that there is no way to detect the
arrow keys?
 

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,763
Messages
2,569,562
Members
45,038
Latest member
OrderProperKetocapsules

Latest Threads

Top