NCURSES- Arrow keys not detected?

Within my code, there is a point where I am trying to see which arrow key a person pressed. No matter which key I press, the result is always default.
I have enabled keypad(stdscr, TRUE); and echo(); as well as raw();, and there is a refresh(); directly before the following code, if that helps at all.



1
2
3
4
5
6
7
8
9
10
11
12
char ch1 = getch();
switch(ch1)
{
    case KEY_LEFT: printw("You pressed left.\n"); break;
        
    case KEY_RIGHT: printw("You pressed right.\n"); break;
    
    case KEY_UP: printw("You pressed up.\n"); break;
        
    case KEY_DOWN: printw("You pressed down.\n"); break;
    default: printw("You didn't press an arrow key.\n");
}




Also, I'm not sure if I should be posting this on the normal windows forum or beginners, since NCURSES isn't taught in any c++ courses I've taken, and it seems more obscure than, well, the standard library. For future reference, should I post these kinds of questions here or elsewhere?
Last edited on
At the very least, refresh() must be called after you print to the buffer.
Here is a complete example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <ncurses.h>

int main() {
  initscr();
  clear();
  noecho();
  cbreak();
  keypad(stdscr, true);

  printw("Press 'q' to quit.");
  refresh();

  for (int ch = getch(); ch != 'q'; ch = getch()) {
    mvprintw(1, 0,
               ch == KEY_UP    ? "up"
             : ch == KEY_DOWN  ? "down"
             : ch == KEY_LEFT  ? "left"
             : ch == KEY_RIGHT ? "right" : "");
    clrtoeol();
    refresh();
  }

  endwin();
}


I think it's fine to post here. It might be specific to Unix programming, but there is a port of NCurses (called PDCurses) for Windows anyway.
Last edited on
getch returns an int.

It's not so important where you post but it's definitely important to post something that can be run without helpers needing to add a bunch of boilerplate to.
https://linux.die.net/man/3/getch
int getch(void);

note that the function returns an int


> I have enabled keypad(stdscr, TRUE); and echo(); as well as raw();, and there is a refresh();
I'd rather have code, thanks.
Why DOESN'T my code work?
In the code posted by mbozzi, it seems that on lines 15-20 or so, what is similar to if statements seems to be of use, I don't remember the name of the ? operator.

What's causing the input not to be evaluated properly, yet the evaluative expression by mbozzi seems to work fine?
Can't you read?
getch RETURNS AN INT!!!!
Just like getchar() in stdio.h.
That's a beginner mistake, dude.
That's why I posted on beginner forums, I'd appreciate if you're not so rude. I don't find it helpful.

As I said, the code that mbozzi posted seems to work, and in my experience, all chars are associated with an integer in ASCII, so I would assume KEY_ has an integer value as well.

Edit: In fact, I've discovered this bout of code that made me assume as much would work:
1
2
3
4
5
6
printw("Type any character to see it in bold\n");
	ch = getch();			/* If raw() hadn't been called
					 * we have to press enter before it
					 * gets to the program 		*/
	if(ch == KEY_F(1))		/* Without keypad enabled this will */
		printw("F1 Key pressed")


It comes directly from the NCURSES how-to, here:
http://www.ibiblio.org/pub/Linux/docs/HOWTO/other-formats/html_single/NCURSES-Programming-HOWTO.html#KEYPAD
Last edited on
If you're going to argue with the manual, we cannot help you.

Post a complete program that exhibits the problem.
Tduck: In that example you found in the how-to, what's the type of 'ch'? It's the first declaration in main().
See also the example in http://www.ibiblio.org/pub/Linux/docs/HOWTO/other-formats/html_single/NCURSES-Programming-HOWTO.html#KEYS

The problem with this:
 
char ch1 = getch();
is that getch() may return information in its return value that doesn't fit into a char. For example, the value of KEY_LEFT could easily be 1026 (I don't know its actual value and it's not important). On a desktop platform, if you try to assign 1026 to a char variable, the variable will hold the value 2 instead. You will get other, similar problems with other values. E.g. (char)384 == (char)-128 (again, this is true on typical desktop platforms, but not necessarily elsewhere).
Your compiler should have issued a warning about loss of information when assigning an int to a char. Did you ignore that warning?
Last edited on
I think I know where the confusion came in. It seems that on the website that the variable ch, I misinterpreted it as a character, not an integer. Thank you helios and mbozzi.

1
2
3
4
5
6
7
8
9
10
int intPut;
while(intPut != 'q')
{
    intPut = getch();
    if(intPut == KEY_LEFT) { printw("You pressed left.\n"); }
    else if(intPut == KEY_RIGHT) { printw("You pressed right.\n"); }
    else if(intPut == KEY_UP) { printw("You pressed up.\n"); }
    else if (intPut == KEY_DOWN) { printw("You pressed down.\n"); }
    else { printw("You didn't press an arrow key.\n"); }
}
Last edited on
characters ARE integers. input should probably have been char or unsigned char, depending on how your constants KEY_RIGHT etc are defined. However, int works, its just a little confusing to use int here. You will frequently see it the other way around (code that uses char as an integer) in older, pre 'auto everything' code.
Last edited on
Topic archived. No new replies allowed.