Solved

CURSES: Uniform method to read keypad keys and function keys

Posted on 1997-06-11
1
224 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
Comment Utility
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Get filename and folder into excel 7 67
Define unique primary key 9 81
Window placement 17 64
sumHeights  challenge 17 59
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
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.
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.

771 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

11 Experts available now in Live!

Get 1:1 Help Now