How to recognize the Function keys in C

B

Broeisi

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
 
U

usenet

Broeisi said:
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.
 
E

eerok

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

Gordon Burditt

I want to be able to use all 12 Function keys in my program.
<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
 
R

Randy Howard

Broeisi wrote
(in article said:
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).
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top