-   Linux - Newbie (
-   -   trapping keypress for navigating a menu (

kaleeswarans 04-14-2001 07:55 AM

g++ or gcc in debian linux. No curses.h or ncurses.h I have a menu of seven options. I want user to pres the arrow keys and navigate the options. I don know how to trap an arrow key pressed by the user. getchar() required a press of the enter key. Any other way around? No Xwindows please. This is pure character based menu (just like the foxpro PROMPT type)

thank you for anyhelp

MacKtheHacK 07-25-2003 05:09 PM

It sounds like you're trying to process input character by character, right? But the problem is, standard input is line buffered, which means that your program doesn't receive any input from the device until the user has entered an entire line. You need to set standard input to be unbuffered, so that you get each character as it is typed. What you need to do is set the terminal device's CBREAK flag, and turning off echoing would be a good idea too.

But you really don't want to do that directly yourself, because you would create a lot of problems for your code to handle. For example, those arrow keys send escape sequences of several characters, and you would have to know how many to read in. Also, different terminals use different sequences for the arrow keys. Fortunately, all these problems have been solved. What you should be using is "ncurses" (do a "man ncurses" to get started). This is a terminal-independent, text-based display package. While it's main focus is on creating output on text terminals, it also handles all the details of input processing for you, which is what you're after. It provides functions such as cbreak() and noecho() to do the things you want. See curs_inopts(3X) for more details.

All times are GMT -5. The time now is 07:33 AM.