Detecting Arrow Keys

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.

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?

On 31 Dec 2007, at 08:34, tho_mica_l wrote:

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

I see.

STDIN buffering until “\n” (all platforms)

curses could be of some help on non-windows platforms.

On Dec 31, 2007, at 3:34 AM, tho_mica_l wrote:

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?

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs