linux下读取键盘扫描码
来源:互联网 发布:网络舆情分析师 培训 编辑:程序博客网 时间:2024/05/16 18:24
键盘扫描码有两种:
一个是make code,也就是键被按下和按住不放时产生
另一种是break code,在键被释放时产生。
每个键都有自己唯一的make code和break code。
提供一个我在Linux下的实现,就是使用ioctl 改变终端I/O模式。
测试程序在“a”健被按下时退出。
#include <stdio.h>
#include <stdlib.h>
#include <termios.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <linux/kd.h>
int main(void)
{
struct termios oldtermios,newtermios;
int oldmode;
unsigned short key;
int i;
if((tcgetattr(fileno(stdin),&oldtermios))<0)
{
perror("tcgetaddr error");
exit(1);
}
if((tcgetattr(fileno(stdin),&newtermios))<0)
{
perror("tcgetaddr error");
exit(1);
}
newtermios.c_lflag &= ~(ICANON|ECHO|ISIG);
newtermios.c_iflag = 0;
newtermios.c_cc[VMIN] = 0;
newtermios.c_cc[VTIME] = 1; //=0延时0 ,=1延时1s
if(tcsetattr(fileno(stdin),TCSAFLUSH,&newtermios))
{
perror("tcsetattr error");
exit(1);
}
ioctl(fileno(stdin),KDGKBMODE,&oldmode);
if(ioctl(fileno(stdin),KDSKBMODE,K_RAW))
{
perror("ioctl error");
exit(1);
}
while(1)
{
if(read(fileno(stdin),&key,sizeof(key))>0)
printf(" key = 0x%x /n",key);
if (key == 0x1e)//key a down , exit.
break;
key = 0;
}
ioctl(fileno(stdin),KDSKBMODE,oldmode);
tcsetattr(fileno(stdin),TCSANOW,&oldtermios);
return 0;
}
一个是make code,也就是键被按下和按住不放时产生
另一种是break code,在键被释放时产生。
每个键都有自己唯一的make code和break code。
提供一个我在Linux下的实现,就是使用ioctl 改变终端I/O模式。
测试程序在“a”健被按下时退出。
#include <stdio.h>
#include <stdlib.h>
#include <termios.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <linux/kd.h>
int main(void)
{
struct termios oldtermios,newtermios;
int oldmode;
unsigned short key;
int i;
if((tcgetattr(fileno(stdin),&oldtermios))<0)
{
perror("tcgetaddr error");
exit(1);
}
if((tcgetattr(fileno(stdin),&newtermios))<0)
{
perror("tcgetaddr error");
exit(1);
}
newtermios.c_lflag &= ~(ICANON|ECHO|ISIG);
newtermios.c_iflag = 0;
newtermios.c_cc[VMIN] = 0;
newtermios.c_cc[VTIME] = 1; //=0延时0 ,=1延时1s
if(tcsetattr(fileno(stdin),TCSAFLUSH,&newtermios))
{
perror("tcsetattr error");
exit(1);
}
ioctl(fileno(stdin),KDGKBMODE,&oldmode);
if(ioctl(fileno(stdin),KDSKBMODE,K_RAW))
{
perror("ioctl error");
exit(1);
}
while(1)
{
if(read(fileno(stdin),&key,sizeof(key))>0)
printf(" key = 0x%x /n",key);
if (key == 0x1e)//key a down , exit.
break;
key = 0;
}
ioctl(fileno(stdin),KDSKBMODE,oldmode);
tcsetattr(fileno(stdin),TCSANOW,&oldtermios);
return 0;
}
- linux下读取键盘扫描码
- 键盘扫描码
- 键盘扫描码大全
- 键盘扫描码大全
- 键盘扫描码大全
- 键盘的扫描码
- 键盘扫描码
- 键盘扫描码
- 键盘扫描码大全
- 键盘扫描码
- 键盘扫描码(全)
- 键盘扫描码
- 键盘扫描码
- 键盘扫描码查询
- 键盘扫描码
- 键盘扫描码
- 键盘扫描码
- 键盘扫描码
- 用Python开发GUI程序
- What is the difference between User Control and Custom Control?
- 用vs2005实现asp.net2.0移动开发
- DelphiSpeedUp IDE plugin
- 最新...
- linux下读取键盘扫描码
- 菜单树以及源文件
- Tomcat 中 jsp 中文乱码显示处理解决方案
- 浅谈C#中的枚举
- 一个比较综合的Java语言基础试题
- 海明码(汉明码,汉明距离)
- 什么是枚举,我来教教你
- 全球卫片超级下载[GoogleMapSuperGet]
- 在后台[复制代码]功能键的js实现