Link to home
Start Free TrialLog in
Avatar of footloose
footloose

asked on

CURSES: Uniform method to read keypad keys and function keys

Is there a method to write a curses program that handles keypad characters like HOME, PageUp/PageDown, and function keys uniformly across all terminal types ? It should also be able to distinguish between Ctrl-A and up-arrow key.

Following the curses man pages, I tried to do a getch() followed by a switch() statement like this:

   switch( getch() )
   {
      case KEY_F1:
         ...
      case KEY_F2:
         ...
      case KEY_HOME:
         ...
   }


These are the mode settings after initscr().
   noecho();
   cbreak();
   nodelay(stdscr,TRUE);
   keypad(stdscr,TRUE);
   scrollok(stdscr,FALSE);
   attrset(A_ALTCHARSET);

It does not work. getch() does not seem to translate the keystrokes into KEY_xx codes. Worse, if I enable the keypad [ keypad( stdscr, TRUE ); ] the arrow keys generate Ctrl-A, Ctrl-B, etc. which is confused with manually typed Ctrl-A or Ctrl-B.

I tried setting raw() mode on, and that complicates matters: now I have to interpret every keystroke. Some keys produce escape sequences consisting of three to six characters. And that is very terminal-specific too.

Maybe what I should do is to build some kind of lookup table during startup using the terminfo database or some such thing and use it for that session. Is this approach right ?
If not, what is a working solution ?

Don't give me terminal-specific suggestions. Thanks !
ASKER CERTIFIED SOLUTION
Avatar of jos010697
jos010697

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial