/**************************************************************************
* 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. jekins,报错 stderr: Could not create directory '/usr/share/tomcat7/.ssh'. Failed to add the host to the list of

    public key是在~/.ssh/id_rsa.pub,而private key是~/.ssh/id_rsa 设置的时候,Jenkins需要的是private key

  2. Jmeter ResponseAssertion 【Ignore Status】

    在Jmeter源码中AssertionGui.java中,定义了Ignore Status的作用域 /** * Checkbox to indicate whether the response sh ...

  3. Spring AOP之使用注解创建切面

    上节中我们已经定义了Performance接口,他是切面中的切点的一个目标对象.那么现在就让我们使用AspectJ注解来定义切面吧. 1.定义切面 下面我们就来定义一场舞台剧中观众的切面类Audien ...

  4. selenium-webdriver 简单教程

    ruby环境下selenium/webdriver可以通过selenium-webdriver.gem包进行安装 gem install selenium-webdriver   支持语言及版本有ru ...

  5. angular5 生命周期钩子函数

    生命周期执行顺序ngOnChanges 在有输入属性的情况下才会调用,该方法接受当前和上一属性值的SimpleChanges对象.如果有输入属性,会在ngOnInit之前调用. ngOnInit 在组 ...

  6. 原生js实现倒计时

    html代码: <div class="box">距离下班还有:<span>01:01:30</span></div> css代码: ...

  7. Nginx配置https, 80端口重定向443

    server { listen 443 ssl; server_name 域名; charset utf-8; access_log /var/log/nginx/webhook.iminho.me/ ...

  8. kmp练习

    kmp板子如下, 失配数组不优化的话, $f_i$就表示子串[0...i]前后缀最大匹配长度 int main() { scanf("%s%s", t, p); int n = s ...

  9. python-day47--mysql数据备份与恢复

    一.IDE工具介绍 掌握: #1. 测试+链接数据库 #2. 新建库 #3. 新建表,新增字段+类型+约束 #4. 设计表:外键 #5. 新建查询 #6. 备份库/表 #注意: 批量加注释:ctrl+ ...

  10. arc路径-磊哥

    不然直接设置80 90要转换成弧度比如Math.PI代表180度你就要 80*Math.PI/180190*Math.PI/180<!DOCTYPE html><html>&l ...