Solved

CURSES: Uniform method to read keypad keys and function keys

Posted on 1997-06-11
1
229 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

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 Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
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.

895 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now