0

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

Ryan Keough
  • 121
  • 1
  • 8
  • OT: the posted code snippet will never find the 'enter' key, unless it is the very first item in `stdin` because it never goes back to look for another key. Suggest: `int ch; while( (ch = getch()) != KEY_ENTER ){;}` – user3629249 Feb 25 '20 at 19:10
  • @user3629249 maybe I should have included more than that. I just tried with "ch = getch() != KEY_ENTER" but that didn't work. Allow me to edit the min. rep. code – Ryan Keough Feb 25 '20 at 19:20
  • did you forget to include the parens `(` and `)` as shown in my comment? – user3629249 Feb 25 '20 at 20:05
  • @user3629249 I edited my original statement, I included the full code that should include the blurb about my issues. I used the parenthesis like you used there – Ryan Keough Feb 26 '20 at 01:14

0 Answers0