/**************************************************************************
* 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. Codeforces 834C - The Meaningless Game

    834C - The Meaningless Game 数学. 思路1:判断a•b能不能化成v3且a%v==0且b%v==0.v可以直接用pow求(或者用cbrt),也可以二分求:还可以用map映射预 ...

  2. English trip -- Iris老师整理的一般时态

    一般疑问句: 用Yes/No 就能回答的问句. a.g Are you an office worker? 问句 <                                        ...

  3. 20170405xlVBA快速录入

    Dim Rng As Range Dim Arr As Variant Dim LastCell As Range Dim FindText As String Dim ItemCount As Lo ...

  4. Jzzhu and Numbers CodeForces - 449D (高维前缀和,容斥)

    大意: 给定集合a, 求a的按位与和等于0的非空子集数. 首先由容斥可以得到 $ans = \sum \limits_{0\le x <2^{20}} (-1)^{\alpha} f_x$, 其 ...

  5. hdu-1849-nim模板

    Rabbit and Grass Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  6. UVA-11882 Biggest Number (DFS+剪枝)

    题目大意:给出一个方格矩阵,矩阵中有数字0~9,任选一个格子为起点,将走过的数字连起来构成一个数,找出最大的那个数,每个格子只能走一次. 题目分析:DFS.剪枝方案:在当前的处境下,找出所有还能到达的 ...

  7. POJ-3087 Shuffle'm Up (模拟)

    Description A common pastime for poker players at a poker table is to shuffle stacks of chips. Shuff ...

  8. RabbitMQ特性

    使用默认的exchange channel.basicPublish("", QUEUE_NAME, null, message.getBytes()); 如果用空字符串去申明一个 ...

  9. ASCII码表(0-127 ) C中的转义字符

    所有的ASCII码都可以用“\”加数字(一般是8进制数字)来表示.而C中定义了一些字母前加"\"来表示常见的那些不能显示的ASCII字符,如\0,\t,\n等,就称为转义字符,因为 ...

  10. 如何获取显示器的EDID信息

    Q1: 为什么要写这篇文章? A1:在最近的工作中遇到了不少问题,其中很多都是和EDID相关的.可以说,作为一家以“显示”为生的企业,我们时时刻刻在与EDID打交道.EDID这东西很简单,但是如果不了 ...