linux在用户程序中如何向操作系统发送按键事件

前些时候也在为了实现这个功能而头疼, 也在csdn上发过贴子求助, 下面是以前的贴子

 

http://topic.csdn.net/u/20110125/15/9bc20719-ea89-41e9-a4d3-687110bf2722.html

 

 考虑到很多人也可能都需要这些类似的功能, 尤其是搞嵌入式的, 我解决这个问题的思路也是从android系统中借鉴的,这个功能需要首先在内核中添加uinput模块, 大家也可以将这个模块直接编译进内核里面, 编译内核大家都熟悉, 我就不在说了, 都是整天配置编译的人啊。。。

 

下面两个文件直接取自我们一个使用手机远程控制系统, 下面是代码, 代码中已经有了注释, 不在解释了, 希望以上代码会对你有所帮助。

 

[c-sharp] view plain copy print ?
  1. /** 
  2.  * @file  The file is using for (>>> function<<<) 
  3.  * @author imxiangpeng 
  4.  * @time   
  5.  * 
  6.  * Copyright (C) imxiangpeng@gmail.com, Inc. All Rights Reserved. 
  7.  * Written by imxiangpeng@gmail.com 
  8.  * 
  9.  * This program is free software; you can redistribute it and/or 
  10.  * modify it under the terms of the GNU General Public License 
  11.  * as published by the Free Software Foundation; either version 
  12.  * 2 of the License, or (at your option) any later version. 
  13.  */ 
  14. #ifndef RCBOARDMOUSE_H  
  15. #define RCBOARDMOUSE_H  
  16. #include <string.h>  
  17. #include <stdio.h>  
  18. #include <sys/types.h>  
  19. #include <sys/stat.h>  
  20. #include <fcntl.h>  
  21. #include <linux/input.h>  
  22. #include <linux/uinput.h>  
  23. #include <sys/time.h>  
  24. #include <unistd.h>  
  25. #include <errno.h>   
  26. static void kbdms_open_dev();  
  27. static int kbdms_init_dev(char *dev);  
  28. extern void kbdms_close_dev(void); /* you can call the function explicitly if you no need the function anymore*/  
  29. /* only the following functions which you can called, we initialise the device when you call the following function firstly */  
  30. /* key_code: referce to linux/input.h 
  31.    is_pressed_or_release: 1 if the key is pressed , 0 :release 
  32.    注意按键有按下,就必然会有释放,如果没有释放,再次调用按下函数是没有反映的 
  33. */  
  34. extern void kbdms_send_button_event(unsigned short key_code, int is_pressed_or_release);  
  35. /* is_rel:1--relative move, else--absolutely 
  36.    if you want to send left or right mouse button , please call kbdms_send_button_event 
  37. */  
  38. //extern void kbdms_send_mouse_move_events(unsigned short is_rel, unsigned short pos_x, unsigned short pos_y);   
  39. extern void kbdms_send_mouse_move_events(unsigned short is_rel, short pos_x, short pos_y);  
  40. extern void kbdms_send_wheel_events(short wheel_value);  
  41. #endif  /* RCBOARDMOUSE_H */  
 

 

 

