Chinaunix首页 | 论坛 | 博客
  • 博客访问: 4027425
  • 博文数量: 366
  • 博客积分: 9916
  • 博客等级: 中将
  • 技术积分: 7195
  • 用 户 组: 普通用户
  • 注册时间: 2011-05-29 23:27
个人简介

简单!

文章分类

全部博文(366)

文章存档

2013年(51)

2012年(269)

2011年(46)

分类: C/C++

2012-12-18 00:51:38


  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <unistd.h>
  5. #include <fcntl.h>
  6. #include <errno.h>
  7. #include <linux/input.h>
  8. #include <linux/uinput.h>


  9. void usage(void)
  10. {
  11.     printf(" Usage : keyboard [key number...]\n");
  12.     printf(" keyboard 29 56 32 /* CTRL + ALT +D */\n");
  13.     printf(" keyboard 125 32 /* WIN +D */\n");
  14.     printf("/* Types of key number are defined in /usr/include/linux/input.h */\n");

  15.     return;
  16. }

  17. /* Setup the uinput device */
  18. int setup_uinput_device(int fd)
  19. {
  20.     struct uinput_user_dev uidev;    // uInput device structure

  21.     memset(&uidev, 0, sizeof(struct uinput_user_dev));    // Intialize the uInput device to NULL
  22.     snprintf(uidev.name, UINPUT_MAX_NAME_SIZE, "uinput-keyboard");
  23.     uidev.id.bustype = BUS_USB;
  24.     uidev.id.vendor = 0x1;
  25.     uidev.id.product = 0x1;
  26.     uidev.id.version = 1;
  27.     // Setup the driver I/O channels
  28.     ioctl(fd, UI_SET_EVBIT, EV_KEY);
  29.     ioctl(fd, UI_SET_KEYBIT, BTN_LEFT);
  30.     for (int i = 0; i < 256; i++) {
  31.         ioctl(fd, UI_SET_KEYBIT, i);
  32.     }
  33.     ioctl(fd, UI_SET_EVBIT, EV_REL);
  34.     ioctl(fd, UI_SET_RELBIT, REL_X);
  35.     ioctl(fd, UI_SET_RELBIT, REL_Y);

  36.     /* Create input device into input sub-system */
  37.     write(fd, &uidev, sizeof(struct uinput_user_dev));
  38.     if (ioctl(fd, UI_DEV_CREATE) < 0) {
  39.         printf("error: create uinput device\n");
  40.         return -1;
  41.     }

  42.     return 0;
  43. }

  44. void uinput_key(int fd, int keycode, int keyvalue)
  45. {
  46.     struct input_event ev;            // Input device structure

  47.     memset(&ev, 0, sizeof(struct input_event));
  48.     gettimeofday(&ev.time, NULL);
  49.     ev.type = EV_KEY;
  50.     ev.code = keycode;
  51.     ev.value = keyvalue;
  52.     if (write(fd, &ev, sizeof(struct input_event)) < 0) {
  53.         printf("uinput key error\n");
  54.         return;
  55.     } else {
  56.         printf("uinput key %d, %d\n", keycode, keyvalue);
  57.     }

  58.     return;
  59. }

  60. void uinput_key_end(int fd)
  61. {
  62.     struct input_event ev;            // Input device structure

  63.     memset(&ev, 0, sizeof(struct input_event));
  64.     ev.type = EV_SYN;
  65.     ev.code = 0;
  66.     ev.value = 0;
  67.     write(fd, &ev, sizeof(struct input_event));

  68.     return;
  69. }

  70. //check the key is control key
  71. int check_valid_control_key(int key)
  72. {
  73.     int ret = 0;
  74.     int control_key[] = {29, 42, 54, 56, 97, 100, 125, 126};
  75.     int len = sizeof(control_key) / sizeof(control_key[0]);

  76.     for (int i = 0; i < len; i++) {
  77.         if (control_key[i] == key) {
  78.             ret = 1;
  79.             break;
  80.         }
  81.     }

  82.     return ret;
  83. }

  84. int main(int argc, char *argv[])
  85. {
  86.     int fd = -1;
  87.     int key;
  88.     int i;
  89.     
  90.     if (argc <= 1) {
  91.         usage();
  92.         return -1;
  93.     }

  94.     fd = open("/dev/uinput", O_WRONLY | O_NONBLOCK);    // Open the input device
  95.     if (fd < 0) {
  96.         printf("error: open /dev/uinput\n");
  97.         return -1;
  98.     }

  99.     setup_uinput_device(fd);

  100.     sleep(1);

  101.     for (i = 1; i < argc; i++) {
  102.         key = atoi(argv[i]);
  103.         uinput_key(fd, key, 1);
  104.         if (check_valid_control_key(key) == 0) {
  105.             uinput_key(fd,key, 0);
  106.         }
  107.     }

  108.     for (i = 1; i < argc; i++) {
  109.         key = atoi(argv[i]);
  110.         if (check_valid_control_key(key) == 1) {
  111.             uinput_key(fd,key, 0);
  112.         }
  113.     }

  114.     uinput_key_end(fd);
  115.     if (ioctl(fd, UI_DEV_DESTROY) < 0) {
  116.         printf("error: ioctl");
  117.         return -1;
  118.     }

  119.     close(fd);
  120.     fd = -1;

  121.     return 0;
  122. }
阅读(2222) | 评论(0) | 转发(1) |
给主人留下些什么吧!~~