_ Kbhit () forLinux

Source: Internet
Author: User
_ Kbhit () forLinux-general Linux technology-Linux programming and kernel information. For details, refer to the following section. The Windows _ kbhit () function returns a nonzero value when a character is waiting in stdin, otherwise it returns zero. it does not consume the character and does not block. A common use of this function is to test when the user has pressed a key in an interactive console application. POSIX (and therefore Linux) lacks a similar function. although it does not directly support this functionality, the GNU Curses library can be used to implement _ kbhit () on Linux. here I present an alternate solution to Curses that implements _ kbhit () using only standard libraries. it shoshould port directly to OS X, AIX, and other Unix-like operating systems as well as Linux. this implementation has two advantages over a Curses based approach. the Curses library must be initialized from main () before it can be used. this implementation is a drop-in replacement for _ kbhit () when porting from Windows and does not require any explicit initialization. also, no external library must be installed and linked.

The ioctl () function is a low level method for controlling I/O drivers. its arguments depend on the stream and driver being used. the last group of lines in _ kbhit () uses this function to determine whether data is waiting on stdin. this implementation was written specifically for Linux and may not port. A more general implementation can replace these lines with a call to the select () function as follows:

Timeval timeout;
Fd_set rdset;

FD_ZERO (& rdset );
FD_SET (STDIN, & rdset );
Timeout. TV _sec = 0;
Timeout. TV _usec = 0;

Return select (STDIN + 1, & rdset, NULL, NULL, & timeout );

Console input is typically line buffered on Linux, special when running over Telnet or SSH. this means that a keypress does not appear on stdin until a newline character is sent. the ioctl () or select () CILS cannot determine if characters are in the buffer waiting for a newline, and can indicate that there are zero characters waiting when really several keys have been pressed.

To fix this, the first code block in _ kbhit () disables line buffering. this uses routines from the termios. h header. another author offers a longer method that uses only ioctl () and avoids termios. h. because termios. h is a standard header on most systems I see no reason to avoid it. both implementations use a static variable to detect the first call and disable buffering then. output buffering on stdout is still enabled. if you wish to print to stdout and see the result before a newline is sent, use the command flush (stdout) as shown in the simple demo.

The Linux version of _ kbhit () now performs to the same specification as the Windows version. The actual value of the non-zero integer returned will be different on the two platforms, however.
CODE :/**
Linux (POSIX) implementation of _ kbhit ().
Morgan McGuire, morgan@cs.brown.edu
*/
# Include
# Include
# Include
# Include Int _ kbhit (){
Static const int STDIN = 0;
Static bool initialized = false; if (! Initialized ){
// Use termios to turn off line buffering
Termios term;
Tcgetattr (STDIN, & term );
Term. c_lflag & = ~ ICANON;
Tcsetattr (STDIN, TCSANOW, & term );
Setbuf (stdin, NULL );
Initialized = true;
} Int bytesWaiting;
Ioctl (STDIN, FIONREAD, & bytesWaiting );
Return bytesWaiting;
}/////////////////////////////////////// ///////
// Simple demo of _ kbhit ()

# Include Int main (int argc, char ** argv ){
Printf ("Press any key ");
While (! _ Kbhit ()){
Printf (".");
Fflush (stdout );
Usleep (1000 );
}
Printf ("\ nDone. \ n"); return 0;
}
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.