This is the mail archive of the cygwin@cygwin.com mailing list for the Cygwin project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

check keyboard for pending characters


This list has been very useful to me in the last couple of days, so I am
calling on you again.

The program I am trying to debug was developped and runs well under Linux
2.0.36 and ncurses 4.2. I am trying to move it to CYGWIN because my customer
wants it to run under Windows and I do not have time to write new software
or convert to MSC++.

I have been using the function below to check if there were characters in
the keyboard buffer without actually removing them from the buffer.

This may not be the best way to do this, but it works under Linux. However,
it does not seem to work well under CYGWIN, which sometimes returns TRUE
even though there is no char in the buffer, and that causes strange
problems. Under CYGWIN, gdb also locks up when I try to debug that section
of code. Maybe select() is overkill for something like this?

I believe I could use wgetch() and set NODELAY, but that would require a
rewrite of the code which now uses kbhit() because if there were a char
pending, wgetch() would get it and I do not know how to put it back in the
keyboard buffer for the rest of the code to work. 

Any suggestion?

Didier

/*-------------------------------------------------------------------*
*	kbhit()
*
*	check if character pending in keyboard buffer
*	( idea borrowed from The Linux Journal article
*	of Sept 95 issue 17 )
*--------------------------------------------------------------------*/
int kbhit( void ){

	struct timeval tv;
	fd_set read_fd;

	/* do not wait at all, not even a microsecond */
	tv.tv_sec = 0;
	tv.tv_usec = 0;

	/* must be done first to initilize read_fd */
	FD_ZERO( &read_fd );

	/* makes select() ask if input is ready :
	* 0 is the file descriptor for stdin  
	*/
	FD_SET( 0, &read_fd );

	/* the first parameter is the number of the 
	* largest file descriptor to check + 1.  
	*/
	if( select( 1, &read_fd,
			NULL,		/* NO writes */
			NULL,		/* NO exceptions */
			&tv ) == -1 )
		return( FALSE );			/* An error occured
*/

	/* read_fd now holds a bitmap of files that are
	* readable. We test the entry for the standard
	* input (file 0). 
	*/
	if( FD_ISSET( 0, &read_fd ))
		/* character pending on stdin */
		return( TRUE );

	/* no characters were pending */
	return( FALSE );

} /* end kbhit() */

--
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
Bug reporting:         http://cygwin.com/bugs.html
Documentation:         http://cygwin.com/docs.html
FAQ:                   http://cygwin.com/faq/


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]