Solved

CURSES: Uniform method to read keypad keys and function keys

Posted on 1997-06-11
1
237 Views
Last Modified: 2013-12-26
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 !
0
Comment
Question by:footloose
1 Comment
 
LVL 4

Accepted Solution

by:
jos010697 earned 200 total points
ID: 1293388
Use the wgetch() routine instead; it returns special token
values (outside the 8 bit range) if the keypad has been
turned on (keypad(win, true) ... Maybe you have to fiddle
with the timeout values though, because the frequency
of the incoming escape sequences to be interpreted
by curses may vary considerably between different
terminals ... check it out.

kind regards,

Jos aka jos@and.nl

ps. don't set raw mode; it inhibits that escape sequence
interpretation.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
repeatSeparator  java  challenge 13 58
withoutTen challenge 14 137
Problem to event 3 114
Trying to run powershell  in a batch file. How do I do this? 8 117
Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question