Linux 使用uinput创建虚拟input设备
2014-04-04 15:14
489 查看
参考了这里:http://thiemonge.org/getting-started-with-uinput
代码如下:
代码如下:
#include <linux/input.h> #include <linux/uinput.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <unistd.h> #include <string.h> #include <stdint.h> #include <stdio.h> int reportkey(int fd, uint16_t type, uint16_t keycode, int32_t value) { int ret; struct input_event ev; memset(&ev, 0, sizeof(struct input_event)); ev.type = type; ev.code = keycode; ev.value = value; ret = write(fd, &ev, sizeof(struct input_event)); if (ret < 0) { printf("report key error!\n"); return ret; } /* printf("key is %d %d\n", keycode, value);*/ return 0; } int main(void) { struct uinput_user_dev uidev; int fd, ret; fd = open("/dev/uinput", O_WRONLY | O_NONBLOCK); if (fd < 0) { return fd; } ret = ioctl(fd, UI_SET_EVBIT, EV_KEY); ret = ioctl(fd, UI_SET_EVBIT, EV_SYN); ret = ioctl(fd, UI_SET_KEYBIT, KEY_D); memset(&uidev, 0, sizeof(struct uinput_user_dev)); snprintf(uidev.name, UINPUT_MAX_NAME_SIZE, "uinput-sample"); uidev.id.bustype = BUS_USB; uidev.id.vendor = 0x1234; uidev.id.product = 0xfedc; uidev.id.version = 1; ret = write(fd, &uidev, sizeof(struct uinput_user_dev)); ret = ioctl(fd, UI_DEV_CREATE); if (ret < 0) { close(fd); return ret; } while (1) { reportkey(fd, EV_KEY, KEY_D, 1); reportkey(fd, EV_SYN, SYN_REPORT, 0); reportkey(fd, EV_KEY, KEY_D, 0); reportkey(fd, EV_SYN, SYN_REPORT, 0); sleep(1); } ioctl(fd, UI_DEV_DESTROY); close(fd); return 0; }
相关文章推荐
- centos svn快速搭建
- Linux进程间通信(二)---管道通信之无名管道及其基础实验
- linux 编译 openssl DES_SPtrans
- Linux进程间通信(一)---进程间通信概述及其种类
- 在Windows下搭建开发Linux程序的环境
- Linux vi常用命令
- Linux重启inotify配置max_user_watches无效被恢复默认值8192的正确修改方
- <摘录>Linux 环境下编译 0.11版本内核 kernel
- 【linux学习笔记】sed替换字符串
- linux新API---timerfd的使用方法
- LINUX 高手的 10 个习惯
- linux知道关键字,查找文件的方法
- linux alias 命令
- Ubuntu Linux 3D桌面完全教程
- linux 环境 mysql写入中文报错
- 遇到个鬼,在WIN08的DELL R710上安装CENTOS 63,无法格式化以前的硬盘分区,安装无法进行下去。
- windows下eclipse java开发能正常运行,移植到linux下,从Mysql中读取数据,出现中文乱码
- linux core dump学习
- linux新API---signalfd的使用方法
- linux初学内核之打印消息