cs442a07@cs.iastate.edu (Sunny Gulati) (05/15/91)
Hi, I need help with curses, I dont know if this is the place to post
this article but I will try anyway.
Working on a unix system, how do you get it to check if a keypress is
waiting for you? I have it in nonl() raw() noecho(), and I have a part
of code that says:
if (typeahead(stdin)) {
do my stuff
}
monitor_incoming_lines();
Does typeahead return True if there are keypresses?
are there any other libraries other than curses which will help me?
(THe above segment seems to always evaluate typeahead to true.... )
THnks for any replies.
Sunny G
taa65@ccvax.iastate.edu
exspes@gdr.bath.ac.uk (P E Smee) (05/17/91)
In article <cs442a07.674280127@zaphod> taa65@ccvax.iastate.edu writes: >Hi, I need help with curses, I dont know if this is the place to post >this article but I will try anyway. > >Working on a unix system, how do you get it to check if a keypress is >waiting for you? I have it in nonl() raw() noecho(), and I have a part >of code that says: This is most certainly not the right newsgroup (comp.lang.c would have been better, or comp.unix.questions) but it'll probably save bandwidth to answer you rather than getting into an argument about it. Following is a chunk of code lifted from one of my curses applications. The basic idea of it is to refresh the screen if there is no typeahead pending, but to delay the refresh until all user commands have been processed if the user has gotten ahead of us. In the networked app I use it in, it can save blasting up menu screens that the user is not going to look at anyway, so makes things look faster. :-) It uses 'select', see select(2). I'm not sure if it is the canonical or best method, but it works. /* Don't remember whether you need both of the following .h's, or just one or the other. Also, you may need a few more declarations. Sorry... */ #include <signal.h>; #include <sys/time.h>; static void wcondrefresh (where) WINDOW * where; { int rfds; /* Note that I'm actually waiting 5000 micro-secs to see if the user is about to type something in. (Gives network time to drain.) For a zero time wait, use timeout initialized to {0, 0}. Passing a NULL timeval ptr means wait forever. See select(2). */ static struct timeval timeout = {0, 5000}; rfds = 1 << fileno (stdin); if (select (1, &rfds, (int *) NULL, (int *) NULL, &timeout) <= 0) { (void) wrefresh (where); /* No pending input, go ahead and refresh */ } else { /* There is pending input, or an error occurred during the select() processing, do something else. */ } return; } -- Paul Smee, Computing Service, University of Bristol, Bristol BS8 1UD, UK P.Smee@bristol.ac.uk - ..!uunet!ukc!bsmail!p.smee - Tel +44 272 303132