linux模拟按键

来源:互联网 发布:centos twisted 安装 编辑:程序博客网 时间:2024/05/16 13:07
#include <stdio.h>#include <stdlib.h>#include <signal.h>#include <sys/fcntl.h>#include <sys/ioctl.h>#include <sys/mman.h>#include <sys/time.h>#include <linux/input.h>int main (){  int keys_fd;  char ret[2];  char a;  struct input_event t,t0;struct timeval  tv; struct timezone tz;int pressed_times;pressed_times = 0;  keys_fd = open ("/dev/input/event0",  O_RDWR);  if (keys_fd <= 0)    {      printf ("open /dev/input/event0 device error!\n");      return 0;    } //if (read (keys_fd, &t, sizeof (t)) == sizeof (t));  while (1)    {        a = getchar();        printf("Input char: %c",a);        switch(a)        {        case 'p':                printf ("Pressed once!\n");                gettimeofday(&t.time, 0);                t.type = EV_ABS;                t.value = 1;                t.code = 57;  //x                if(write(keys_fd, &t, sizeof (t)) < 0)                    printf("write event failed!!");                t.type = EV_ABS;                t.value = 226;                t.code = 53;  //x                if(write(keys_fd, &t, sizeof (t)) < 0)                    printf("write event failed!!");                t.type = EV_ABS;                t.value = 273;                t.code = 54;  //y                if(write(keys_fd, &t, sizeof (t)) < 0)                    printf("write event failed!!");                t.type = EV_KEY;  // key                t.value = 1;                t.code = 330;                if(write(keys_fd, &t, sizeof (t)) < 0)                    printf("write event failed!!");                t.type = EV_ABS;                t.value = 226;                t.code = 0;  //x                if(write(keys_fd, &t, sizeof (t)) < 0)                    printf("write event failed!!");                t.type = EV_ABS;                t.value = 273;                t.code = 1;  //y                if(write(keys_fd, &t, sizeof (t)) < 0)                    printf("write event failed!!");                t.type = EV_SYN;                t.code = SYN_REPORT;                t.value = 0;                if(write(keys_fd, &t, sizeof (t)) < 0)                    printf("write event failed!!");                break;        case 'r':                printf ("Released once!\n");                gettimeofday(&t.time, 0);                t.type = EV_ABS;                t.value = -1;                t.code = 57;  //y                if(write(keys_fd, &t, sizeof (t)) < 0)                    printf("write event failed!!");                t.type = EV_KEY;                t.value = 0;                t.code = 330;                if(write(keys_fd, &t, sizeof (t)) < 0)                    printf("write event failed!!");                t.type = EV_SYN;                t.code = SYN_REPORT;                t.value = 0;                if(write(keys_fd, &t, sizeof (t)) < 0)                    printf("write event failed!!");                break;        case 't':                t.type = EV_ABS;                gettimeofday(&t.time, 0);                t.type = EV_ABS;                t.value = 690;                t.code = 53;  //x                if(write(keys_fd, &t, sizeof (t)) < 0)                    printf("write event failed!!");                t.type = EV_ABS;                t.value = 145;                t.code = 54;  //y                if(write(keys_fd, &t, sizeof (t)) < 0)                    printf("write event failed!!");                t.value = 690;                t.code = 0;  //x                if(write(keys_fd, &t, sizeof (t)) < 0)                    printf("write event failed!!");                t.type = EV_ABS;                t.value = 145;                t.code = 1;  //y                if(write(keys_fd, &t, sizeof (t)) < 0)                    printf("write event failed!!");                t.type = EV_SYN;                t.code = SYN_REPORT;                t.value = 0;                if(write(keys_fd, &t, sizeof (t)) < 0)                    printf("write event failed!!");                break;                      case 'e':                close (keys_fd);                exit(1);        default:                break;        }            }  return 0;}
0 0
原创粉丝点击