如何读取Linux键值,输入子系统,key,dev/input/event,dev/event,C语言键盘【转】
转自:https://blog.csdn.net/lanmanck/article/details/8423669
相信各位使用嵌入式的都希望直接读取键值,特别是芯片厂家已经提供input驱动的情况下,例如GPIO或者扫描类型的键盘。那么在应用层如何通过C语言获取键值呢?
给兄弟们一个重量级的源码,看下面,大家拿去编译运行就知道怎么回事了,当然,可以使用select而不是while()来读取更好一点,留给各位去想象了:
注意:
#include<linux/input.h>
为内核源码的头文件,注意路径,一般为kernel/include/linux/input.h
ev.c:
- /*
- * Copyright 2002 Red Hat Inc., Durham, North Carolina.
- *
- * All Rights Reserved.
- *
- * Permission is hereby granted, free of charge, to any person obtaining
- * a copy of this software and associated documentation files (the
- * "Software"), to deal in the Software without restriction, including
- * without limitation on the rights to use, copy, modify, merge,
- * publish, distribute, sublicense, and/or sell copies of the Software,
- * and to permit persons to whom the Software is furnished to do so,
- * subject to the following conditions:
- *
- * The above copyright notice and this permission notice (including the
- * next paragraph) shall be included in all copies or substantial
- * portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
- * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
- * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
- * NON-INFRINGEMENT. IN NO EVENT SHALL RED HAT AND/OR THEIR SUPPLIERS
- * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
- * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
- * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
- * SOFTWARE.
- *
- * This is a simple test program that reads from /dev/input/event*,
- * decoding events into a human readable form.
- */
- /*
- * Authors:
- * Rickard E. (Rik) Faith <faith@redhat.com>
- *
- */
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <string.h>
- #include <sys/types.h>
- #include <fcntl.h>
- #include <errno.h>
- #include <time.h>
- #include <linux/input.h>
- struct input_event event;
- int main(int argc, char **argv)
- {
- char name[64]; /* RATS: Use ok, but could be better */
- char buf[256] = { 0, }; /* RATS: Use ok */
- unsigned char mask[EV_MAX/8 + 1]; /* RATS: Use ok */
- int version;
- int fd = 0;
- int rc;
- int i, j;
- char *tmp;
- #define test_bit(bit) (mask[(bit)/8] & (1 << ((bit)%8)))
- for (i = 0; i < 32; i++) {
- sprintf(name, "/dev/input/event%d", i);
- if ((fd = open(name, O_RDONLY, 0)) >= 0) {
- ioctl(fd, EVIOCGVERSION, &version);
- ioctl(fd, EVIOCGNAME(sizeof(buf)), buf);
- ioctl(fd, EVIOCGBIT(0, sizeof(mask)), mask);
- printf("%s\n", name);
- printf(" evdev version: %d.%d.%d\n",
- version >> 16, (version >> 8) & 0xff, version & 0xff);
- printf(" name: %s\n", buf);
- printf(" features:");
- for (j = 0; j < EV_MAX; j++) {
- if (test_bit(j)) {
- const char *type = "unknown";
- switch(j) {
- case EV_KEY: type = "keys/buttons"; break;
- case EV_REL: type = "relative"; break;
- case EV_ABS: type = "absolute"; break;
- case EV_MSC: type = "reserved"; break;
- case EV_LED: type = "leds"; break;
- case EV_SND: type = "sound"; break;
- case EV_REP: type = "repeat"; break;
- case EV_FF: type = "feedback"; break;
- }
- printf(" %s", type);
- }
- }
- printf("\n");
- close(fd);
- }
- }
- if (argc > 1) {
- sprintf(name, "/dev/input/event%d", atoi(argv[1]));
- if ((fd = open(name, O_RDWR, 0)) >= 0) {
- printf("%s: open, fd = %d\n", name, fd);
- for (i = 0; i < LED_MAX; i++) {
- event.time.tv_sec = time(0);
- event.time.tv_usec = 0;
- event.type = EV_LED;
- event.code = i;
- event.value = 0;
- write(fd, &event, sizeof(event));
- }
- while ((rc = read(fd, &event, sizeof(event))) > 0) {
- printf("%-24.24s.%06lu type 0x%04x; code 0x%04x;"
- " value 0x%08x; ",
- ctime(&event.time.tv_sec),
- event.time.tv_usec,
- event.type, event.code, event.value);
- switch (event.type) {
- case EV_KEY:
- if (event.code > BTN_MISC) {
- printf("Button %d %s",
- event.code & 0xff,
- event.value ? "press" : "release");
- } else {
- printf("Key %d (0x%x) %s",
- event.code & 0xff,
- event.code & 0xff,
- event.value ? "press" : "release");
- }
- break;
- case EV_REL:
- switch (event.code) {
- case REL_X: tmp = "X"; break;
- case REL_Y: tmp = "Y"; break;
- case REL_HWHEEL: tmp = "HWHEEL"; break;
- case REL_DIAL: tmp = "DIAL"; break;
- case REL_WHEEL: tmp = "WHEEL"; break;
- case REL_MISC: tmp = "MISC"; break;
- default: tmp = "UNKNOWN"; break;
- }
- printf("Relative %s %d", tmp, event.value);
- break;
- case EV_ABS:
- switch (event.code) {
- case ABS_X: tmp = "X"; break;
- case ABS_Y: tmp = "Y"; break;
- case ABS_Z: tmp = "Z"; break;
- case ABS_RX: tmp = "RX"; break;
- case ABS_RY: tmp = "RY"; break;
- case ABS_RZ: tmp = "RZ"; break;
- case ABS_THROTTLE: tmp = "THROTTLE"; break;
- case ABS_RUDDER: tmp = "RUDDER"; break;
- case ABS_WHEEL: tmp = "WHEEL"; break;
- case ABS_GAS: tmp = "GAS"; break;
- case ABS_BRAKE: tmp = "BRAKE"; break;
- case ABS_HAT0X: tmp = "HAT0X"; break;
- case ABS_HAT0Y: tmp = "HAT0Y"; break;
- case ABS_HAT1X: tmp = "HAT1X"; break;
- case ABS_HAT1Y: tmp = "HAT1Y"; break;
- case ABS_HAT2X: tmp = "HAT2X"; break;
- case ABS_HAT2Y: tmp = "HAT2Y"; break;
- case ABS_HAT3X: tmp = "HAT3X"; break;
- case ABS_HAT3Y: tmp = "HAT3Y"; break;
- case ABS_PRESSURE: tmp = "PRESSURE"; break;
- case ABS_DISTANCE: tmp = "DISTANCE"; break;
- case ABS_TILT_X: tmp = "TILT_X"; break;
- case ABS_TILT_Y: tmp = "TILT_Y"; break;
- case ABS_MISC: tmp = "MISC"; break;
- default: tmp = "UNKNOWN"; break;
- }
- printf("Absolute %s %d", tmp, event.value);
- break;
- case EV_MSC: printf("Misc"); break;
- case EV_LED: printf("Led"); break;
- case EV_SND: printf("Snd"); break;
- case EV_REP: printf("Rep"); break;
- case EV_FF: printf("FF"); break;
- break;
- }
- printf("\n");
- }
- printf("rc = %d, (%s)\n", rc, strerror(errno));
- close(fd);
- }
- }
- return 0;
- }
如何读取Linux键值,输入子系统,key,dev/input/event,dev/event,C语言键盘【转】的更多相关文章
- 读取当前键值,并赋值给LED
/********************************* 代码功能:读取当前键值,并赋值给LED 使用函数: digitalRead(数字输入端口号); 创作时间:2016*10*07 作 ...
- JSON创建键值对(key是中文或者数字)方式详解
JSON创建键值对(key是中文或者数字)方式详解 先准备好一个空的json对象 var obj = {}; 1. 最原始的方法 obj.name = 'zhangsan'; //这种方式很简单的添加 ...
- Linux驱动之输入子系统简析
输入子系统由驱动层.输入子系统核心.事件处理层三部分组成.一个输入事件,如鼠标移动.键盘按下等通过Driver->Inputcore->Event handler->userspac ...
- linux驱动模型<输入子系统>
在linux中提供一种输入子系统的驱动模型,其主要是实现在input.c中. 在输入子系统这套模型中,他把驱动分层分类.首先分为上下两层,上层为input.c .下层为驱动的实现,下层分为两部分,一部 ...
- 读取Properties键值对
public class CommonFunc { /** * 取properties文件中的键值对 */ public static String getProperties(String para ...
- Properties/Property文件读取(键值均)乱码问题!
方法一:使用native2ascii进行转码,这个不做说明,客户不可能帮你转码的. 方法二:当键是因为时直接getProperty即可,但加载后的propertis对象里的键也是中文乱码,就无法通过g ...
- Linux输入子系统 转载
NQian 记录成长~ 首页 新随笔 联系 订阅 管理 随笔 - 305 文章 - 0 评论 - 254 12.Linux之输入子系统分析(详解) 在此节之前,我们学的都是简单的字符驱动,涉及 ...
- Android4.0 添加一个新的Android 键值
这里添加新的键值,不是毫无凭据凭空创造的一个键值,而是根据kernel中检测到的按键值,然后转化为Android所需要的数值: 以添加一个Linux键值为217,把它映射为android的键值Brow ...
- Android4.2增加新键值
这里添加新的键值,不是毫无凭据凭空创造的一个键值, 而是根据kernel中检测到的按键值,然后转化为android所需要的数值: 以添加一个linux键值为217,把它映射为android的键值Bro ...
随机推荐
- Surrounded Regions - LeetCode
目录 题目链接 注意点 解法 小结 题目链接 https://leetcode.com/problems/surrounded-regions/ 注意点 边缘不算包围'O' 解法 解法一:dfs.找处 ...
- [动态dp]线段树维护转移矩阵
背景:czy上课讲了新知识,从未见到过,总结一下. 所谓动态dp,是在动态规划的基础上,需要维护一些修改操作的算法. 这类题目分为如下三个步骤:(都是对于常系数齐次递推问题) 1先不考虑修改,不考虑区 ...
- [luogu1655][小朋友的球]
luogu1665 思路 一道第二类斯特兰数的模板题.只不过需要写个高精. f[i][j]表示前i个球放到j个盒子里的方案数.第i个球可以单独一个盒子,所以f[i][j]+=f[i-1][j-1].还 ...
- [luogu1327][生活大爆炸石头剪子布]
题目地址 https://www.luogu.org/problemnew/show/P1328 题目描述 石头剪刀布是常见的猜拳游戏:石头胜剪刀,剪刀胜布,布胜石头.如果两个人出拳一样,则不分胜负. ...
- springcloud的配置文件的读取顺序
SpringBoot默认支持properties和YAML两种格式的配置文件.前者格式简单,但是只支持键值对.如果需要表达列表,最好使用YAML格式.SpringBoot支持自动加载约定名称的配置文件 ...
- 也谈同步异步I/O
也谈同步异步I/O [转自: http://www.smithfox.com/?e=191 ] I/O Model 是一个很大的话题, 也是一个实践性很强的事情, 网上有各种说法和资料, 我们必须用辩 ...
- 类的内置方法__attr__介绍
1.hasattr getaddr setaddr delattr 这四个函数同样也适用于类 class BlackMedium: feture="Ugly" def __in ...
- ElasticSearch 之 dis_max tie_break的应用
1. 插入数据 PUT /my_index/my_type/ { "title": "Quick brown rabbits", "body" ...
- javascript 面向过程和面向对象
面向过程 思维方式:把解决问题的关注点,放到解决问题的每一个详细步骤上面. 面向对象 思维方式:把解决问题的关注点,放到解决问题需要的一些对象身上. 创建对象: 对象字面量 使用内置构造对象 封装简单 ...
- javascript 小清新颜色翻页效果
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...