473,387 Members | 1,485 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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
Dec 31 '05 #1
4 6654
Broeisi <br*******@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
Dec 31 '05 #2
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
Dec 31 '05 #3
>> 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
Dec 31 '05 #4
Broeisi wrote
(in article <4f********************@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

Dec 31 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Michael G. Schneider | last post by:
On Nov 16th I asked about a problem where ASP pages, which were located on a UNC-share, would not work correctly. IIS would not recognize, if the ASP page has been changed. Nobofy had a hint...
1
by: Don W. | last post by:
I need to use some function keys as speed-keys (instead of clicking a button with the mouse.) Using this code doesn't display any codes for the function keys: <html><body> <SCRIPT...
0
by: OC | last post by:
I am attempting to trap special keys on the keypad for a high-speed dataentry application. It MOSTLY works, however it does not work on every computer. IOW, Keys.Divide, Keys.Multiply, and...
1
by: Steve | last post by:
I would like to override the IsInputKey function in order to trap the arrow up/down keys in the Key Events. I understand that arrow keys don't have a char representation and thus, hard to trap. ...
1
by: Sam Samson | last post by:
Greeetings All .. For my users convenience I have mapped function keys F2 .. F12 to change the tabs on the Tabcontrol. Works like a charm <ominous musicuntil one hits F8</ominous music> ...
5
by: CyberSoftHari | last post by:
How to Recognize key pressed when the application is in system tray? I am using a context menu with some hot keys (like Alt+Ctrl+Delete) once I press the key my appropriate command should...
9
by: Wingot | last post by:
Hey, I am using Visual Studio 2008 Beta 2 for some application development in C#, but I presume that the following question applies equally well to any environment.
5
by: =?Utf-8?B?Sm9obiBLb3R1Ynk=?= | last post by:
Hi all, We lease a non-managed Web Server running AV software but no IDS. It is Windows 2003 STD which receives automatic nightly Windows Security patches at 3AM. When I logged into the RDP...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.