Ncurses

Hey guys I am trying to take keyboard input in a c program. I want the down and up arrow keys to do something but every time I use them in the function getch() the char it returns is 27 which is the escape value.

while(1){
displayEnemyStatus(scale);
char userCommand = getch();
if(userCommand==KEY_UP){ /*Down List*/
scale++;
displayEnemyStatus(scale);
}
if(userCommand==KEY_DOWN){ /*Up Arrow*/
if(scale>-1){
scale--;
displayEnemyStatus(scale);
}
}
if(userCommand == 27){
clear();
printGrid();
wrefresh(window);
break;
}

            }
            break;
  1. If you're going to have the brackets at the end of the line, at least fix the tabbing so it's readable.

  2. Print userCommand as an int and see what comes out.

That just messed up from copying for some reason, but I will check what happens if I get the int. It still appears to print 27.

Do you have a special keyboard with lots of extra features?

Are you running some keyboard software?

no I am using Linux and my keyboard is a IBM model M keyboard.

oh, ok. Is it just ps2?

If so, I dunno what your issue is...

I see you like to live dangerously.

1 Like

maybe try something like

char c = '';
scanf(" %c", c); //note the space before the %

well I found that when I would do getch() and hit an arrow key it would send three messages to ncurses, 27,[,A for up arrow for example. The problem now is when I do getch() again and I want to hit the escape key it waits for a command but when I hit an arrow key it doesn't wait because that pushes multiple things while escape just pushes 27.

 while(!escape){
                clear();
                refresh();
                displayEnemyStatus(scale);
                char userCommand = getch();
                if(userCommand==27){
                getch();
                userCommand = getch();
                escape=1;
                if(userCommand=='B'){ /*Down List*/
                    scale++;
                    escape=0;
                }
                if(userCommand=='A'){ /*Up Arrow*/
                    if(scale>-1){
                        scale--;
                        escape=0;
                    }
                }
                }
                }
                    clear();
                    printGrid();
                    wrefresh(window);

Ah, yess.... forgot about escape sequence comes first for special characters.

I would check the freeglut code base. I know they are able to handle it just fine. See what they did.

Do you think there is a way to change it in system settings on Linux Mint to output a combined value like 66 or 65 or better yet if I could access everything in the ncurses input buffer without calling getch()?