linux模拟按键

#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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值