/**************************************************************************
* how to get keyboard key with non blocking in terminal
* 声明:
* 如何在终端下以非堵塞的方式获取按键的键值,这个想法最初是因为
* 在单线程下实现多任务,同时不因键盘输入而堵塞,核心内容来自网络,
* 但已经忘了出处。
*
* 2015-7-5 晴 深圳 南山平山村 曾剑锋
*************************************************************************/ \\\\\\-*- 目录 -*-/////
| 一、cat kbhit.h
| 二、cat kbhit.c
\\\\\\\\\\\\/////////// 一、cat kbhit.h
#ifndef __KBHIT_H__
#define __KBHIT_H__ #include <stdio.h>
#include <termios.h>
/**
* 初始化键盘操作
*/
void init_keyboard(void);
/**
* 关闭键盘操作
*/
void close_keyboard(void);
/**
* 判断是否有按键按下,如果有案件按下返回1,没有按键按下
* 则返回0
*/
int kbhit(void);
/**
* 用于在有案件按下之后,读取字符
*/
int readch(void); #endif // __KBHIT_H__ 二、cat kbhit.c
#include "kbhit.h" static struct termios initial_settings, new_settings;
static int peek_character = -; void init_keyboard()
{
tcgetattr(,&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] = ;
new_settings.c_cc[VTIME] = ;
tcsetattr(, TCSANOW, &new_settings);
} void close_keyboard()
{
tcsetattr(, TCSANOW, &initial_settings);
} int kbhit()
{
unsigned char ch;
int nread; if (peek_character != -) return ;
new_settings.c_cc[VMIN]=;
tcsetattr(, TCSANOW, &new_settings);
nread = read(,&ch,);
new_settings.c_cc[VMIN]=;
tcsetattr(, TCSANOW, &new_settings);
if(nread == )
{
peek_character = ch;
return ;
}
return ;
} int readch()
{
char ch; if(peek_character != -)
{
ch = peek_character;
peek_character = -;
return ch;
}
read(,&ch,);
return ch;
}

how to get keyboard key with non blocking in terminal的更多相关文章

  1. keyboard键盘demo

    main.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:and ...

  2. SSH key introduction

    Preface At the first time, we take the connection with GitLab remote server. You need to type userna ...

  3. 如何给 GitHub 添加 SSH key, 如何生成 SSH key 详细图文教程!

    如何给 GitHub 添加  SSH key, 如何生成  SSH key 详细图文教程! 一. 生成  SSH key https://ide.c9.io/xgqfrms/ 创建一个空项目:(或使用 ...

  4. windows消息机制详解(转载)

    消息,就是指Windows发出的一个通知,告诉应用程序某个事情发生了.例如,单击鼠标.改变窗口尺寸.按下键盘上的一个键都会使Windows发送一个消息给应用程序.消息本身是作为一个记录传递给应用程序的 ...

  5. OC 类簇与复合

    OC 类簇与复合 类簇: 类簇是Foundation框架中广泛使用的设计模式.类簇将一些私有的.具体的子类组合在一个公共的.抽象的超类下面,以这种方法来组织类可以简化一个面向对象框架的公开架构,而又不 ...

  6. [No00006A]Js的addEventListener()及attachEvent()区别分析【js中的事件监听】

    1.添加时间监听: Chrom中: addEventListener的使用方式: target.addEventListener(type, listener, useCapture); target ...

  7. SikuliLibrary 库关键字注释

    在  https://github.com/rainmanwy/robotframework-SikuliLibrary 看到rainmanwy 整理的SikuliLibrary库,非常适合工作需要, ...

  8. Android自定义键盘

    布局文件 <?xml version="1.0" encoding="UTF-8"?> <Keyboard android:keyWidth= ...

  9. WM (Constants)

    Create page WM (Constants)   Summary WM_* Constants and their definitions or descriptions and what c ...

随机推荐

  1. [库][c++]tinyxml2使用小结

    参考:http://blog.csdn.net/educast/article/details/12908455 1.配置TinyXML2 去这里把项目弄下来,然后解压,我们之需要里面的tinyxml ...

  2. Git工作区、暂存区和版本库

    基本概念 我们先来理解下Git 工作区.暂存区和版本库概念 工作区:就是你在电脑里能看到的目录. 暂存区:英文叫stage, 或index.一般存放在 ".git目录下" 下的in ...

  3. Howto: 使用ImageBrush替换PictureMarkerSymbol以加强graphic显示性能

      软件: ArcGIS API for Microsoft Silverlight/WPF 9.3.1 操作系统: N/A 摘要:          ArcGIS API for Microsoft ...

  4. codeforces 1042d//Petya and Array// Codeforces Round #510 (Div. 2)

    题意:给出一个数组,求其中和小于t的区间数. 先计算前缀和数组sum[i].对当前的sum[i],查询树状数组中有几个比(sum[i]-t)大的数,那么用sum[i]减它就是一个合法区间.再将当前的s ...

  5. linux中whereis、which、find、location的区别和用法

    摘自:https://www.cnblogs.com/kex1n/p/5233821.html 1. find find是最常见和最强大的查找命令,你可以用它找到任何你想找的文件. find的使用格式 ...

  6. consumer的DubboClientHandler线程池

    1. 创建线程池 创建线程池的调用栈如下: SimpleDataStore把线程池存放在map中. public class NettyClient extends AbstractClient { ...

  7. 069——VUE中vuex之使用getters高效获取购物车商品总价

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  8. SQL Server 调优系列基础篇 - 联合运算符总

    前言 上两篇文章我们介绍了查看查询计划的方式,以及一些常用的连接运算符的优化技巧,本篇我们总结联合运算符的使用方式和优化技巧. 废话少说,直接进入本篇的主题. 技术准备 基于SQL Server200 ...

  9. zoj3888

    题解: 维护比这个大的第二大 代码: #include<cstdio> #include<algorithm> #include<queue> #include&l ...

  10. System.out.println(i++); System.out.println(++i);的区别

    之前一直对i++和++i很模糊,这次通过两个小demo来探究下. 例1: public static void main(String[] args) { int i=2; System.out.pr ...