linux下kbhit()函数 getch函数。

2024-01-23 03:32
文章标签 linux 函数 getch kbhit

本文主要是介绍linux下kbhit()函数 getch函数。,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

怎样在C语言执行循环程序时按键盘任意键停止程序,再按再继续执行?
参考http://zhidao.baidu.com/question/272086855.html

在linux下C语言写了一个while循环,怎么实现按任意键退出。如何编写程序?
参考:http://zhidao.baidu.com/question/461121586.html


对于上面的问题,都用到

linux下的getch函数与kbhit函数。

参考了http://kpld8888.wordpress.com/2007/03/07/linux%E4%B8%8B%E7%9A%84getch%E5%87%BD%E6%95%B0%E4%B8%8Ekbhit%E5%87%BD%E6%95%B0-2/
和:http://zhidao.baidu.com/question/461121586.html

但是我照搬上面两条都不能很好的编译,总是有错误。奉上我的文件:
将下列三个文件放在工程目录下,build project即可。

main.c:
#include "kbhit.h"
#include <stdio.h>
#include <stdlib.h>
#include "accelerometer.h" //contains the middle level macros, state variable// definition and declaration for the//middle layer (class layer) functionsvoid delay(int a){int i,j,delay;for(i = 0 ;  i < 1170*(200/a); i++){delay = 0;for(j = 0; j < 100; j++){delay = delay+1;}}
}int main()
{float x,y,z;       // store x,y,z accelerationacc_t state;int i = 0;int Hz,N;// initialize using ADXL345 at 50 HZif (acc_Init(&state,ADXL345,50,"/dev/input/event3") < 0) {printf("Cannot Init \n");return (-1);}// No Auto Sleep1:if (acc_autosleepSet(&state,NO_AUTOSLEEP) < 0) {printf("Failed to set AutoSleep condition\n");}// No Auto Sleep2:system("echo 0 > /sys/bus/i2c/devices/0-0053/autosleep");//--------------Initialize finished. --------------//printf("input the number of data, Hz: ");scanf("%d,%d", &N, &Hz);while(i < N){if (acc_xyzGet(&state,&x,&y,&z) < 0) {  // get accelerationprintf("Failed to read acceleration values via non blocking read\n");}else {printf("%f,%f,%f\n",x-0.573,y,z); //x correction}delay(Hz);i++;if( kbhit()>0 ){break;}}if (acc_Release(&state) < 0) {printf("Failed to Release Accelerometer Nuggets\n");return (-1);}else{printf("Accelerometer Released! \n");}return 0;
}/* 	if (acc_xyzGet(&state,&x,&y,&z) < 0) {  // get accelerationprintf("Failed to read acceleration values via non blocking read\n");}else {printf("Accelerations:::  X=%f,  Y=%f,  Z=%f\n",x,y,z);}if (acc_xyzEventGet(&state,&x,&y,&z) < 0) {  // get accelerationprintf("Failed to read acceleration values via event get\n");}else {printf("Accelerations:::  X=%f,  Y=%f,  Z=%f\n",x,y,z);}if (acc_xyzEventGet(&state,&x,&y,&z) < 0) {  // get accelerationprintf("Failed to read acceleration values via event get\n");}else {printf("Accelerations:::  X=%f,  Y=%f,  Z=%f\n",x,y,z);}if (acc_freefallEventGet(&state) < 0) {printf("Failed to wait for Freefall \n");}else {printf ("Freefall!!\n");}*/


kbhit.c
/*#include <termios.h>
#include <unistd.h>
#include <assert.h>
#include <string.h>
#include <stdlib.h>
#include <sys/ioctl.h>
#include <sys/time.h>
#include <sys/types.h>
#include "kbhit.h"static struct termios initial_settings, new_settings;
static int peek_character = -1;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 kbhit()
{
unsigned 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;
}int readch()
{
char ch;if(peek_character != -1)
{
ch = peek_character;
peek_character = -1;
return ch;
}
read(0,&ch,1);
return ch;
}
*/#include <termios.h>
#include <unistd.h>
#include <assert.h>
#include <string.h>
#include <stdlib.h>
#include <sys/ioctl.h>
#include <sys/time.h>
#include <sys/types.h>
#include "kbhit.h"#undef TERMIOSECHO
#define TERMIOSFLUSH/** kbhit() — a keyboard lookahead monitor** returns the number of characters available to read*/
int kbhit ( void )
{struct timeval tv;struct termios old_termios, new_termios;int            error;int            count = 0;tcgetattr( 0, &old_termios );new_termios              = old_termios;/** raw mode*/new_termios.c_lflag     &= ~ICANON;/** disable echoing the char as it is typed*/new_termios.c_lflag     &= ~ECHO;/** minimum chars to wait for*/new_termios.c_cc[VMIN]   = 1;/** minimum wait time, 1 * 0.10s*/new_termios.c_cc[VTIME]  = 1;error                    = tcsetattr( 0, TCSANOW, &new_termios );tv.tv_sec                = 0;tv.tv_usec               = 100;/** insert a minimal delay*/select( 1, NULL, NULL, NULL, &tv );error                   += ioctl( 0, FIONREAD, &count );error                   += tcsetattr( 0, TCSANOW, &old_termios );return( error == 0 ? count : -1 );
}  /* end of kbhit */
/*————————————————*/

