如何读取Linux键值,输入子系统,key,dev/input/event,dev/event,C语言键盘【转】

时间:2023-03-09 04:28:28
如何读取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:

  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. * Authors:
  32. * Rickard E. (Rik) Faith <faith@redhat.com>
  33. *
  34. */
  35. #include <stdio.h>
  36. #include <stdlib.h>
  37. #include <unistd.h>
  38. #include <string.h>
  39. #include <sys/types.h>
  40. #include <fcntl.h>
  41. #include <errno.h>
  42. #include <time.h>
  43. #include <linux/input.h>
  44. struct input_event event;
  45. int main(int argc, char **argv)
  46. {
  47. char name[64]; /* RATS: Use ok, but could be better */
  48. char buf[256] = { 0, }; /* RATS: Use ok */
  49. unsigned char mask[EV_MAX/8 + 1]; /* RATS: Use ok */
  50. int version;
  51. int fd = 0;
  52. int rc;
  53. int i, j;
  54. char *tmp;
  55. #define test_bit(bit) (mask[(bit)/8] & (1 << ((bit)%8)))
  56. for (i = 0; i < 32; i++) {
  57. sprintf(name, "/dev/input/event%d", i);
  58. if ((fd = open(name, O_RDONLY, 0)) >= 0) {
  59. ioctl(fd, EVIOCGVERSION, &version);
  60. ioctl(fd, EVIOCGNAME(sizeof(buf)), buf);
  61. ioctl(fd, EVIOCGBIT(0, sizeof(mask)), mask);
  62. printf("%s\n", name);
  63. printf(" evdev version: %d.%d.%d\n",
  64. version >> 16, (version >> 8) & 0xff, version & 0xff);
  65. printf(" name: %s\n", buf);
  66. printf(" features:");
  67. for (j = 0; j < EV_MAX; j++) {
  68. if (test_bit(j)) {
  69. const char *type = "unknown";
  70. switch(j) {
  71. case EV_KEY: type = "keys/buttons"; break;
  72. case EV_REL: type = "relative"; break;
  73. case EV_ABS: type = "absolute"; break;
  74. case EV_MSC: type = "reserved"; break;
  75. case EV_LED: type = "leds"; break;
  76. case EV_SND: type = "sound"; break;
  77. case EV_REP: type = "repeat"; break;
  78. case EV_FF: type = "feedback"; break;
  79. }
  80. printf(" %s", type);
  81. }
  82. }
  83. printf("\n");
  84. close(fd);
  85. }
  86. }
  87. if (argc > 1) {
  88. sprintf(name, "/dev/input/event%d", atoi(argv[1]));
  89. if ((fd = open(name, O_RDWR, 0)) >= 0) {
  90. printf("%s: open, fd = %d\n", name, fd);
  91. for (i = 0; i < LED_MAX; i++) {
  92. event.time.tv_sec = time(0);
  93. event.time.tv_usec = 0;
  94. event.type = EV_LED;
  95. event.code = i;
  96. event.value = 0;
  97. write(fd, &event, sizeof(event));
  98. }
  99. while ((rc = read(fd, &event, sizeof(event))) > 0) {
  100. printf("%-24.24s.%06lu type 0x%04x; code 0x%04x;"
  101. " value 0x%08x; ",
  102. ctime(&event.time.tv_sec),
  103. event.time.tv_usec,
  104. event.type, event.code, event.value);
  105. switch (event.type) {
  106. case EV_KEY:
  107. if (event.code > BTN_MISC) {
  108. printf("Button %d %s",
  109. event.code & 0xff,
  110. event.value ? "press" : "release");
  111. } else {
  112. printf("Key %d (0x%x) %s",
  113. event.code & 0xff,
  114. event.code & 0xff,
  115. event.value ? "press" : "release");
  116. }
  117. break;
  118. case EV_REL:
  119. switch (event.code) {
  120. case REL_X: tmp = "X"; break;
  121. case REL_Y: tmp = "Y"; break;
  122. case REL_HWHEEL: tmp = "HWHEEL"; break;
  123. case REL_DIAL: tmp = "DIAL"; break;
  124. case REL_WHEEL: tmp = "WHEEL"; break;
  125. case REL_MISC: tmp = "MISC"; break;
  126. default: tmp = "UNKNOWN"; break;
  127. }
  128. printf("Relative %s %d", tmp, event.value);
  129. break;
  130. case EV_ABS:
  131. switch (event.code) {
  132. case ABS_X: tmp = "X"; break;
  133. case ABS_Y: tmp = "Y"; break;
  134. case ABS_Z: tmp = "Z"; break;
  135. case ABS_RX: tmp = "RX"; break;
  136. case ABS_RY: tmp = "RY"; break;
  137. case ABS_RZ: tmp = "RZ"; break;
  138. case ABS_THROTTLE: tmp = "THROTTLE"; break;
  139. case ABS_RUDDER: tmp = "RUDDER"; break;
  140. case ABS_WHEEL: tmp = "WHEEL"; break;
  141. case ABS_GAS: tmp = "GAS"; break;
  142. case ABS_BRAKE: tmp = "BRAKE"; break;
  143. case ABS_HAT0X: tmp = "HAT0X"; break;
  144. case ABS_HAT0Y: tmp = "HAT0Y"; break;
  145. case ABS_HAT1X: tmp = "HAT1X"; break;
  146. case ABS_HAT1Y: tmp = "HAT1Y"; break;
  147. case ABS_HAT2X: tmp = "HAT2X"; break;
  148. case ABS_HAT2Y: tmp = "HAT2Y"; break;
  149. case ABS_HAT3X: tmp = "HAT3X"; break;
  150. case ABS_HAT3Y: tmp = "HAT3Y"; break;
  151. case ABS_PRESSURE: tmp = "PRESSURE"; break;
  152. case ABS_DISTANCE: tmp = "DISTANCE"; break;
  153. case ABS_TILT_X: tmp = "TILT_X"; break;
  154. case ABS_TILT_Y: tmp = "TILT_Y"; break;
  155. case ABS_MISC: tmp = "MISC"; break;
  156. default: tmp = "UNKNOWN"; break;
  157. }
  158. printf("Absolute %s %d", tmp, event.value);
  159. break;
  160. case EV_MSC: printf("Misc"); break;
  161. case EV_LED: printf("Led"); break;
  162. case EV_SND: printf("Snd"); break;
  163. case EV_REP: printf("Rep"); break;
  164. case EV_FF: printf("FF"); break;
  165. break;
  166. }
  167. printf("\n");
  168. }
  169. printf("rc = %d, (%s)\n", rc, strerror(errno));
  170. close(fd);
  171. }
  172. }
  173. return 0;
  174. }
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/lanmanck/article/details/8423669