cinputpdcurses

PDCurses Hanging on Input After Loop


While I was building a PDCurses application, I came across an issue where while I was waiting for input, it would just hang and not pass any data along to the program, even though it was still technically reading input. I managed to boil it down to the following code:

#include <curses.h>

main()
{
    initscr(); // initialize screen
    noecho(); // don't echo the keys to the screen
    cbreak(); // no waiting for enter key
    clear(); // clear screen

    while (getch() != 13) // loop until they press ENTER
        mvaddch(0, 0, 'a'); // GETS PAST HERE

    getch(); // HANGS; WINDOWS IS RECEIVING INPUT FROM THE KEYBOARD (turn off noecho()), BUT NEVER RETURNS IT TO THE ACTUAL PROGRAM

    return 0;
}

To put it in context as to how this was displayed in my program, I had a loop in main() that infinitely called a function that would display a menu until the ENTER key was pressed. However, if the user pressed a specific key while in the menu, it would call another menu and get input from that function. In the above code, I simplified it by just having two subsequent getch() calls.

My question is, why does it hang only after a loop checking for the ENTER key?? Shouldn't this be a simple thing?

Also, my PDCurses application is running on the Windows cmd. (13 = ENTER; KEY_ENTER does not work here)


Solution

  • Pressing the ENTER key makes getch() return 10 (LF), not 13 (CR). Thus, the second getch() is never reached. This is not specific to PDCurses or to Windows. You can change PDCurses' behavior in this regard by calling raw(), but that may be PDCurses-specific.