I wish I could help you specifically with ncurses, however I have not used it in nearly 10 years. However, I think what you want is to be able to set up non-blocking I/O, such that there is no delay between obtaining a keystroke from the user.
I know that ncurses offers this feature; it's touted all of the time on this forum. If you need to overcome this hurdle immediately, and yet standby for a 'better' solution, for now you should be able to use something like this to setup your input-stream to be non-blocking:
Code:
#include <stdio.h>
#include <termios.h>
#include <unistd.h>
#include <string.h>
int getch()
{
int ch;
struct termios old;
struct termios tmp;
if (tcgetattr(STDIN_FILENO, &old))
{
return -1;
}
memcpy(&tmp, &old, sizeof(old));
tmp.c_lflag &= ~ICANON & ~ECHO;
/* setup stdin to be non-blocking */
if (tcsetattr(STDIN_FILENO, TCSANOW, (const struct termios*) &tmp))
{
return -1;
}
/* get input */
ch = getchar();
/* re-enable stdin blocking */
tcsetattr(STDIN_FILENO, TCSANOW, (const struct termios*) &old);
return ch;
}
I'm not stating this is exactly what you need, but it is close. It is probably best to turn-off (disable) blocking one time in your code, then from there, acquire input using getchar(). When you app is done, if you need, you can restore, or turn-on, blocking again.
Bookmarks