转自:https://blog.csdn.net/lanmanck/article/details/8423669

相信各位使用嵌入式的都希望直接读取键值,特别是芯片厂家已经提供input驱动的情况下,例如GPIO或者扫描类型的键盘。那么在应用层如何通过C语言获取键值呢?

给兄弟们一个重量级的源码,看下面,大家拿去编译运行就知道怎么回事了,当然,可以使用select而不是while()来读取更好一点,留给各位去想象了:

注意:

#include<linux/input.h>

为内核源码的头文件,注意路径,一般为kernel/include/linux/input.h

ev.c:

  1. /*
  2. * Copyright 2002 Red Hat Inc., Durham, North Carolina.
  3. *
  4. * All Rights Reserved.
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining
  7. * a copy of this software and associated documentation files (the
  8. * "Software"), to deal in the Software without restriction, including
  9. * without limitation on the rights to use, copy, modify, merge,
  10. * publish, distribute, sublicense, and/or sell copies of the Software,
  11. * and to permit persons to whom the Software is furnished to do so,
  12. * subject to the following conditions:
  13. *
  14. * The above copyright notice and this permission notice (including the
  15. * next paragraph) shall be included in all copies or substantial
  16. * portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  19. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  20. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  21. * NON-INFRINGEMENT. IN NO EVENT SHALL RED HAT AND/OR THEIR SUPPLIERS
  22. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  23. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  24. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  25. * SOFTWARE.
  26. *
  27. * This is a simple test program that reads from /dev/input/event*,
  28. * decoding events into a human readable form.
  29. */
  30.  
  31. /*
  32. * Authors:
  33. * Rickard E. (Rik) Faith <faith@redhat.com>
  34. *
  35. */
  36.  
  37. #include <stdio.h>
  38. #include <stdlib.h>
  39. #include <unistd.h>
  40. #include <string.h>
  41. #include <sys/types.h>
  42. #include <fcntl.h>
  43. #include <errno.h>
  44. #include <time.h>
  45. #include <linux/input.h>
  46.  
  47. struct input_event event;
  48.  
  49. int main(int argc, char **argv)
  50. {
  51. char name[64]; /* RATS: Use ok, but could be better */
  52. char buf[256] = { 0, }; /* RATS: Use ok */
  53. unsigned char mask[EV_MAX/8 + 1]; /* RATS: Use ok */
  54. int version;
  55. int fd = 0;
  56. int rc;
  57. int i, j;
  58. char *tmp;
  59.  
  60. #define test_bit(bit) (mask[(bit)/8] & (1 << ((bit)%8)))
  61.  
  62. for (i = 0; i < 32; i++) {
  63. sprintf(name, "/dev/input/event%d", i);
  64. if ((fd = open(name, O_RDONLY, 0)) >= 0) {
  65. ioctl(fd, EVIOCGVERSION, &version);
  66. ioctl(fd, EVIOCGNAME(sizeof(buf)), buf);
  67. ioctl(fd, EVIOCGBIT(0, sizeof(mask)), mask);
  68. printf("%s\n", name);
  69. printf(" evdev version: %d.%d.%d\n",
  70. version >> 16, (version >> 8) & 0xff, version & 0xff);
  71. printf(" name: %s\n", buf);
  72. printf(" features:");
  73. for (j = 0; j < EV_MAX; j++) {
  74. if (test_bit(j)) {
  75. const char *type = "unknown";
  76. switch(j) {
  77. case EV_KEY: type = "keys/buttons"; break;
  78. case EV_REL: type = "relative"; break;
  79. case EV_ABS: type = "absolute"; break;
  80. case EV_MSC: type = "reserved"; break;
  81. case EV_LED: type = "leds"; break;
  82. case EV_SND: type = "sound"; break;
  83. case EV_REP: type = "repeat"; break;
  84. case EV_FF: type = "feedback"; break;
  85. }
  86. printf(" %s", type);
  87. }
  88. }
  89. printf("\n");
  90. close(fd);
  91. }
  92. }
  93.  
  94. if (argc > 1) {
  95. sprintf(name, "/dev/input/event%d", atoi(argv[1]));
  96. if ((fd = open(name, O_RDWR, 0)) >= 0) {
  97. printf("%s: open, fd = %d\n", name, fd);
  98. for (i = 0; i < LED_MAX; i++) {
  99. event.time.tv_sec = time(0);
  100. event.time.tv_usec = 0;
  101. event.type = EV_LED;
  102. event.code = i;
  103. event.value = 0;
  104. write(fd, &event, sizeof(event));
  105. }
  106.  
  107. while ((rc = read(fd, &event, sizeof(event))) > 0) {
  108. printf("%-24.24s.%06lu type 0x%04x; code 0x%04x;"
  109. " value 0x%08x; ",
  110. ctime(&event.time.tv_sec),
  111. event.time.tv_usec,
  112. event.type, event.code, event.value);
  113. switch (event.type) {
  114. case EV_KEY:
  115. if (event.code > BTN_MISC) {
  116. printf("Button %d %s",
  117. event.code & 0xff,
  118. event.value ? "press" : "release");
  119. } else {
  120. printf("Key %d (0x%x) %s",
  121. event.code & 0xff,
  122. event.code & 0xff,
  123. event.value ? "press" : "release");
  124. }
  125. break;
  126. case EV_REL:
  127. switch (event.code) {
  128. case REL_X: tmp = "X"; break;
  129. case REL_Y: tmp = "Y"; break;
  130. case REL_HWHEEL: tmp = "HWHEEL"; break;
  131. case REL_DIAL: tmp = "DIAL"; break;
  132. case REL_WHEEL: tmp = "WHEEL"; break;
  133. case REL_MISC: tmp = "MISC"; break;
  134. default: tmp = "UNKNOWN"; break;
  135. }
  136. printf("Relative %s %d", tmp, event.value);
  137. break;
  138. case EV_ABS:
  139. switch (event.code) {
  140. case ABS_X: tmp = "X"; break;
  141. case ABS_Y: tmp = "Y"; break;
  142. case ABS_Z: tmp = "Z"; break;
  143. case ABS_RX: tmp = "RX"; break;
  144. case ABS_RY: tmp = "RY"; break;
  145. case ABS_RZ: tmp = "RZ"; break;
  146. case ABS_THROTTLE: tmp = "THROTTLE"; break;
  147. case ABS_RUDDER: tmp = "RUDDER"; break;
  148. case ABS_WHEEL: tmp = "WHEEL"; break;
  149. case ABS_GAS: tmp = "GAS"; break;
  150. case ABS_BRAKE: tmp = "BRAKE"; break;
  151. case ABS_HAT0X: tmp = "HAT0X"; break;
  152. case ABS_HAT0Y: tmp = "HAT0Y"; break;
  153. case ABS_HAT1X: tmp = "HAT1X"; break;
  154. case ABS_HAT1Y: tmp = "HAT1Y"; break;
  155. case ABS_HAT2X: tmp = "HAT2X"; break;
  156. case ABS_HAT2Y: tmp = "HAT2Y"; break;
  157. case ABS_HAT3X: tmp = "HAT3X"; break;
  158. case ABS_HAT3Y: tmp = "HAT3Y"; break;
  159. case ABS_PRESSURE: tmp = "PRESSURE"; break;
  160. case ABS_DISTANCE: tmp = "DISTANCE"; break;
  161. case ABS_TILT_X: tmp = "TILT_X"; break;
  162. case ABS_TILT_Y: tmp = "TILT_Y"; break;
  163. case ABS_MISC: tmp = "MISC"; break;
  164. default: tmp = "UNKNOWN"; break;
  165. }
  166. printf("Absolute %s %d", tmp, event.value);
  167. break;
  168. case EV_MSC: printf("Misc"); break;
  169. case EV_LED: printf("Led"); break;
  170. case EV_SND: printf("Snd"); break;
  171. case EV_REP: printf("Rep"); break;
  172. case EV_FF: printf("FF"); break;
  173. break;
  174. }
  175. printf("\n");
  176. }
  177. printf("rc = %d, (%s)\n", rc, strerror(errno));
  178. close(fd);
  179. }
  180. }
  181. return 0;
  182. }
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/lanmanck/article/details/8423669

