?
Solved

CURSES: Uniform method to read keypad keys and function keys

Posted on 1997-06-11
1
Medium Priority
?
245 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 4

Accepted Solution

by:
jos010697 earned 400 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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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

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…
Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
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.
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
Suggested Courses
Course of the Month12 days, 17 hours left to enroll

777 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