how to get keyboard key with non blocking in terminal
/**************************************************************************
* 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的更多相关文章
- keyboard键盘demo
main.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:and ...
- SSH key introduction
Preface At the first time, we take the connection with GitLab remote server. You need to type userna ...
- 如何给 GitHub 添加 SSH key, 如何生成 SSH key 详细图文教程!
如何给 GitHub 添加 SSH key, 如何生成 SSH key 详细图文教程! 一. 生成 SSH key https://ide.c9.io/xgqfrms/ 创建一个空项目:(或使用 ...
- windows消息机制详解(转载)
消息,就是指Windows发出的一个通知,告诉应用程序某个事情发生了.例如,单击鼠标.改变窗口尺寸.按下键盘上的一个键都会使Windows发送一个消息给应用程序.消息本身是作为一个记录传递给应用程序的 ...
- OC 类簇与复合
OC 类簇与复合 类簇: 类簇是Foundation框架中广泛使用的设计模式.类簇将一些私有的.具体的子类组合在一个公共的.抽象的超类下面,以这种方法来组织类可以简化一个面向对象框架的公开架构,而又不 ...
- [No00006A]Js的addEventListener()及attachEvent()区别分析【js中的事件监听】
1.添加时间监听: Chrom中: addEventListener的使用方式: target.addEventListener(type, listener, useCapture); target ...
- SikuliLibrary 库关键字注释
在 https://github.com/rainmanwy/robotframework-SikuliLibrary 看到rainmanwy 整理的SikuliLibrary库,非常适合工作需要, ...
- Android自定义键盘
布局文件 <?xml version="1.0" encoding="UTF-8"?> <Keyboard android:keyWidth= ...
- WM (Constants)
Create page WM (Constants) Summary WM_* Constants and their definitions or descriptions and what c ...
随机推荐
- 在centos下解决 “致命错误:curses.h:没有那个文件或目录”
当在centos下编译带有头文件<curses.h> 的程序时,出现以下错误: “致命错误:curses.h:没有那个文件或目录” ,最后在“https://zhidao.baidu.co ...
- Atcoder Yet Another Palindrome Partitioning(状压dp)
Atcoder Yet Another Palindrome Partitioning 思路: 一个字符串满足条件的情况是奇数字母个数小于等于1,也就是异或起来是1<<j(0<=j& ...
- angular5 生命周期钩子函数
生命周期执行顺序ngOnChanges 在有输入属性的情况下才会调用,该方法接受当前和上一属性值的SimpleChanges对象.如果有输入属性,会在ngOnInit之前调用. ngOnInit 在组 ...
- java中的static关键字 学习总结
使用static关键字修饰的变量和方法为静态变量.静态方法. 非静态方法可以访问静态变量/方法和非静态变量/方法,但静态方法只能访问静态变量/方法. 可以看到在静态方法中调用非静态变量和非静态方法时, ...
- Javascript基础一(2)
循环语句 1.for循环 <script> //从1乘到100: //计数器 // var product = 1; // for(var i=1;i<=10;i++){ // pr ...
- AI学习路径
- bzoj2595: [Wc2008]游览计划 斯坦纳树
斯坦纳树是在一个图中选取某些特定点使其联通(可以选取额外的点),要求花费最小,最小生成树是斯坦纳树的一种特殊情况 我们用dp[i][j]来表示以i为根,和j状态是否和i联通,那么有 转移方程: dp[ ...
- 使用axios发送post请求,将JSON数据改为为form类型
我的github(PS:希望star):https://github.com/thWinterSun/v-admin 通常前端通过POST请求向服务器端提交数据格式有4中,分别是"appli ...
- vue input框数字后保留两位小数正则
<el-input type="text" v-model.trim="ruleForm2.marketPrice" maxlength="10 ...
- utime修改文件的存取,修改时间
#include <sys/types.h> #include <utime.h> int utime(const char *filename, const struct u ...