I am writing a program where I want the user to click the "enter" key in order for the program to continue.
So I use the following code
int ch = getch();
while(ch != KEY_ENTER){}
This code works with literally every other ncurses keys such as "KEY_RIGHT" or "KEY_DOWN"
The current solution I have been using is that instead of using "KEY_ENTER", I instead use
while(ch != '\n'){}
And that seems to do the trick. I don't feel super happy about using the newline character, but if it is fine then whatever. I'd rather use KEY_ENTER if possible.
Love to hear feedback, so please let me know
-Edit
#include <ncurses.h>
#include <string.h>
#define BORDER_INSIDE_X 100
#define BORDER_HEIGHT 42
int main(void)
{
int i, row, col, ch;
size_t menuLengths;
char op1[] = "Play";
char op2[] = "Settings";
initscr();
getmaxyx(stdscr, row,col);
keypad(stdscr, TRUE);
noecho();
curs_set(0);
menuLengths = strlen(op1);
move((row / 2) + (BORDER_HEIGHT / 4) + 3, (col / 2) - (BORDER_INSIDE_X / 4) - (menuLengths / 2));
for(i = 0; i < menuLengths; ++i)
{
printw("%c", op1[i]);
}
menuLengths = strlen(op2);
move((row / 2) + (BORDER_HEIGHT / 4) + 3, (col / 2) + (BORDER_INSIDE_X / 4) - 2 - (menuLengths / 2));
for(i = 0; i < menuLengths; ++i)
{
printw("%c", op2[i]);
}
menuLengths = strlen(op1);
move((row / 2) + (BORDER_HEIGHT / 4) + 3, (col / 2) - (BORDER_INSIDE_X / 4) - menuLengths);
printw(">");
refresh();
//ch = getch();
while((ch = getch()) != KEY_ENTER)
{
if(ch == KEY_RIGHT)
{
move((row / 2) + (BORDER_HEIGHT / 4) + 3, (col / 2) - (BORDER_INSIDE_X / 4) - menuLengths);
printw(" ");
menuLengths = strlen(op2);
move((row / 2) + (BORDER_HEIGHT / 4) + 3, (col / 2) + (BORDER_INSIDE_X / 4) - menuLengths);
printw(">");
}
else if(ch == KEY_LEFT)
{
move((row / 2) + (BORDER_HEIGHT / 4) + 3, (col / 2) + (BORDER_INSIDE_X / 4) - menuLengths);
printw(" ");
menuLengths = strlen(op1);
move((row / 2) + (BORDER_HEIGHT / 4) + 3, (col / 2) - (BORDER_INSIDE_X / 4) - menuLengths);
printw(">");
}
refresh();
//ch = getch();
}
endwin();
return 0;
}
-Ryan