linux 应用层模拟按键输入
2016-02-18 09:14
435 查看
#include <linux/input.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdint.h>
#include <stdio.h>
/*
struct input_event {
struct timeval time;
__u16 type;
__u16 code;
__s32 value;
};
#define EV_KEY 0x01
*/
int reportkey(int fd, uint16_t type, uint16_t keycode, int32_t value)
{
struct input_event event;
event.type = type;
event.code = keycode;
event.value = value;
gettimeofday(&event.time, 0);
if (write(fd, &event, sizeof(struct input_event)) < 0) {
printf("report key error!\n");
return -1;
}
return 0;
}
#define DEVNAME "/dev/input/event4"
#define KEYDOWN 1
#define KEYUP 0
int main(int argc, char *argv[])
{
uint16_t keycode;
int k_fd;
k_fd = open(DEVNAME, O_RDWR);
if (k_fd < 0) {
printf("open error!\n");
return k_fd;
}
keycode = KEY_A;
reportkey(k_fd, EV_KEY, keycode, KEYDOWN);
reportkey(k_fd, EV_KEY, keycode, KEYUP);
close(k_fd);
return 0;
}
上述代码是网上的一份代码,在运行过程中发现并没有起作用,愿意是发送按键后没有发送同步信号,每次reportkey后都再次reportkey(k_fd, 0, 0, 0)才行,也就是代码中的reportkey(k_fd,EV_KEY,keycode,KEYDOWN);reportkey(k_fd,EV_KEY,keycode,KEYDOWN);改为reportkey(k_fd,EV_KEY,keycode,KEYDOWN);reportkey(k_fd, 0, 0, 0);reportkey(k_fd,EV_KEY,keycode,KEYDOWN);reportkey(k_fd,
0, 0, 0);
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdint.h>
#include <stdio.h>
/*
struct input_event {
struct timeval time;
__u16 type;
__u16 code;
__s32 value;
};
#define EV_KEY 0x01
*/
int reportkey(int fd, uint16_t type, uint16_t keycode, int32_t value)
{
struct input_event event;
event.type = type;
event.code = keycode;
event.value = value;
gettimeofday(&event.time, 0);
if (write(fd, &event, sizeof(struct input_event)) < 0) {
printf("report key error!\n");
return -1;
}
return 0;
}
#define DEVNAME "/dev/input/event4"
#define KEYDOWN 1
#define KEYUP 0
int main(int argc, char *argv[])
{
uint16_t keycode;
int k_fd;
k_fd = open(DEVNAME, O_RDWR);
if (k_fd < 0) {
printf("open error!\n");
return k_fd;
}
keycode = KEY_A;
reportkey(k_fd, EV_KEY, keycode, KEYDOWN);
reportkey(k_fd, EV_KEY, keycode, KEYUP);
close(k_fd);
return 0;
}
上述代码是网上的一份代码,在运行过程中发现并没有起作用,愿意是发送按键后没有发送同步信号,每次reportkey后都再次reportkey(k_fd, 0, 0, 0)才行,也就是代码中的reportkey(k_fd,EV_KEY,keycode,KEYDOWN);reportkey(k_fd,EV_KEY,keycode,KEYDOWN);改为reportkey(k_fd,EV_KEY,keycode,KEYDOWN);reportkey(k_fd, 0, 0, 0);reportkey(k_fd,EV_KEY,keycode,KEYDOWN);reportkey(k_fd,
0, 0, 0);
相关文章推荐
- Linux锁机制
- linux通过端口号杀进程
- linux jdk 路径设置
- Linux 下 C++ 异常处理技巧
- Linux下利用backtrace追踪函数调用堆栈以及定位段错误
- Linux下c/c++开发之程序崩溃(Segment fault)时内核转储文件(core dump)生成设置方法
- Linux系统中的异常堆栈跟踪简单实现
- 一些关于linux的总结
- Linux中WordPress的搭建与配置
- archlinux 安装easygui
- archlinux安装-1-基础安装
- SARDU - 超简单制作多合一系统iso文件 (支持Win/PE/Linux启动盘)
- centos挂载硬盘
- centos系统日志
- archlinux安装-2-安装图形环境基础
- archlinux pacman常用命令
- linux命令--压缩解压
- archlinux提醒virtualbox里面qq消息
- 根据Archboot.iso猜测的archlinux最小化安装的包
- Linux文件特殊属性