Detecting Arrow Keys

Discussion in 'Ruby' started by thefed, Dec 31, 2007.

  1. thefed

    thefed Guest

    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.
     
    thefed, Dec 31, 2007
    #1
    1. Advertising

  2. thefed

    tho_mica_l Guest

    > 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?
     
    tho_mica_l, Dec 31, 2007
    #2
    1. Advertising

  3. thefed

    James Tucker Guest

    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).
     
    James Tucker, Dec 31, 2007
    #3
  4. thefed

    thefed Guest

    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?
     
    thefed, Dec 31, 2007
    #4
  5. thefed

    tho_mica_l Guest

    I see.

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


    curses could be of some help on non-windows platforms.
     
    tho_mica_l, Dec 31, 2007
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Rob R. Ainscough
    Replies:
    0
    Views:
    715
    Rob R. Ainscough
    Jul 20, 2005
  2. (Pete Cresswell)
    Replies:
    2
    Views:
    591
    (Pete Cresswell)
    Oct 12, 2004
  3. Daniel Alexandre

    Arrow-keys bug.

    Daniel Alexandre, Feb 23, 2005, in forum: Python
    Replies:
    0
    Views:
    408
    Daniel Alexandre
    Feb 23, 2005
  4. Vidar Gundersen
    Replies:
    1
    Views:
    791
  5. tac-tics

    arrow keys don't work

    tac-tics, Jan 22, 2007, in forum: Python
    Replies:
    6
    Views:
    361
    Terry Reedy
    Jan 23, 2007
Loading...

Share This Page