CURSES: Uniform method to read keypad keys and function keys
Posted on 1997-06-11
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() )
These are the mode settings after initscr().
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 !