您的位置:首页 > 运维架构 > Linux

linux模拟按键

2015-06-30 11:17 357 查看
#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<
4000
/span>(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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: