
所以要想读取快捷键,那么就要从系统层面读取
而不是用库函数
在Windows下可以用key hook
在linux下可以读取input event
linux的stdio不支持监听键盘的按键,你需要包含扩展的头文件和库。
比如包含termios.h,这个头文件里有一些终端IO的相关扩展。我们使用tcsetattr来设置一个属性,就可以不用回车就即时监听到按键。
#include <termios.h>//....其它代码
int in
struct termios new_settings
struct termios stored_settings
tcgetattr(0,&stored_settings) /* 把当前设置保存起来,以供恢复 */
new_settings = stored_settings
new_settings.c_lflag &= (~ICANON) /* 新设置,将canonical模式取消,屏蔽整行缓存 */
new_settings.c_cc[VTIME] = 0 /* 超时设置为0 */
new_settings.c_cc[VMIN] = 1 /* 读取的最小字符数 */
tcsetattr(0,TCSANOW,&new_settings)
in = getchar() /* 此时就可以正常的使用getchar()函数来获取一个输入了 */
/* 此时,你可以使用while,并将获得的输入进行处理 */
tcsetattr(0,TCSANOW,&stored_settings) /* 恢复原设置 */
另外,你可以考虑使用linux/input.h,使用事件监控的方式,来监控键盘事件。这个功能就强大多了,但是,这个 *** 作只适用于本机,也就是说,这个事件,只能监听到与主机直接相连的键盘和鼠标的事件。如果使用ssh登录过去的,是监听不到。关于具体用法,自己搜一下就好。
第一种方法数组内容自定义 #include <stdio.h> main() { int a[10],n,i scanf("%d",&n) for(i=0i<10i++)scanf("%d",&a[i]) for(i=0i<10i++)if(n==a[i])break if(i==10)printf("no found")else printf("%d",i) system("PAUSE") } 第二种方法数组内容已定义 #include <stdio.h> main() { int a[10]={0,1,2,3,4,5,6,7,8,9},n,i scanf("%d",&n) for(i=0i<10i++)if(n==a[i])break if(i==10)printf("no found")else printf("%d",i) system("PAUSE") }欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)