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

相关文章

Python函数作用域示例详解

《Python函数作用域示例详解》本文介绍了Python中的LEGB作用域规则,详细解析了变量查找的四个层级,通过具体代码示例,展示了各层级的变量访问规则和特性,对python函数作用域相关知识感兴趣... 目录一、LEGB 规则二、作用域实例2.1 局部作用域(Local)2.2 闭包作用域(Enclos

Linux中压缩、网络传输与系统监控工具的使用完整指南

《Linux中压缩、网络传输与系统监控工具的使用完整指南》在Linux系统管理中,压缩与传输工具是数据备份和远程协作的桥梁,而系统监控工具则是保障服务器稳定运行的眼睛,下面小编就来和大家详细介绍一下它... 目录引言一、压缩与解压:数据存储与传输的优化核心1. zip/unzip:通用压缩格式的便捷操作2.

MySQL count()聚合函数详解

《MySQLcount()聚合函数详解》MySQL中的COUNT()函数,它是SQL中最常用的聚合函数之一,用于计算表中符合特定条件的行数,本文给大家介绍MySQLcount()聚合函数,感兴趣的朋... 目录核心功能语法形式重要特性与行为如何选择使用哪种形式?总结深入剖析一下 mysql 中的 COUNT

Linux中SSH服务配置的全面指南

《Linux中SSH服务配置的全面指南》作为网络安全工程师,SSH(SecureShell)服务的安全配置是我们日常工作中不可忽视的重要环节,本文将从基础配置到高级安全加固,全面解析SSH服务的各项参... 目录概述基础配置详解端口与监听设置主机密钥配置认证机制强化禁用密码认证禁止root直接登录实现双因素

MySQL 中 ROW_NUMBER() 函数最佳实践

《MySQL中ROW_NUMBER()函数最佳实践》MySQL中ROW_NUMBER()函数,作为窗口函数为每行分配唯一连续序号,区别于RANK()和DENSE_RANK(),特别适合分页、去重... 目录mysql 中 ROW_NUMBER() 函数详解一、基础语法二、核心特点三、典型应用场景1. 数据分

MySQL数据库的内嵌函数和联合查询实例代码

《MySQL数据库的内嵌函数和联合查询实例代码》联合查询是一种将多个查询结果组合在一起的方法,通常使用UNION、UNIONALL、INTERSECT和EXCEPT关键字,下面:本文主要介绍MyS... 目录一.数据库的内嵌函数1.1聚合函数COUNT([DISTINCT] expr)SUM([DISTIN

在Linux终端中统计非二进制文件行数的实现方法

《在Linux终端中统计非二进制文件行数的实现方法》在Linux系统中,有时需要统计非二进制文件(如CSV、TXT文件)的行数,而不希望手动打开文件进行查看,例如,在处理大型日志文件、数据文件时,了解... 目录在linux终端中统计非二进制文件的行数技术背景实现步骤1. 使用wc命令2. 使用grep命令

Python get()函数用法案例详解

《Pythonget()函数用法案例详解》在Python中,get()是字典(dict)类型的内置方法,用于安全地获取字典中指定键对应的值,它的核心作用是避免因访问不存在的键而引发KeyError错... 目录简介基本语法一、用法二、案例:安全访问未知键三、案例:配置参数默认值简介python是一种高级编

python 常见数学公式函数使用详解(最新推荐)

《python常见数学公式函数使用详解(最新推荐)》文章介绍了Python的数学计算工具,涵盖内置函数、math/cmath标准库及numpy/scipy/sympy第三方库,支持从基础算术到复杂数... 目录python 数学公式与函数大全1. 基本数学运算1.1 算术运算1.2 分数与小数2. 数学函数

Linux如何快速检查服务器的硬件配置和性能指标

《Linux如何快速检查服务器的硬件配置和性能指标》在运维和开发工作中,我们经常需要快速检查Linux服务器的硬件配置和性能指标,本文将以CentOS为例,介绍如何通过命令行快速获取这些关键信息,... 目录引言一、查询CPU核心数编程(几C?)1. 使用 nproc(最简单)2. 使用 lscpu(详细信