How to recognize the Function keys in C

Discussion in 'C Programming' started by Broeisi, Dec 31, 2005.

  1. Broeisi

    Broeisi Guest

    Hello,

    I'm trying to write a console based program in C in Linux.
    I want to use the function keys in my program, but I don;t know how to
    let the C program know when for exmaple the F1 key is pressed.

    I want to be able to use all 12 Function keys in my program.
    Can someone help me maybe on this one?

    I guess that those function keys also have an ascii number.
    But so far I haven't seen none of the funtion keys in my ascii chart.

    Cheers,

    Broeisi
     
    Broeisi, Dec 31, 2005
    #1
    1. Advertising

  2. Broeisi

    Guest

    Broeisi <> wrote:

    > I'm trying to write a console based program in C in Linux.
    > I want to use the function keys in my program, but I don;t know how to
    > let the C program know when for exmaple the F1 key is pressed.
    >
    > I want to be able to use all 12 Function keys in my program.
    > Can someone help me maybe on this one?
    >
    > I guess that those function keys also have an ascii number.
    > But so far I haven't seen none of the funtion keys in my ascii chart.


    <Offtopic>

    Depending on the terminal type, function keys usually generate a specific
    sequence of characters, often starting with ESC (0x1b). For example, on my
    terminal, the F1 key generates the string "\x1b\x5b\x31\x31\x7e"

    Reading function keys is something very system- and OS-specific and is not
    possible with only ANSI-C; you might want to ask this question in the
    appropriate newsgroup that discusses programming on unix/linux. You also
    might want to look into the 'curses' and/or 'termcap' libraries, which are
    specially designed to handle keyboard and screen IO on unix systems.



    --
    :wq
    ^X^Cy^K^X^C^C^C^C
     
    , Dec 31, 2005
    #2
    1. Advertising

  3. Broeisi

    eerok Guest

    On Sat, 31 Dec 2005 10:26:38 -0600, Broeisi wrote:

    > Hello,
    >
    > I'm trying to write a console based program in C in Linux.
    > I want to use the function keys in my program, but I don;t know how to
    > let the C program know when for exmaple the F1 key is pressed.
    >
    > I want to be able to use all 12 Function keys in my program.
    > Can someone help me maybe on this one?
    >
    > I guess that those function keys also have an ascii number.
    > But so far I haven't seen none of the funtion keys in my ascii chart.


    Study the source for something like vim, which is a mature,
    portable codebase that uses the keyboard extensively.

    --
    "The secret of being boring is to say everything." - Voltaire
     
    eerok, Dec 31, 2005
    #3
  4. >> I want to be able to use all 12 Function keys in my program.
    >> Can someone help me maybe on this one?
    >>
    >> I guess that those function keys also have an ascii number.
    >> But so far I haven't seen none of the funtion keys in my ascii chart.

    >
    ><Offtopic>
    >
    >Depending on the terminal type, function keys usually generate a specific
    >sequence of characters, often starting with ESC (0x1b). For example, on my
    >terminal, the F1 key generates the string "\x1b\x5b\x31\x31\x7e"
    >
    >Reading function keys is something very system- and OS-specific and is not
    >possible with only ANSI-C; you might want to ask this question in the
    >appropriate newsgroup that discusses programming on unix/linux. You also
    >might want to look into the 'curses' and/or 'termcap' libraries, which are
    >specially designed to handle keyboard and screen IO on unix systems.


    It is possible to read the characters generated by a function key
    in a system-independent manner. To figure out what sequence of
    characters go with what keys, you can ask the user to "Press the
    F1 key followed by the key used to end a line of input (typically
    Enter or Return)", then fgets() (possibly multiple times if your
    initial buffer wasn't long enough) the result and save it. This
    will screw up if the function keys generate something interpreted
    as a newline. Repeat for other function keys that you will use.

    Distinguishing the function key from an individually-typed sequence
    that matches it isn't practical (most programs that do this depend
    on character-at-a-time I/O and timing). On many systems, this
    reduces to "don't try to use the ESCAPE key as something the user
    will type by itself".

    C also doesn't have non-blocking or character-at-a-time I/O, so the
    user has to press ENTER (or whatever) to finish a line of input
    before it's visible to the program. This severely messes up any
    idea you might have about the user pressing F1 and having a help
    screen appear immediately.

    The "termcap" database doesn't do anything that can't be done in
    ANSI C, although the data contained in it is terminal-specific.
    ANSI C allows getenv("TERM") which can be used as a key into the
    database to look up the sequences generated by various function
    keys on the terminal (or emulation thereof) that the user is using.

    Gordon L. Burditt
     
    Gordon Burditt, Dec 31, 2005
    #4
  5. Broeisi

    Randy Howard Guest

    Broeisi wrote
    (in article <>):

    > Hello,
    >
    > I'm trying to write a console based program in C in Linux.


    Google for curses and/or ncurses, possibly with the addition of
    "example code". It's off-topic here, because standard C doesn't
    know what a function key is. Many C implementations are
    available for platforms with no keyboard at all.

    Even worse, you'll discover that not all terminal types even use
    the same mapping for keys, so you even that is not standard, but
    that is one of the thing that curses can help with (provided you
    have correct terminal type descriptions for the associated
    hardware).


    --
    Randy Howard (2reply remove FOOBAR)
    "The power of accurate observation is called cynicism by those
    who have not got it." - George Bernard Shaw
     
    Randy Howard, Dec 31, 2005
    #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. sandeep Kanwal

    serial keys/validation keys

    sandeep Kanwal, Oct 29, 2004, in forum: C++
    Replies:
    1
    Views:
    585
    Mike Wahler
    Oct 29, 2004
  2. Harry George
    Replies:
    9
    Views:
    705
    sonal
    Jun 13, 2006
  3. Replies:
    10
    Views:
    728
    Daniel T.
    Feb 3, 2006
  4. alan
    Replies:
    3
    Views:
    365
    Victor Bazarov
    Nov 28, 2007
  5. A. Farber
    Replies:
    10
    Views:
    238
    A. Farber
    Jun 12, 2004
Loading...

Share This Page