[c-sharp] view plain copy print ?
  1. /** 
  2.  * @file  The file is using for (>>> function<<<) 
  3.  * @author imxiangpeng 
  4.  * @time   
  5.  * 
  6.  * Copyright (C) imxiangpeng@gmail.com, Inc. All Rights Reserved. 
  7.  * Written by imxiangpeng@gmail.com 
  8.  * 
  9.  * This program is free software; you can redistribute it and/or 
  10.  * modify it under the terms of the GNU General Public License 
  11.  * as published by the Free Software Foundation; either version 
  12.  * 2 of the License, or (at your option) any later version. 
  13.  */ 
  14.   
  15. #include "rcboardmouse.h"   
  16. static int uinput_fd = -1;  
  17. static struct uinput_user_dev uinput;  
  18. static struct input_event event;  
  19. static void kbdms_open_dev(){  
  20.   int ret = -1;  
  21.   ret = kbdms_init_dev("/dev/uinput");  
  22.   if(ret < 0){  
  23.     ret = kbdms_init_dev("/dev/input/uinput");  
  24.     if(ret < 0){  
  25.       printf("call kbdms_open_dev failure/n");  
  26.       return;  
  27.     }  
  28.   }  
  29. }  
  30. static int kbdms_init_dev(char *dev){  
  31.   int i = 0;  
  32.   if(dev == NULL){  
  33.     printf("The device is null/n");  
  34.     return -1;  
  35.   }  
  36.   uinput_fd = open(dev, O_WRONLY | O_NDELAY);  
  37.   if(uinput_fd < 0){  
  38.     perror("open uinput device failure:");  
  39.     return -1;  
  40.   }  
  41.   memset(&uinput, 0, sizeof(uinput));  
  42.   strncpy(uinput.name,"xiangpeng's virtual device",UINPUT_MAX_NAME_SIZE);  
  43.   uinput.id.version = 4;  
  44.   uinput.id.bustype = BUS_USB;  
  45.   ioctl(uinput_fd, UI_SET_EVBIT, EV_KEY);  
  46.   ioctl(uinput_fd, UI_SET_EVBIT, EV_REL);  
  47.   ioctl(uinput_fd, UI_SET_RELBIT, REL_X);  
  48.   ioctl(uinput_fd, UI_SET_RELBIT, REL_Y);  
  49.   ioctl(uinput_fd, UI_SET_RELBIT, REL_WHEEL);  
  50.     
  51.   for(i = 0; i < 256; i++){  
  52.     ioctl(uinput_fd, UI_SET_KEYBIT, i);  
  53.   } /* end for */  
  54.   ioctl(uinput_fd, UI_SET_KEYBIT, BTN_MOUSE);  
  55.   ioctl(uinput_fd, UI_SET_KEYBIT, BTN_TOUCH);  
  56.   ioctl(uinput_fd, UI_SET_KEYBIT, BTN_LEFT);  
  57.   ioctl(uinput_fd, UI_SET_KEYBIT, BTN_MIDDLE);  
  58.   ioctl(uinput_fd, UI_SET_KEYBIT, BTN_RIGHT);  
  59.   ioctl(uinput_fd, UI_SET_KEYBIT, BTN_FORWARD);  
  60.   ioctl(uinput_fd, UI_SET_KEYBIT, BTN_BACK);  
  61.   /* Create input device into input sub-system */  
  62.   write(uinput_fd, &uinput, sizeof(uinput));  
  63.   if(ioctl(uinput_fd, UI_DEV_CREATE)){  
  64.     perror("create uinput device");  
  65.     return -1;  
  66.   }  
  67.   return 0;                     /* success */  
  68.     
  69. /* end init_uinput_dev function */  
  70. /* is_rel:1--relative move, else--absolutely 
  71.    which_button_pressed: 0--no button press, 1--left button press, 2--right button press 3--middle button press 
  72.     
  73.  */  
  74. void kbdms_send_mouse_move_events(unsigned short is_rel, short pos_x, short pos_y){  
  75.   if(uinput_fd <= 0){  
  76.       
  77.     printf("RC : %s please call dbdms_init_dev first/n",__FUNCTION__);  
  78.     kbdms_open_dev();  
  79.     if(uinput_fd <= 0)  
  80.       return;  
  81.   }  
  82.   /* Move pointer to (0,0) location */  
  83.   memset(&event, 0, sizeof(event));  
  84.   gettimeofday(&event.time, NULL);  
  85.   event.type = (is_rel == 1)?EV_REL:EV_ABS;  
  86.   event.code = (is_rel == 1)?REL_X:ABS_X;  
  87.   event.value = pos_x;  
  88.   write(uinput_fd, &eventsizeof(event));    
  89.   event.type = EV_SYN;  
  90.   event.code = SYN_REPORT;  
  91.   event.value = 0;  
  92.   write(uinput_fd, &eventsizeof(event));  
  93.   memset(&event, 0, sizeof(event));  
  94.   gettimeofday(&event.time, NULL);  
  95.   event.type = (is_rel == 1)?EV_REL:EV_ABS;  
  96.   event.code = (is_rel == 1)?REL_Y:ABS_Y;  
  97.   event.value = pos_y;  
  98.   write(uinput_fd, &eventsizeof(event));  
  99.   /* 鼠标点击事件我们也通过 kbdms_send_button_event 来发送*/  
  100.     
  101. /* end send_click_events function */  
  102. /* key_code: referce to linux/input.h 
  103.    is_pressed_or_release: 1 if the key is pressed , 0 :release 
  104.  */  
  105. void kbdms_send_button_event(unsigned short key_code, int is_pressed_or_release){  
  106.     
  107.   if(uinput_fd <= 0){  
  108.     printf("RC : %s please call dbdms_init_dev first/n",__FUNCTION__);  
  109.     kbdms_open_dev();  
  110.     if(uinput_fd <= 0)  
  111.       return;  
  112.   }  
  113.   /* report button click --press event */  
  114.     
  115.   memset(&event, 0, sizeof(event));  
  116.   gettimeofday(&event.time, NULL);  
  117.   event.type = EV_KEY;  
  118.   event.code = key_code;  
  119.   event.value = is_pressed_or_release;  
  120.   write(uinput_fd, &eventsizeof(event));  
  121.   event.type = EV_SYN;  
  122.   event.code = SYN_REPORT;  
  123.   event.value = 0;  
  124.   write(uinput_fd, &eventsizeof(event));  
  125.     
  126. }  
  127. void kbdms_send_wheel_events(short wheel_value){  
  128.   if(uinput_fd <= 0){  
  129.     printf("RC : %s please call dbdms_init_dev first/n",__FUNCTION__);  
  130.     kbdms_open_dev();      
  131.     if(uinput_fd <= 0)  
  132.       return;  
  133.      
  134.   }  
  135.   /* Move pointer to (0,0) location */  
  136.   memset(&event, 0, sizeof(event));  
  137.   gettimeofday(&event.time, NULL);  
  138.   event.type = EV_REL;  
  139.   event.code = REL_WHEEL;  
  140.   event.value = wheel_value;  
  141.   write(uinput_fd, &eventsizeof(event));    
  142.   event.type = EV_SYN;  
  143.   event.code = SYN_REPORT;  
  144.   event.value = 0;  
  145.   write(uinput_fd, &eventsizeof(event));  
  146.     
  147. /* end send_click_events function */  
  148. void kbdms_close_dev(void){  
  149.   if(uinput_fd > 0){  
  150.     ioctl(uinput_fd, UI_DEV_DESTROY);  
  151.     close(uinput_fd);  
  152.   }  
  153. }  
  154. #if 0                           /* using for test all the functions */   
  155. int main(int argc, char **argv){  
  156.   if(argc <2){  
  157.     printf("Please specified the uinput device path/n");  
  158.     return -1;  
  159.   }  
  160.   printf("The device is :%s/n",argv[1]);  
  161.   if(kbdms_init_dev(argv[1]) < 0){  
  162.     printf("Call init_uinput_dev failure/n");  
  163.     return -1;  
  164.   }  
  165.     
  166.   while(1){  
  167.     printf("we will send a click and a button event/n");  
  168.     //send_click_events();   
  169.     kbdms_send_button_event(KEY_LEFT, 1);  
  170.     kbdms_send_button_event(KEY_LEFT, 0);  
  171.     sleep(3);  
  172.     kbdms_send_button_event(KEY_DOWN,1);  
  173.     kbdms_send_button_event(KEY_DOWN,0);  
  174.     sleep(2);  
  175.     kbdms_send_button_event(KEY_DOWN,1);  
  176.     kbdms_send_button_event(KEY_DOWN,0);  
  177.     sleep(2);  
  178.     printf("we will call kbdms_send_wheel_events/n");  
  179.     kbdms_send_wheel_events(10);  
  180.     printf("We will wheel back up/n");  
  181.     sleep(2);  
  182.       
  183.     kbdms_send_wheel_events(-10);  
  184.   }  
  185.   kbdms_close_dev();  
  186.   return 0;  
  187. }  
  188. #endif  
 

 

 

希望以上代码会对你有所帮助, 如果有什么疑问也可以给我发邮件, 邮件地址在每个文件的头部

当然, 本人计算机都是自学的, 如果各位看着在编程风格和代码书写上如果有什么不足, 希望大家可以提出来

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值