NCurses: Key codes not working

I'm using NCurses on Linux Mint (Cinnamon). I'm loving it.

I'm writing a program, but I'm having trouble with character key codes. I have switched to using integers to analyze them, but the results are the same. The only key that does work is backspace, and it still doesn't match up with what's in the curses.h header file.

Example:

enter key should be 527, but no matter what (unsiged int, char, int) it always returns 10.

Backspace returns two hundered somthing, even though it's supposed to be 407.

I don't know what to do... The key codes are of an interger type, so I think this should work. However, in the header file, it also says this:

curses.h, line 1,389:
* Pseudo-character tokens outside ASCII range. The curses wgetch() function
* will return any given one of these only if the corresponding k- capability
* is defined in your terminal's terminfo entry.


I'm not sure what this means, but there is a function keyok(int, bool) defined, and that's the only thing I can think of that the quote could be refering to...

Can anyone help? Thank you for your time!

Also, if you want to look at the full source code, or even contribute, the repository is on github: https://github.com/BeenEncoded/linuxtimer
Last edited on
I haven't looked too hard at your source, but it looks like you may be screwing up curses by the way you wrap it.

Call initscr() etc at the beginning of your program, once.
Call endwin() only once at the end of your program.

The other possibility (exceedingly unlikely) is that your terminfo database (that defines all the codes used to communicate with the terminal I/O) is boogered. Unless you are using some very exotic hardware, though, I doubt this is the problem.
I made cin, cout, and the base class static so that they're only initialized once throughout the entire program. I ran some tests, and it still used the base class' constructor (even though I explicitly defined one for the I/O sub-classes). I had to write a static bool to keep it from initializeing NCurses multiple times. It is only initialized once now, and I may re-design the wrapper based on my findings... (I thought it would use the constructor for the class, not it's base! I thought it was only supposed to do that if you casted it as the base class...)

In any case, the problem is still there: the key codes aren't working...

I pushed a topic branch if you want to check out the code:

https://github.com/BeenEncoded/linuxtimer/tree/topic2_topic1_fixingCursesWrapper
Last edited on
Maybe this thread on your dist's forum will help: http://forums.linuxmint.com/viewtopic.php?f=47&t=90884
Note that Thomas Dickey is the maintainer of ncurses.

Backspace returns two hundered somthing, even though it's supposed to be 407.
That's 0407 (octal) == 263 (decimal)
oh... it's octal. Well, I looked at curses.h and it's of type int, so I don't know how it's computing it...

Also, that still doesn't explain why KEY_ENTER fails to register... I gave up though. I'm just going to log thier values and create my own definitions. It's not portable, but it will have to work for now.
Last edited on
Found another post by Thomas Dickey: http://lists.gnu.org/archive/html/bug-ncurses/2011-01/msg00011.html

I downloaded your project and changed the following in common.cpp as per the above. I can set hours, minutes & seconds and then backspace. Was that the problem you were having?.
1
2
3
4
5
6
7
case KEY_ENTER:  // line 88
case '\n':
case '\r':
{
    return input;
}
break;



Yeah, '\n' works, but I want NCurses key codes to work... Like the escape key, etc...
Last edited on
are you looking to be able to press keys like enter and the arrows and what not? you need to set up keypad(stdscr, TRUE); and then say you wanted it to do something when you pressed the up key do
1
2
3
4
5
int ch = getch();
switch (ch) {
  case KEY_DOWN:
    //stuff that happens when you press down go here
  }

that switch ch i think indicates that you are about to press a special button. in this case it is the up arrow. i havent had any use with the enter key but i am almost 100% sure it is used like Key_Enter: instead of all caps like in this case. i hope i helped out :)
Last edited on
Topic archived. No new replies allowed.