kbhit.h
#ifndef KBHITh
#define KBHIThvoid init_keyboard(void);
void close_keyboard(void);
int kbhit(void);
int readch(void);#endif


这篇关于linux下kbhit()函数 getch函数。的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/635113

相关文章

防止Linux rm命令误操作的多场景防护方案与实践

《防止Linuxrm命令误操作的多场景防护方案与实践》在Linux系统中,rm命令是删除文件和目录的高效工具,但一旦误操作,如执行rm-rf/或rm-rf/*,极易导致系统数据灾难,本文针对不同场景... 目录引言理解 rm 命令及误操作风险rm 命令基础常见误操作案例防护方案使用 rm编程 别名及安全删除

Linux下MySQL数据库定时备份脚本与Crontab配置教学

《Linux下MySQL数据库定时备份脚本与Crontab配置教学》在生产环境中,数据库是核心资产之一,定期备份数据库可以有效防止意外数据丢失,本文将分享一份MySQL定时备份脚本,并讲解如何通过cr... 目录备份脚本详解脚本功能说明授权与可执行权限使用 Crontab 定时执行编辑 Crontab添加定

C++统计函数执行时间的最佳实践

《C++统计函数执行时间的最佳实践》在软件开发过程中,性能分析是优化程序的重要环节,了解函数的执行时间分布对于识别性能瓶颈至关重要,本文将分享一个C++函数执行时间统计工具,希望对大家有所帮助... 目录前言工具特性核心设计1. 数据结构设计2. 单例模式管理器3. RAII自动计时使用方法基本用法高级用法

使用docker搭建嵌入式Linux开发环境

《使用docker搭建嵌入式Linux开发环境》本文主要介绍了使用docker搭建嵌入式Linux开发环境,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面... 目录1、前言2、安装docker3、编写容器管理脚本4、创建容器1、前言在日常开发全志、rk等不同

GO语言中函数命名返回值的使用

《GO语言中函数命名返回值的使用》在Go语言中,函数可以为其返回值指定名称,这被称为命名返回值或命名返回参数,这种特性可以使代码更清晰,特别是在返回多个值时,感兴趣的可以了解一下... 目录基本语法函数命名返回特点代码示例命名特点基本语法func functionName(parameters) (nam

linux系统上安装JDK8全过程

《linux系统上安装JDK8全过程》文章介绍安装JDK的必要性及Linux下JDK8的安装步骤,包括卸载旧版本、下载解压、配置环境变量等,强调开发需JDK,运行可选JRE,现JDK已集成JRE... 目录为什么要安装jdk?1.查看linux系统是否有自带的jdk:2.下载jdk压缩包2.解压3.配置环境

Linux搭建ftp服务器的步骤

《Linux搭建ftp服务器的步骤》本文给大家分享Linux搭建ftp服务器的步骤,本文通过图文并茂的形式给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录ftp搭建1:下载vsftpd工具2:下载客户端工具3:进入配置文件目录vsftpd.conf配置文件4:

Python Counter 函数使用案例

《PythonCounter函数使用案例》Counter是collections模块中的一个类,专门用于对可迭代对象中的元素进行计数,接下来通过本文给大家介绍PythonCounter函数使用案例... 目录一、Counter函数概述二、基本使用案例(一)列表元素计数(二)字符串字符计数(三)元组计数三、C

Linux实现查看某一端口是否开放

《Linux实现查看某一端口是否开放》文章介绍了三种检查端口6379是否开放的方法:通过lsof查看进程占用,用netstat区分TCP/UDP监听状态,以及用telnet测试远程连接可达性... 目录1、使用lsof 命令来查看端口是否开放2、使用netstat 命令来查看端口是否开放3、使用telnet

Linux系统管理与进程任务管理方式

《Linux系统管理与进程任务管理方式》本文系统讲解Linux管理核心技能,涵盖引导流程、服务控制(Systemd与GRUB2)、进程管理(前台/后台运行、工具使用)、计划任务(at/cron)及常用... 目录引言一、linux系统引导过程与服务控制1.1 系统引导的五个关键阶段1.2 GRUB2的进化优