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

相关文章

PostgreSQL中rank()窗口函数实用指南与示例

《PostgreSQL中rank()窗口函数实用指南与示例》在数据分析和数据库管理中,经常需要对数据进行排名操作,PostgreSQL提供了强大的窗口函数rank(),可以方便地对结果集中的行进行排名... 目录一、rank()函数简介二、基础示例:部门内员工薪资排名示例数据排名查询三、高级应用示例1. 每

Linux线程之线程的创建、属性、回收、退出、取消方式

《Linux线程之线程的创建、属性、回收、退出、取消方式》文章总结了线程管理核心知识:线程号唯一、创建方式、属性设置(如分离状态与栈大小)、回收机制(join/detach)、退出方法(返回/pthr... 目录1. 线程号2. 线程的创建3. 线程属性4. 线程的回收5. 线程的退出6. 线程的取消7.

Linux下进程的CPU配置与线程绑定过程

《Linux下进程的CPU配置与线程绑定过程》本文介绍Linux系统中基于进程和线程的CPU配置方法,通过taskset命令和pthread库调整亲和力,将进程/线程绑定到特定CPU核心以优化资源分配... 目录1 基于进程的CPU配置1.1 对CPU亲和力的配置1.2 绑定进程到指定CPU核上运行2 基于

全面掌握 SQL 中的 DATEDIFF函数及用法最佳实践

《全面掌握SQL中的DATEDIFF函数及用法最佳实践》本文解析DATEDIFF在不同数据库中的差异,强调其边界计算原理,探讨应用场景及陷阱,推荐根据需求选择TIMESTAMPDIFF或inte... 目录1. 核心概念:DATEDIFF 究竟在计算什么?2. 主流数据库中的 DATEDIFF 实现2.1

golang程序打包成脚本部署到Linux系统方式

《golang程序打包成脚本部署到Linux系统方式》Golang程序通过本地编译(设置GOOS为linux生成无后缀二进制文件),上传至Linux服务器后赋权执行,使用nohup命令实现后台运行,完... 目录本地编译golang程序上传Golang二进制文件到linux服务器总结本地编译Golang程序

Linux下删除乱码文件和目录的实现方式

《Linux下删除乱码文件和目录的实现方式》:本文主要介绍Linux下删除乱码文件和目录的实现方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录linux下删除乱码文件和目录方法1方法2总结Linux下删除乱码文件和目录方法1使用ls -i命令找到文件或目录

MySQL中的LENGTH()函数用法详解与实例分析

《MySQL中的LENGTH()函数用法详解与实例分析》MySQLLENGTH()函数用于计算字符串的字节长度,区别于CHAR_LENGTH()的字符长度,适用于多字节字符集(如UTF-8)的数据验证... 目录1. LENGTH()函数的基本语法2. LENGTH()函数的返回值2.1 示例1:计算字符串

Linux在线解压jar包的实现方式

《Linux在线解压jar包的实现方式》:本文主要介绍Linux在线解压jar包的实现方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录linux在线解压jar包解压 jar包的步骤总结Linux在线解压jar包在 Centos 中解压 jar 包可以使用 u

linux解压缩 xxx.jar文件进行内部操作过程

《linux解压缩xxx.jar文件进行内部操作过程》:本文主要介绍linux解压缩xxx.jar文件进行内部操作,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、解压文件二、压缩文件总结一、解压文件1、把 xxx.jar 文件放在服务器上,并进入当前目录#

Linux系统性能检测命令详解

《Linux系统性能检测命令详解》本文介绍了Linux系统常用的监控命令(如top、vmstat、iostat、htop等)及其参数功能,涵盖进程状态、内存使用、磁盘I/O、系统负载等多维度资源监控,... 目录toppsuptimevmstatIOStatiotopslabtophtopdstatnmon