使用UInput模拟系统键盘鼠标动作
来源:互联网 发布:php异步执行 编辑:程序博客网 时间:2024/03/28 17:00
1. 打开UInput Device:
应用程序:
dev 为 UInput Node名:通常为/dev/uinput。
open(dev, O_WRONLY | O_NDELAY);
此时,在Kernel 层,对应的动作为:
static int uinput_open(struct inode *inode, struct file *file)
参数inode对应的是 主设备为10,子设备为223的node(即位用户态的dev)
参数file对应打开的文件。
动作:
创建了newdev-- uinput_device结构。
newdev->state = UIST_NEW_DEVICE;
file->private_data = newdev;
2. 设置UInput Device:
ioctl(fd, UI_SET_EVBIT, EV_KEY);
此时,在Kernel 层,对应的动作为:
static long uinput_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
参数file对应打开的文件。
参数cmd 对应用户态ioctl参数2。UI_SET_EVBIT
参数arg对应用户态ioctl参数3。EV_KEY
动作:
2.1 将driver参数传递过来。
udev = file->private_data;
udev->dev
如果udev->dev为空,则使用 uinput_allocate_device(udev);申请input_dev结构
具体到CMD=UI_SET_EVBIT
uinput_set_bit(arg, evbit, EV_MAX);
首先判断newdev->state为UIST_CREATED,则返回错误码。
这就说明:设置bit,需要在create input device 之前。
具体动作为:udev->dev->evbit 设为EV_KEY.
注意:此处input device的evbit:
一个是evbit.表示设备所支持的动作.
#define EV_KEY
#define EV_REL
3.继续设置 Device:
ret = ioctl(fd, UI_SET_RELBIT, REL_X); //鼠标
ret = ioctl(fd, UI_SET_RELBIT, REL_Y);
ret = ioctl(fd, UI_SET_EVBIT, EV_ABS);
ret = ioctl(fd, UI_SET_ABSBIT, ABS_X);
ret = ioctl(fd, UI_SET_ABSBIT, ABS_Y);
ret = ioctl(fd, UI_SET_ABSBIT, ABS_PRESSURE);
同上。设置了Keybit等。
这里就是设置了Input Device关心或者说会产生的消息。
4. 写入设备:
struct uinput_user_dev uinput;
uinput.id.version = 4;
uinput.id.bustype = BUS_USB;
uinput.absmin[ABS_X] = 0;
uinput.absmax[ABS_X] = 65535; //sam 把屏幕设为0-65535
uinput.absmin[ABS_Y] = 0;
uinput.absmax[ABS_Y] = 65535;
uinput.absmin[ABS_PRESSURE] = 0;
uinput.absmax[ABS_PRESSURE] = 0xfff;
ret = write(fd, &uinput, sizeof(uinput));
此时,在Kernel 层,对应的动作为:
此时Device status为UIST_NEW_DEVICE
并将udev->dev 这个input device 具体化。初始化该input_dev。
之后,改变状态:
udev->state = UIST_SETUP_COMPLETE;
5.创建Input Device:
注意,此处是创建了Input Device。而不是UInput Device。
ioctl(fd, UI_DEV_CREATE);
input_register_device(udev->dev); //向子系统注册该设备,之后中断时input_event()向子系统报告事件
udev->state = UIST_CREATED;
6. 向Input Device发送Event:
struct input_event event = {0};
static ssize_t uinput_write(struct file *file, const char __user *buffer, size_t count, loff_t *ppos)
因为此时state为UIST_CREATED
input_event(udev->dev, ev.type, ev.code, ev.value);
发送event.
总结:
使用UInput的步骤为:
1. 打开设备。
2. 使用ioctl() 配置设备。
3. 使用write() 将input device信息设置好。
4. 使用ioctl(UI_DEV_CREATE)创建Input Device。(即使用write设置的)
5. 再使用write() 写入event.
UInput添加的Input Device在/proc的反应:
#cat /proc/bus/input/device
I: Bus=0003 Vendor=0000 Product=0000 Version=0004
N: Name="uinput"
P: Phys=
S: Sysfs=/class/input/input6
H: Handlers=event1 mouse1
B: EV=f
B: KEY=400 0 670000 ffff ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff
B: REL=3
B: ABS=1000003
解释如下:
Bus=0003 Vendor=0000 Product=0000 Version=0004
这是在第一次write时设置的:
uinp.id.version = 4;
uinp.id.bustype = BUS_USB;
struct input_id {
};
EV=f
源地址:http://blog.sina.com.cn/s/blog_602f87700100llew.html
- 使用UInput模拟系统键盘鼠标动作
- 使用UInput模拟系统键盘鼠标动作
- 使用UInput模拟系统键盘鼠标动作
- 使用UInput模拟系统键盘鼠标动作 UInput driver分析
- 使用UInput模拟系统键盘鼠标动作 UInput driver分析
- 使用SendInput模拟键盘和鼠标动作
- uinput模拟鼠标
- Python-模拟鼠标键盘动作
- 2011-7 模拟鼠标键盘动作
- linux 虚拟输入设备(uinput)模拟鼠标和键盘的使用方法
- 【RFB】Linux uinput 分析,虚拟鼠标,键盘
- 【RFB】Linux uinput 分析,虚拟鼠标,键盘
- VFP调用API实现代码模拟鼠标.键盘动作
- VFP调用API实现代码模拟鼠标.键盘动作
- linux下python之模拟鼠标键盘动作具体实现
- 用pywin32实现windows模拟鼠标及键盘动作
- Linux使用uinput实现虚拟鼠标
- Android系统模拟鼠标键盘详解
- Red_Hat_Linux忘记root密码解决办法
- 20120418_升级时间统计
- QT移植步骤
- django数据库基本操作-增删改查(tip)-基本
- 系统管理员设置了系统策略,禁止进行此安装
- 使用UInput模拟系统键盘鼠标动作
- Python 字符串操作(string替换、删除、截取、复制、连接、比较、查找、包含、大小写转换、分割等)
- JFFS2 文件系统及新特性介绍
- unity脚本帮助不能搜索的解决办法
- c/c++基本功测试题一套
- 键盘键值表
- VC编译的五子棋游戏核心算法分析(附源码)
- 模糊系统:挑战与机遇并存——十年研究之感悟 王立新
- python Property属性用法