使用/dev/uinput模拟键盘发送数据demo程序

 

 

    #include <stdio.h>  
    #include <stdlib.h>  
    #include <unistd.h>  
    #include <fcntl.h>  
    #include <string.h>  
    #include <linux/uinput.h>  
    #include <linux/input.h>  
      
      
    #define KEY_CUSTOM_UP 0x20  
    #define KEY_CUSTOM_DOWN 0x00  
      
      
    static struct uinput_user_dev uinput_dev;  
    static int uinput_fd;  
      
    int creat_user_uinput(void);  
    int report_key(unsigned int type, unsigned int keycode, unsigned int value);  
      
      
    int main(int argc, char *argv[])  
    {  
        int ret = 0;  
        ret = creat_user_uinput();  
        if(ret < 0){  
            printf("%s:%d\n", __func__, __LINE__);  
            return -1;//error process.  
        }  
          
        sleep(10);// help you to 'hexdump -C /dev/input/event[X]' for test.  
          
        report_key(EV_KEY, KEY_A, 1);// Report BUTTON A CLICK - PRESS event  
        report_key(EV_KEY, KEY_A, 0);// Report BUTTON A CLICK - RELEASE event  

report_key(EV_KEY, KEY_B, 1);// Report BUTTON A CLICK - PRESS event  
        report_key(EV_KEY, KEY_B, 0);// Report BUTTON A CLICK - RELEASE event  

report_key(EV_KEY, KEY_C, 1);// Report BUTTON A CLICK - PRESS event  
        report_key(EV_KEY, KEY_C, 0);// Report BUTTON A CLICK - RELEASE event  

	report_key(EV_KEY, KEY_CUSTOM_DOWN, 11);// Report BUTTON A CLICK - PRESS event  
        report_key(EV_KEY, KEY_CUSTOM_DOWN, 0);// Report BUTTON A CLICK - RELEASE event  
          
        report_key(EV_KEY, KEY_CUSTOM_UP, 12);  
        report_key(EV_KEY, KEY_CUSTOM_UP, 0);  
          
        sleep(5);  
        close(uinput_fd);  
        return 0;  
    }  
      
      
    int creat_user_uinput(void)  
    {  
        int i;  
        int ret = 0;  
      
        uinput_fd = open("/dev/uinput", O_RDWR | O_NDELAY);  
        if(uinput_fd < 0){  
            printf("%s:%d\n", __func__, __LINE__);  
            return -1;//error process.  
        }  
          
        //to set uinput dev  
        memset(&uinput_dev, 0, sizeof(struct uinput_user_dev));  
        snprintf(uinput_dev.name, UINPUT_MAX_NAME_SIZE, "uinput-custom-dev");  
        uinput_dev.id.version = 1;  
        uinput_dev.id.bustype = BUS_VIRTUAL;  
          
        ioctl(uinput_fd, UI_SET_EVBIT, EV_SYN);  
        ioctl(uinput_fd, UI_SET_EVBIT, EV_KEY);  
        ioctl(uinput_fd, UI_SET_EVBIT, EV_MSC);  
          
        for(i = 0; i < 256; i++){  
            ioctl(uinput_fd, UI_SET_KEYBIT, i);  
        }  
        ioctl(uinput_fd, UI_SET_MSCBIT, KEY_CUSTOM_UP);  
        ioctl(uinput_fd, UI_SET_MSCBIT, KEY_CUSTOM_DOWN);  
          
        ret = write(uinput_fd, &uinput_dev, sizeof(struct uinput_user_dev));  
        if(ret < 0){  
            printf("%s:%d\n", __func__, __LINE__);  
            return ret;//error process.  
        }  
          
        ret = ioctl(uinput_fd, UI_DEV_CREATE);  
        if(ret < 0){  
            printf("%s:%d\n", __func__, __LINE__);  
            close(uinput_fd);  
            return ret;//error process.  
        }  
    }  
      
    int report_key(unsigned int type, unsigned int keycode, unsigned int value)  
    {  
        struct input_event key_event;  
        int ret;  
          
        memset(&key_event, 0, sizeof(struct input_event));  
          
        gettimeofday(&key_event.time, NULL);  
        key_event.type = type;  
        key_event.code = keycode;  
        key_event.value = value;  
        ret = write(uinput_fd, &key_event, sizeof(struct input_event));  
        if(ret < 0){  
            printf("%s:%d\n", __func__, __LINE__);  
            return ret;//error process.  
        }  
          
        gettimeofday(&key_event.time, NULL);  
        key_event.type = EV_SYN;  
        key_event.code = SYN_REPORT;  
        key_event.value = 0;//event status sync  
        ret = write(uinput_fd, &key_event, sizeof(struct input_event));  
        if(ret < 0){  
            printf("%s:%d\n", __func__, __LINE__);  
            return ret;//error process.  
        }  
          
        return 0;  
    }  

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值