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相关推荐
- Selenium测试时清理chromedriver【linux\windows通用】
Selenium测试时清理chromedriver[linux\windows通用] 我们平时在使用chromedriver驱动时会存在未完全清楚的现象 解决思路 1.打开chromedriver时记 ...
- windows命令行下访问linux,Windows支持直接访问Linux子系统文件:你的下一台Linux何必是Linux...
原标题:Windows支持直接访问Linux子系统文件:你的下一台Linux何必是Linux 晓查 发自 凹非寺 量子位 报道 | 公众号 QbitAI 微软,致力于做最好的Linux发行版. 今天, ...
- 延时关机命令 linux,windows定时关机和linux定时关机的方法(shutdown命令)
windows版:由于各种原因我们可能需要定时关机的功能.在windows下常用的做法可能是使用一些工具或常用软件(例如酷我音乐盒)实现.但如果你没有该功能的软件又不想为了实现这个功能去增添软件,你可 ...
- kbhit linux windows通用,linux模拟windows的kbhit
linux模拟windows的kbhit Windows有个很不错的函数kbhit,可以检测是否有任何键按下了.例如我们可以检测键按下,来退出一个无限循环.还可以得到用户按下的这个键. 但是linux ...
- linux打印机通用协议,为 Linux 选择打印机
原标题:为 Linux 选择打印机 编译自: https://opensource.com/article/18/11/choosing-printer-linux 作者: Don Watkins 译 ...
- linux windows 字符编码转换,Linux操作系统下汉字编码的转换
因为项目的需要linux下将GBK编码转换为utf8编码,google一下,网上的相关资源比较少,下面的操作经过本人的反复试验.本例子同样适用于其他的编码转换. 有gbk到utf8的转换过程,需要经过 ...
- windows密钥连接linux,windows使用密钥登录linux
在用windows时管理linux服务器时,常会用到SecureCRT.Xshell以及开源的putty.在我工作环境大多都是采用密码认证的方式进行登录.今天对学习了些SecureCRT的密钥登录方式 ...
- Windows改装成Linux,Windows系统改装成Linux系统
说下背景:上级领导要求的将一台windows系统的电脑改装成Linux系统的电脑.弄了一天半的时间终于弄好了. 下面时操作过程以及自己遇到的一些坑. 一.制作一个启动盘 使用一个大于8G的U盘制作启动 ...
- linux windows 运维比较,linux与windows的比较_系统运维_linux操作_linux系统_课课家
用户做比较是一个学习的过程,深入学习的朋友们都喜欢拿Linux与Windows来进行比较,对比一下这个两种系统它们有什么不一样的地方,本文将为大家简单的进行这两种系统的比较,让大家可以简单的进行一个学 ...
最新文章
- 实现DFS之“油田”
- Spring Singleton,请求,会话Bean和线程安全
- c# Linq实现 获得某一个路径下所有文件的名(不含扩展名)
- linux7inittab文件不存在,Linux下修复inittab文件丢失的两种方法
- linux系统显卡驱动下载官网,NVIDIA显卡Linux系统驱动313.09版下载
- 基于socket编程的多人聊天室
- [转载]数字全息与计算全息
- Linux命令大全完整版
- HDR中HLG与PQ曲线的互转
- oracle学习入门系列之五内存结构、数据库结构、进程
- linux执行startx进入桌面黑屏,linux下误操作startx命令后,在登录界面进不去解决办法...
- 计算机怎么没有word文档,word没了怎么回事 为什么电脑没有了word
- [BZOJ4379][POI2015]Modernizacja autostrady[树的直径+换根dp]
- Google Summer of Code谷歌编程之夏活动流程全解析(上)
- 【超级简单但超级有用】让PDF书籍变身为可搜索文件
- UEFI 之 UFS Recovery模式下载
- Tomcat的默认管理员密码是什么?
- 网络安全观察物联网协议威胁观察
- 【面试问题总结】zj5
- 基于 React video 视频打点
热门文章
- hdu4370 最短路
- 转:我在淘宝这7年(四)
- 豌豆荚Hack Day:百宝袋负责人访谈
- 解决问题:cannot create symlink in “/etc/docker“: existing file in the way
- excel 导出加水印
- gitea mysql8_初试drone1.0+gitea—docker安装
- 【计算机网络】Socket聊天室程序
- C语言中的类型限定符有哪些?
- 大小口圆锥台计算机,2011年对口单招计算机试卷A04(18页)-原创力文档
- opencv学习之视频帧率