如何读取Linux键值,输入子系统,key,dev/input/event,dev/event,C语言键盘【转】的更多相关文章

  1. 读取当前键值,并赋值给LED

    /********************************* 代码功能:读取当前键值,并赋值给LED 使用函数: digitalRead(数字输入端口号); 创作时间:2016*10*07 作 ...

  2. JSON创建键值对(key是中文或者数字)方式详解

    JSON创建键值对(key是中文或者数字)方式详解 先准备好一个空的json对象 var obj = {}; 1. 最原始的方法 obj.name = 'zhangsan'; //这种方式很简单的添加 ...

  3. Linux驱动之输入子系统简析

    输入子系统由驱动层.输入子系统核心.事件处理层三部分组成.一个输入事件,如鼠标移动.键盘按下等通过Driver->Inputcore->Event handler->userspac ...

  4. linux驱动模型&lt;输入子系统&gt;

    在linux中提供一种输入子系统的驱动模型,其主要是实现在input.c中. 在输入子系统这套模型中,他把驱动分层分类.首先分为上下两层,上层为input.c .下层为驱动的实现,下层分为两部分,一部 ...

  5. 读取Properties键值对

    public class CommonFunc { /** * 取properties文件中的键值对 */ public static String getProperties(String para ...

  6. Properties/Property文件读取(键值均)乱码问题!

    方法一:使用native2ascii进行转码,这个不做说明,客户不可能帮你转码的. 方法二:当键是因为时直接getProperty即可,但加载后的propertis对象里的键也是中文乱码,就无法通过g ...

  7. Linux输入子系统 转载

    NQian 记录成长~ 首页 新随笔 联系 订阅 管理 随笔 - 305  文章 - 0  评论 - 254 12.Linux之输入子系统分析(详解)   在此节之前,我们学的都是简单的字符驱动,涉及 ...

  8. Android4.0 添加一个新的Android 键值

    这里添加新的键值,不是毫无凭据凭空创造的一个键值,而是根据kernel中检测到的按键值,然后转化为Android所需要的数值: 以添加一个Linux键值为217,把它映射为android的键值Brow ...

  9. Android4.2增加新键值

    这里添加新的键值,不是毫无凭据凭空创造的一个键值, 而是根据kernel中检测到的按键值,然后转化为android所需要的数值: 以添加一个linux键值为217,把它映射为android的键值Bro ...

随机推荐

  1. px em rem在WEB前端开发中的区别

    我们都知道基于像素的字体大小所用的单位是px,但是随着响应式设计的不断火热,基于相对字体大小的单位em变开始流行起来.当然,rem也在Web前端开发人员讨论如何更好设置字体大小的讨论话题之列.是不是需 ...

  2. O-C相关05:方法的封装.

    前言:在 OC 中进行封装, 就是实现设置实例变量和获取实例变量数据的方法, 常常称为 setter 方法和 getter 方法. 或称为 get set 读写器. 1,setter 方法 sette ...

  3. windows/NBTSTAT,linux/nmblookup命令详解,查询NetBIOS名

    NBTSTAT命令详解 请问: Linux下有没有和nbtstat一样的命令,用 nmblookup -A ip 可以 nbstat命令主要用于查看当前基于netbios的tcp/ip连接状态,通过该 ...

  4. 迁移/home目录至新硬盘分区总结--无备份情况下

    搞了一天,终于成功迁移.由于一开始就没备份过程实在很曲折. 希望本篇对那些没有备份习惯的朋友们有所帮助. 准备工作: sudo vim /etc/fstab 在文件中加入: /dev/sdb8     ...

  5. zabbix之1监控概念

    1.通过通用的snmp监控,无需代理端 2.通过snmp代理 snmp代理的工作原理:在被监控端设置代理,代理不断的获取本地数据,而管理端定期通过代理获取监控数据. snmp目前有v1,v2,v3三种 ...

  6. Space Shooter

    项目:https://pan.baidu.com/s/1jIDe2H4 using UnityEngine; using System.Collections; namespace VoidGame ...

  7. commons -lang(2) RandomStringUtils RandomUtils

    上一篇是StringUtils 链接http://www.cnblogs.com/tele-share/p/8060129.html 1.RandomStringUtils 1.1模拟实现random ...

  8. GitLab的安装及使用教程

    1.配置yum源 vim /etc/yum.repos.d/gitlab-ce.repo 复制以下内容: [gitlab-ce] name=gitlab-ce baseurl=http://mirro ...

  9. 汇编语言--微机CPU的指令系统(五)(循环指令)

    (8)循环指令 循环结构是程序的三大结构之一.为了方便构成循环结构,汇编语言提供了多种循环指令,这些循环指令的循环次数都是保存在计数器CX或ECX中.除了CX或ECX可以决定循环是否结束外,有的循环指 ...

  10. Node.js 回调函数 1) 阻塞 ,同步 2) 非阻塞 ,异步.

    1.阻塞. 同步. 1) 读取的文件: input.txt 菜鸟教程官网地址:www.runoob.com 2) main.js var fs = require("fs"); / ...