Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game. |
| Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
03-06-2003, 05:19 PM
|
#1
|
|
Member
Registered: Feb 2003
Location: huntsville, al
Distribution: debian, opensolaris
Posts: 99
Rep:
|
Trapping keystrokes in C
I am trying to build a program with C, which will run as a text-mode console application under Linux. I need some functions to respond on a keystroke, e.g. anytime the user presses 'h' a help screen pops up. I don't want the user to have to press [enter] after the keystroke and I also want to be able to capture the [enter] key by itself.
I've read that this can be done under curses, but I want this to remain a strictly text-mode app. I also read that there is a function getch() in <conio.h> that seems to be what I'm looking for; I put these into a sample program and the compiler (gcc) didn't recognize either one. Are those in C++ only? I'm pretty knowledgeable about Basic, but I know much less about C.
Thanks
|
|
|
|
03-06-2003, 05:40 PM
|
#2
|
|
Senior Member
Registered: Jan 2003
Posts: 2,786
|
getch(0 does exist, but I don't remember the header it is in off-hand. conio.h sounds very familiar though.
Interesting, I just tried "man getch" and the system said it was in curses.h. It would probably do what you need.
|
|
|
|
03-07-2003, 04:29 AM
|
#3
|
|
Member
Registered: Aug 2002
Distribution: RH 7.x
Posts: 71
Rep:
|
Hi,
I don't think getch() will solve your problem if I've understood it, becasue you said the program should respond anytime "h" is pressed, but getch() works at a point, and is exactly an an unbuffered and not-echo getchar() only.
What you need is signal handling. So that the user can do whatever he is doing, and at any time he presses "Cntrl-h" or something you've defined, the execution passes to the handler and the popup help menu is visible.
adi
|
|
|
|
03-09-2003, 01:13 PM
|
#4
|
|
Member
Registered: Feb 2003
Location: huntsville, al
Distribution: debian, opensolaris
Posts: 99
Original Poster
Rep:
|
Thanks for your suggestions.
I did another (better) search on the Net, and found some free sourcecode that emulates getch() under Unix/Linux. I'll probably try to use that.
Actually, the user will have a prompt in my program where commands will be entered; I just didn't want them to have to press Enter afterwards. Being as the prompt is always on the screen and there aren't going to be any other processes running, I'm pretty sure that getch() will work.
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 03:21 AM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|