Thread: How to identify arrow-chars?

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    40

    How to identify arrow-chars?

    Hi!

    I'm trying to write my own shell, and I've got it working pretty well now. But I want to add options like tab-completion, and input-buffer (i.e. so you can use the arrow characters to reuse earlier entered commands).

    I found an example on how to use termios to detect a character being pressed without having to wait for the return-character, but I'm having problems with identifying the arrow-characters.

    I changed my exampleprogram so that it writes out the ascii-number for the character pressed, but it identifies the arrow-characters as 65,66,67 and 68. But values are the same as A,B,C and D.

    So does anyone know how this works? Or is it like some national characters that it is a combination of two characters/bytes that make up the arrow characters?

    Best regards,

    David

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Zarniwoop View Post
    So does anyone know how this works? Or is it like some national characters that it is a combination of two characters/bytes that make up the arrow characters?
    They probably are multi-byte sequences. IMO, it is not really worth the headache doing this kind of thing without using an existing library*. Bash and probably almost all other shells use readline() for tab completion, etc, so google that. I've used it for CLI histories (not in C, but "readline" is the same idea everywhere).

    *You may think it is the other way around at first (ie, "why do I have to learn the readline API? I just want to detect the arrow keys...") but once you do, that will be easy, as opposed to if you actually find a way to do this "manually", it will still be awkward and painful to work with.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    40
    Hi!

    Thanks for the quick answer. It turns out that it is multi-byte sequences, first 91, then 65-68.

    I'm not sure how I will do now, but it might be better to use the function you suggest. It turns out that 91 is also "[", so it could be complicated to see when it is e.g. up-arrow (91 65) or [A (91 65).

    Regards,

    David

  4. #4
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Include <curses.h> and use the KEY_UP / KEY_DOWN etc. symbolic constants defined in it and compare them to characters read from keyboard.

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Zarniwoop View Post
    I'm not sure how I will do now, but it might be better to use the function you suggest. It turns out that 91 is also "[", so it could be complicated to see when it is e.g. up-arrow (91 65) or [A (91 65).
    That's just the ASCII int value of [ (qv.).

    From my memory of trying to do this "as low level as possible" some of them are just two byte sequences, but others are actually three and there is a gimmicky relationship, so you might really have a "91 91 65". You *can* pull this off, but I was so happy when I discovered readline() I decided to forget my clumsy old method (and I guess I have). I would also suspect using the escape sequences is going to be very "non-portable", like they may even change if you plug in a different keyboard (but I'm not sure about that).
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  6. #6
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    And there is FAQ on this topic, have a look.

    -ssharish
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Ascii code for arrow keys
    By beginner in forum C Programming
    Replies: 1
    Last Post: 11-07-2002, 01:29 PM
  2. Arrow keys!
    By Paninaro in forum C Programming
    Replies: 8
    Last Post: 06-26-2002, 07:39 PM
  3. msdos arrow keys?
    By seditee in forum Game Programming
    Replies: 3
    Last Post: 05-07-2002, 11:29 PM
  4. fancy strcpy
    By heat511 in forum C++ Programming
    Replies: 34
    Last Post: 05-01-2002, 04:29 PM
  5. Arrow keys
    By Nutshell in forum C Programming
    Replies: 5
    Last Post: 03-27-2002, 11:49 AM