/**************************************************************************
* 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. Python day17 模块介绍1(time,random)

    module模块和包的介绍(略掉了) 常用模块 # time模块 import time print(time.time())#时间戳,在1970年开始到现在一共多少秒 print(time.gmti ...

  2. android 开发 出错

    Error:Execution failed for task ':app:processDebugResources'. > com.android.ide.common.process.Pr ...

  3. 从celery rabbitmq with docker-compose 引出对容器、依赖注入、TDD的感悟

    用docker配置项目管理系统taiga的时候,不是我一个人遇到这个问题.https://github.com/douglasmiranda/docker-taiga/issues/5 问题描述: 用 ...

  4. Idea设置默认不折叠一行的函数

  5. centos7: iptables保存(配置完nginx的web规则后)

    centos7: iptables保存(配置完nginx的web规则后) 以本地虚拟机为例: 添加规则:入站规则 iptables -I INPUT -p tcp --dport 80 -j ACCE ...

  6. 雷林鹏分享:C# 文件的输入与输出

    C# 文件的输入与输出 一个 文件 是一个存储在磁盘中带有指定名称和目录路径的数据集合.当打开文件进行读写时,它变成一个 流. 从根本上说,流是通过通信路径传递的字节序列.有两个主要的流:输入流 和 ...

  7. windows 命令巧用(持续更新)

    netstat -ano netstat -anvb netstat -s -p [tcp|udp|ip|icmp] # 关闭/打开防火墙 netsh firewall set opmode disa ...

  8. 013 - 关于GC root: Native Stack | MAT分析

      Question:   I have some third library code that I run and after some time I run into OutOfMemoryEr ...

  9. golang martini 源码阅读笔记之martini核心

    继上一篇关于inject注入的笔记,理解了martini的关键核心之一:依赖注入.注入回调函数,由运行时进行主动调用执行.这一篇主要是注解martini的骨架martini.go的实现,下面先从一个简 ...

  10. Android之侧滑菜单DrawerLayout的使用

    在android support.v4 中有一个抽屉视图控件DrawerLayout.使用这个控件,可以生成通过在屏幕上水平滑动打开或者关闭菜单,能给用户一个不错的体验效果. DrawerLayout ...