首页 / LINUX / linux下的键盘检测
linux下的键盘检测
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了linux下的键盘检测,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1783字,纯文字阅读大概需要3分钟。
内容图文
话说,仅仅是一个键盘检测就好麻烦。我还是懂的太少了。。。
#include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <errno.h> #include <fcntl.h> #include <stdio.h> #include <termios.h> #include <unistd.h> #include <string.h> #define key_ESC 27 void init_keyboard(); void close_keyboard(); int kbhit(); int readch(); /* 相关函数声明 */ static struct termios initial_settings, new_settings; static int peek_character = -1; /* 用于测试一个按键是否被按下 */ /* 检测键盘按键的函数 */ int kbhit() { char ch; int nread; if ( peek_character != -1 ) return(1); new_settings.c_cc[VMIN] = 0; tcsetattr( 0, TCSANOW, &new_settings ); nread = read( 0, &ch, 1 ); new_settings.c_cc[VMIN] = 1; tcsetattr( 0, TCSANOW, &new_settings ); if ( nread == 1 ) { peek_character = ch; return(1); } return(0); } /* 用来接收按下的按键,并peek_character = -1恢复状态 */ int readch() { char ch; if ( peek_character != -1 ) { ch = peek_character; peek_character = -1; return(ch); } read( 0, &ch, 1 ); return(ch); } /* 配置终端函数 */ void init_keyboard() { tcgetattr( 0, &initial_settings ); new_settings = initial_settings; new_settings.c_lflag &= ~ICANON; new_settings.c_lflag &= ~ECHO; new_settings.c_lflag &= ~ISIG; new_settings.c_cc[VMIN] = 1; new_settings.c_cc[VTIME] = 0; tcsetattr( 0, TCSANOW, &new_settings ); } void close_keyboard() { tcsetattr( 0, TCSANOW, &initial_settings ); } int main(int argc, char const *argv[]) { int ch = 0; init_keyboard(); printf( "You can put ESC to quit!\n" ); while ( ch != 27 ) { if ( kbhit() ) { ch = readch(); if ( ch != 27 ) printf( "You put %c ! Only put ESC can quit! \n", ch ); } } close_keyboard(); return 0; }
参考:
-
http://blog.163.com/liang_w_yan/blog/static/210657088201332444016981/
原文:http://my.oschina.net/itfanr/blog/365285
内容总结
以上是互联网集市为您收集整理的linux下的键盘检测全部内容,希望文章能够帮你解决linux下的键盘检测所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。
来源:【匿名】