ProgrammingThis 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.
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.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.