参考http://blog.csdn.net/r77683962/article/details/50858461
由于设置的qt监听的是红外遥控器,输入子系统为/dev/input/event2
代码如下,模拟上键发送给event2,
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <linux/input.h>
#include <string.h>
void simulate_key(int fd , int kval) {
struct input_event event;
int ret;
printf("start write:%d fd:%d\n", sizeof(event), fd);
event.type = EV_KEY;//down key
event.code = kval;
event.value = 1; //down key
//gettimeofday(&event.time, 0);
ret = write(fd, &event, sizeof(event));
printf("write %d ret=%d type:%d code:%d value:%d\n", __LINE__, ret, event.type, event.code, event.value);
event.type = EV_SYN;
event.code = SYN_REPORT;
event.value = 0;
ret = write(fd, &event, sizeof(event));
printf("write %d ret=%d type:%d code:%d value:%d\n", __LINE__, ret, event.type, event.code, event.value);
memset(&event, 0, sizeof(event));
gettimeofday(&event.time, NULL);
event.type = EV_KEY;
event.code = kval;
event.value = 0;
ret = write(fd, &event, sizeof(event));
printf("write %d ret=%d type:%d code:%d value:%d\n", __LINE__, ret, event.type, event.code, event.value);
event.type = EV_SYN;
event.code = SYN_REPORT;
event.value = 0;
ret = write(fd, &event, sizeof(event));
printf("write %d ret=%d type:%d code:%d value:%d\n", __LINE__, ret, event.type, event.code, event.value);
}
int main(void) {
int fd;
int ret;
struct input_event event = {0};
fd = open("/dev/input/event2", O_RDWR);
if (fd <= 0) {
printf("open device fail\n");
return -1;
}
#if 1
printf("start send a key\n");
//simulate_key(fd, KEY_UP);
simulate_key(fd, 103);
#else
while(1) {
ret = read(fd, &event, sizeof(event));
if (ret == sizeof(event)) {
printf("type:%d code:%d value:%d\n", event.type, event.code, event.value);
} else {
printf("fail read\n");
break;
}
}
#endif
close(fd);
return 0;
}