You'll (almost certainly) want to use the curses library.
If you want to pause to wait for input, you call halfdelay
to set the time to wait for input. If the user hasn't entered anything in that time, it'll return ERR
(or some error code anyway--been a while so I can't remember for sure what). If memory serves, however, the shortest delay you can specify is 1/10th of a second; there's no way to tell it to just pause for (say) up to 2 ms. Edit: thinking about it, it seems like there is a timeout
(or something like that) that lets you set a timeout in milliseconds.
More often, you just want to react to input if there is any, and just continue other processing if here is none. In this case, you'd call nodelay
.
Either way, you also want to call cbreak
to disable line buffering at the OS level, so any key the user presses will be available to the program immediately instead of waiting for them to press return before you receive the input.
I'd guess you probably also want to call noecho()
so the user's input isn't echoed by getch
. For the kind of program you seem to be talking about, echoing the input command is rarely desired.
Then inside your loop, you'll call getch()
to get the key input data (if any). If you get input, you process it. If it returns the error code to indicate there's no input, you continue your other processing.
So the code would be structured something like:
cbreak();
noecho();
nodelay(mywindow, true);
while (1) {
int ch;
if (ERR != (ch = getch()))
process(ch);
update_screen();
do_other_processing();
}