Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   How to recognize the Function keys in C (http://www.velocityreviews.com/forums/t440713-how-to-recognize-the-function-keys-in-c.html)

Broeisi 12-31-2005 04:26 PM

How to recognize the Function keys in C
 
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

usenet@zevv.nl 12-31-2005 04:36 PM

Re: How to recognize the Function keys in C
 
Broeisi <broeisito@gmail.com> 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

eerok 12-31-2005 05:42 PM

Re: How to recognize the Function keys in C
 
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



Gordon Burditt 12-31-2005 08:40 PM

Re: How to recognize the Function keys in C
 
>> 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

Randy Howard 12-31-2005 11:01 PM

Re: How to recognize the Function keys in C
 
Broeisi wrote
(in article <4f-dnasT5_2jLCveRVny0Q@casema.nl>):

> 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







All times are GMT. The time now is 08:12 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.