您的位置:首页 > 编程语言 > C语言/C++

C语言键盘按键无阻塞侦测:kbhit()

2013-07-13 23:41 232 查看
http://www.360doc.com/content/12/0414/09/1317564_203474440.shtml


kbhit in c

kbhit in c: kbhit function is used to determine if a key has been pressed or not. To use kbhit function in your program you should include the header file "conio.h". If a key has been pressed then it returns a
non zero value otherwise returns zero.
Declaration : int kbhit();


C programming code for kbhit

#include <stdio.h>
#include <conio.h>
 
main()
{
   while (!kbhit())
      printf("You haven't pressed a key.\n");
 
   return 0;
}


As long as in the above program user doesn't presses a key kbhit() return zero and (!0) i.e. 1 the condition in while loop is true and "You haven't pressed a key." will be printed again and again. As a key is pressed
from the keyboard the condition in while loop become false as now kbhit() will return a non-zero value and ( !(non-zero) = 0), so the control will come out of the while loop.

===========================

ubuntu上没有conio.h如何解决

www.MyException.Cn 发布于:2012-08-01 17:53:40 浏览:142次



本人用的ubuntu12.04版本,gcc是4.6.3版本,要下载什么样的conio.h和库文件?难道只能用ncurses替代吗

------解决方案--------------------------------------------------------

来自百度百科: 

conio 库不仅适用于 Window 平台,在 Linux 下也可使用.网上已经有兼容包,下载后打开就可使用;而至于Mac则完全跟Window没有区别,直接可以使用.

------解决方案--------------------------------------------------------

自己没试过,希望对你有帮助,或者直接自己搜索关键字 "linux 使用 conio.h".
http://zhidao.baidu.com/question/241772898.html http://tech.techweb.com.cn/thread-183749-1-1.html

------解决方案--------------------------------------------------------

用curses.h

------解决方案--------------------------------------------------------

sudo apt-get install libncurses5-dev
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: