[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Detecting Arrow Keys

fedzor

12/31/2007 1:06:00 AM

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

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.



4 Answers

ThoML

12/31/2007 8:31:00 AM

0

> 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?

James Tucker

12/31/2007 4:42:00 PM

0


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

fedzor

12/31/2007 5:59:00 PM

0


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

>> 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?

ThoML

12/31/2007 6:03:00 PM

0

I see.

> STDIN buffering until "\n" (all platforms)

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