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内核定时器使用及说明

《Linux内核定时器使用及说明》文章详细介绍了Linux内核定时器的特性、核心数据结构、时间相关转换函数以及操作API,通过示例展示了如何编写和使用定时器,包括按键消抖的应用... 目录1.linux内核定时器特征2.Linux内核定时器核心数据结构3.Linux内核时间相关转换函数4.Linux内核定时

Linux镜像文件制作方式

《Linux镜像文件制作方式》本文介绍了Linux镜像文件制作的过程,包括确定磁盘空间布局、制作空白镜像文件、分区与格式化、复制引导分区和其他分区... 目录1.确定磁盘空间布局2.制作空白镜像文件3.分区与格式化1) 分区2) 格式化4.复制引导分区5.复制其它分区1) 挂载2) 复制bootfs分区3)

pandas使用apply函数给表格同时添加多列

《pandas使用apply函数给表格同时添加多列》本文介绍了利用Pandas的apply函数在DataFrame中同时添加多列,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习... 目录一、Pandas使用apply函数给表格同时添加多列二、应用示例一、Pandas使用apply函

Linux服务器数据盘移除并重新挂载的全过程

《Linux服务器数据盘移除并重新挂载的全过程》:本文主要介绍在Linux服务器上移除并重新挂载数据盘的整个过程,分为三大步:卸载文件系统、分离磁盘和重新挂载,每一步都有详细的步骤和注意事项,确保... 目录引言第一步:卸载文件系统第二步:分离磁盘第三步:重新挂载引言在 linux 服务器上移除并重新挂p

Python中Namespace()函数详解

《Python中Namespace()函数详解》Namespace是argparse模块提供的一个类,用于创建命名空间对象,它允许通过点操作符访问数据,比字典更易读,在深度学习项目中常用于加载配置、命... 目录1. 为什么使用 Namespace?2. Namespace 的本质是什么?3. Namesp

Linux下屏幕亮度的调节方式

《Linux下屏幕亮度的调节方式》文章介绍了Linux下屏幕亮度调节的几种方法,包括图形界面、手动调节(使用ACPI内核模块)和外接显示屏调节,以及自动调节软件(CaliseRedshift和Reds... 目录1 概述2 手动调节http://www.chinasem.cn2.1 手动屏幕调节2.2 外接显

Linux(centos7)虚拟机没有IP问题及解决方案

《Linux(centos7)虚拟机没有IP问题及解决方案》文章介绍了在CentOS7中配置虚拟机网络并使用Xshell连接虚拟机的步骤,首先,检查并配置网卡ens33的ONBOOT属性为yes,然后... 目录输入查看ZFhrxIP命令:ip addr查看,没有虚拟机IP修改ens33配置文件重启网络Xh

linux实现对.jar文件的配置文件进行修改

《linux实现对.jar文件的配置文件进行修改》文章讲述了如何使用Linux系统修改.jar文件的配置文件,包括进入文件夹、编辑文件、保存并退出编辑器,以及重新启动项目... 目录linux对.jar文件的配置文件进行修改第一步第二步 第三步第四步总结linux对.jar文件的配置文件进行修改第一步进

MySQL中如何求平均值常见实例(AVG函数详解)

《MySQL中如何求平均值常见实例(AVG函数详解)》MySQLavg()是一个聚合函数,用于返回各种记录中表达式的平均值,:本文主要介绍MySQL中用AVG函数如何求平均值的相关资料,文中通过代... 目录前言一、基本语法二、示例讲解1. 计算全表平均分2. 计算某门课程的平均分(例如:Math)三、结合

linux ssh如何实现增加访问端口

《linuxssh如何实现增加访问端口》Linux中SSH默认使用22端口,为了增强安全性或满足特定需求,可以通过修改SSH配置来增加或更改SSH访问端口,具体步骤包括修改SSH配置文件、增加或修改... 目录1. 修改 SSH 配置文件2. 增加或修改端口3. 保存并退出编辑器4. 更新防火墙规则使用uf