I'm still confused and I don't understand how would I implement this on my code... For instance, I don't use the getch() directly, I have a custom function to read input with getch() to fit my needs. On another piece of code, I have while loop that will read the directional keys (to move the cursor in a matrix).
The function I'm using is this:
Code:
void getInput(int l, int c, string str, int limit, bool pwd) {
int i = 0;
int ch = 0;
// Activates the cursor and changes the input color
curs_set(1);
attron(COLOR_PAIR(6));
// Loops until the user presses the Return key
while(ch != '\n') {
ch = mvgetch(l, c);
if(ch != '\n' && ch != '\t') {
// Did the user pressed the backspace key?
if(i > 0 && (ch == KEY_BACKSPACE || ch == 127)) {
move(l, --c);
attroff(COLOR_PAIR(6));
mvprintw(l, c, " ");
attron(COLOR_PAIR(6));
refresh();
i--;
} else {
// Is the char invalid or the user went over the limit?
if(i < limit && (isalnum(ch) || isspace(ch))) {
str[i++] = (char)ch;
// Hide input? (password)
if(pwd) mvprintw(l, c++, "*");
else mvprintw(l, c++, "%c", ch);
refresh();
}
}
}
}
// Deactivates the cursor and removes the input color
curs_set(0);
attroff(COLOR_PAIR(6));
// Terminates the string and/or removes the line termination
if(str[i-1] == '\n') {
str[i-1] = '\0';
} else {
str[i] = '\0';
}
}
And I use it like this:
Code:
char buffer[10];
getInput(0, 0, buffer, sizeof(buffer)-1, FALSE);
Could you provide me with the simplest and full example of how you would put a timer on screen updating everysecond and using getInput() to read user input? For instance, inside this loop:
Code:
do {
getInput(0, 0, buffer, sizeof(buffer)-1, FALSE);
} while(buffer[0] != 'q');