日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > linux >内容正文

linux

kbhit linux windows通用,_kbhit() for Linux

發布時間:2024/3/13 linux 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 kbhit linux windows通用,_kbhit() for Linux 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

_kbhit() for Linux

發布時間:2006-09-18 20:52:57來源:紅聯作者:Service

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 should 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, particularly 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() calls 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;

}[/code]

總結

以上是生活随笔為你收集整理的kbhit linux windows通用,_kbhit() for Linux的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。