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?
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.
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?
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")
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?
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.
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.