Dashboard January 2007 IssueMehul PatelUsing uinput driver in Linux-2.6.x to send user inputDashboard January 2007 IssueUsing uinput driver in Linux-2.6.x to send userinputIntroduction:The Linux 2.6.x provides a “uinput” driver, which helps users to inject data to the Linux kernel.This is very useful while writing applications to interface customized input devices like wirelessjoystick, keyboard etc.The driver uses /dev/uinput device to send data to kernel space which in turn send data to XwindowsOR active shell. This feature can be used to perform automated shell scripts whichinvolve graphical user inputs.Uinput is configured as a loadable module in most of the Linux kernels. You can load uinputdriver by giving the following commands.$ modprobe uinput$ lsmodThe “lsmod” command lists all the drivers loaded in the Linux system. You should see “uinput”driver in the list.The next step is to develop a sample application. This application will send the user keysequence to kernel which is in turn sent to X-Windows or shell.Opening an input device:// Open the input deviceuinp_fd = open("/dev/uinput", O_WRONLY | O_NDELAY);if (uinp_fd == NULL){printf("Unable to open /dev/uinput/n");return -1;}After opening device you have to configure the uinput device parameters (mouse, keyboard,etc) using the ioctl() function such as:ioctl (out_fd, UI_SET_EVBIT, EV_KEY);ioctl (out_fd, UI_SET_EVBIT, EV_REP);The EV_KEY and EV_REP inform the uinput driver as the event is a keyboard event and thekey value contains the key repetition property.Dashboard January 2007 IssueSending events to kernel:All the events coming from the user program will be carried to the kernel space through thestructure "struct input_event" defined in kernels "/usr/include/linux/input.h".A keyboard can be generated using following piece of code:event.type = EV_KEY;event.code = KEY_ENTER;event.value = 1;write (uinp_fd, &event, sizeof(event));The above example will send ENTER key to kernel. This key event in-turn will be sent to userspace application. All the key definitions are defined in “/usr/include/linux/input.h” file.You can use the following sample code to test Linux uinput interface.// uinput.c#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

/* Globals */

static int uinp_fd = -1;

struct uinput_user_dev uinp; // uInput device structure

struct input_event event; // Input device structure

/* Setup the uinput device */

int setup_uinput_device()

{

// Temporary variable

int i=0;

// Open the input device

uinp_fd = open("/dev/uinput", O_WRONLY | O_NDELAY);

if (uinp_fd == NULL)

{

Dashboard January 2007 Issue

printf("Unable to open /dev/uinput/n");

return -1;

}

memset(&uinp,0,sizeof(uinp)); // Intialize the uInput device to NULL

strncpy(uinp.name, "PolyVision Touch Screen", UINPUT_MAX_NAME_SIZE);

uinp.id.version = 4;

uinp.id.bustype = BUS_USB;

// Setup the uinput device

ioctl(uinp_fd, UI_SET_EVBIT, EV_KEY);

ioctl(uinp_fd, UI_SET_EVBIT, EV_REL);

ioctl(uinp_fd, UI_SET_RELBIT, REL_X);

ioctl(uinp_fd, UI_SET_RELBIT, REL_Y);

for (i=0; i < 256; i++) {

ioctl(uinp_fd, UI_SET_KEYBIT, i);

}

ioctl(uinp_fd, UI_SET_KEYBIT, BTN_MOUSE);

ioctl(uinp_fd, UI_SET_KEYBIT, BTN_TOUCH);

ioctl(uinp_fd, UI_SET_KEYBIT, BTN_MOUSE);

ioctl(uinp_fd, UI_SET_KEYBIT, BTN_LEFT);

ioctl(uinp_fd, UI_SET_KEYBIT, BTN_MIDDLE);

ioctl(uinp_fd, UI_SET_KEYBIT, BTN_RIGHT);

ioctl(uinp_fd, UI_SET_KEYBIT, BTN_FORWARD);

ioctl(uinp_fd, UI_SET_KEYBIT, BTN_BACK);

/* Create input device into input sub-system */

write(uinp_fd, &uinp, sizeof(uinp));

if (ioctl(uinp_fd, UI_DEV_CREATE))

{

printf("Unable to create UINPUT device.");

return -1;

}

return 1;

}

void send_click_events( )

{

// Move pointer to (0,0) location

memset(&event, 0, sizeof(event));

gettimeofday(&event.time, NULL);

event.type = EV_REL;

event.code = REL_X;

Dashboard January 2007 Issue

event.value = 100;

write(uinp_fd, &event, sizeof(event));

event.type = EV_REL;

event.code = REL_Y;

event.value = 100;

write(uinp_fd, &event, sizeof(event));

event.type = EV_SYN;

event.code = SYN_REPORT;

event.value = 0;

write(uinp_fd, &event, sizeof(event));

// Report BUTTON CLICK - PRESS event

memset(&event, 0, sizeof(event));

gettimeofday(&event.time, NULL);

event.type = EV_KEY;

event.code = BTN_LEFT;

event.value = 1;

write(uinp_fd, &event, sizeof(event));

event.type = EV_SYN;

event.code = SYN_REPORT;

event.value = 0;

write(uinp_fd, &event, sizeof(event));

// Report BUTTON CLICK - RELEASE event

memset(&event, 0, sizeof(event));

gettimeofday(&event.time, NULL);

event.type = EV_KEY;

event.code = BTN_LEFT;

event.value = 0;

write(uinp_fd, &event, sizeof(event));

event.type = EV_SYN;

event.code = SYN_REPORT;

event.value = 0;

write(uinp_fd, &event, sizeof(event));

}

void send_a_button()

{

// Report BUTTON CLICK - PRESS event

memset(&event, 0, sizeof(event));

gettimeofday(&event.time, NULL);

event.type = EV_KEY;

event.code = KEY_A;

Dashboard January 2007 Issue

event.value = 1;

write(uinp_fd, &event, sizeof(event));

event.type = EV_SYN;

event.code = SYN_REPORT;

event.value = 0;

write(uinp_fd, &event, sizeof(event));

// Report BUTTON CLICK - RELEASE event

memset(&event, 0, sizeof(event));

gettimeofday(&event.time, NULL);

event.type = EV_KEY;

event.code = KEY_A;

event.value = 0;

write(uinp_fd, &event, sizeof(event));

event.type = EV_SYN;

event.code = SYN_REPORT;

event.value = 0;

write(uinp_fd, &event, sizeof(event));

}

/* This function will open the uInput device. Please make

sure that you have inserted the uinput.ko into kernel. */

int main()

{

// Return an error if device not found.

if (setup_uinput_device() < 0)

{

printf("Unable to find uinput device/n");

return -1;

}

send_a_button(); // Send a "A" key

send_click_events(); // Send mouse event

/* Destroy the input device */

ioctl(uinp_fd, UI_DEV_DESTROY);

/* Close the UINPUT device */

close(uinp_fd);

}

原文出处 : http://blog.csdn.net/outblue/article/details/5288760

android uinput 按键_linux 虚拟输入设备(uinput)模拟鼠标和键盘的使用方法相关推荐

  1. Android 使用 sendevent 模拟鼠标和键盘事件

    Android 使用 sendevent 模拟鼠标和键盘事件 模拟原理是对驱动发送消息,就是linux里面的input子系统. 命令格式: sendevent /dev/input/eventX [t ...

  2. C++实现鼠标控制 封装常见的模拟鼠标、键盘的操作函数

    API 或 MFC 视窗程序 里 有 函数, 例如 API 函数 设位置: BOOL SetCursorPos( int x, int y); 参数是屏幕坐标x,y 头文件 Winuser.h 链接库 ...

  3. python模拟键盘输入视频_python教程-模拟鼠标和键盘输入

    大家可能知道,有的情形下,如果我们需进行自动化操作的应用程序不提供相对应的的接口,就难以通过Python直接调用API来做到自动化.在此类情形下,Python也并非压根没有办法,我们可以通过模拟键盘和 ...

  4. DirectX11 输入设备——DirectInput检测鼠标、键盘状态

    DirectX11 输入设备--DirectInput检测鼠标.键盘状态 1. 什么是DirectInput? 由于windows应用程序的消息机制,Windows 成为了在应用程序和硬件之间的一堵无 ...

  5. C# WPF 中用代码模拟鼠标和键盘的操作

    C# WPF 中用代码模拟鼠标和键盘的操作 原文:C# WPF 中用代码模拟鼠标和键盘的操作 原文地址 C#开发者都知道,在Winform开发中,SendKeys类提供的方法是很实用的.但是可惜的是, ...

  6. 控制台模拟鼠标、键盘操作

    控制台模拟鼠标.键盘操作 模拟鼠标.键盘操作,能让命令行顿然强大,想想,制作批处理版屏幕键盘等都不在话下(已制作过,效果很不错).虽然这也跟CUI无关.本教程教会你如何让命令行模拟鼠标.键盘的操作. ...

  7. java模拟器键盘输入_Java模拟鼠标和键盘输入

    用途 在电脑(Windows/Mac)上模拟鼠标和键盘输入 Mac运行需要打开相关权限,详见文末说明. 效果图 代码 import java.awt.*; import java.awt.event. ...

  8. Python - Python 模拟鼠标和键盘进行基本操作

    文章目录 Python - Python 模拟鼠标和键盘进行基本操作 相关内容:Python 实用内容 1.安装模块pyautogui 2.基本操作 Python - Python 模拟鼠标和键盘进行 ...

  9. python驱动级模拟按键 检测_py库: pyautogui (自动测试模块,模拟鼠标、键盘动作)...

    PyAutoGUI 是一个人性化的跨平台 GUI 自动测试模块 pyautogui 库 2017-10-4 pip install pyautogui python pip.exe install p ...

最新文章

  1. 自己架设windows升级服务器
  2. 【百度地图API】小学生找哥哥——小学生没钱打车,所以此为公交查询功能
  3. python入门指南 小说-Python 入门指南
  4. 兼顾稳定和性能,58大数据平台的技术演进与实践
  5. java怎么把随机数放入数组_Java学习:集合的使用与数组的区别
  6. stl-vector
  7. 如是院长说:买不起房就多买两套,大家怎么看
  8. 用C++实现简单随机二元四则运算
  9. 小程序中input标签没有反应_鸢尾花预测:如何创建机器学习Web应用程序?
  10. leetcode79. 单词搜索 网格地图搜索+回溯经典写法啦
  11. NHibernate3.0剖析:Query篇之NHibernate.Linq标准查询
  12. RocketMq 消费消息的两种方式 pull 和 push
  13. 利用Pandas库进行简单的数据规整
  14. No goals have been specified for this build. You must specify a valid lifecycle phase or a goal.....
  15. C++之unique_ptr
  16. 隐藏你不想要的:Bartender 4 for Mac菜单栏应用管理软件
  17. php号码归属地查询源码,手机号码归属地查询
  18. 使用Air724UG模块拍摄照片并上传至云服务器
  19. 问题处理——无法导航到插入符号下的符号
  20. 启发式算法 Heuristic Algorithm

热门文章

  1. mysql user表添加记录_《MySQL数据操作与查询》- 返校复习课练习题,创建数据库user_system,创建数据表user及user_ext...
  2. str_pad函数php,str_pad
  3. js判断ipad还是安卓_还考虑iPad?荣耀平板V6麒麟985支持5G才3000多元
  4. python文件指针放在文件的开头_将文件指针倒带到上一个lin的开头
  5. php addslashes 数组,用递归addslashes函数转义数组
  6. flask之url_for()函数解析
  7. mysql训练逻辑的题_全国计算机等级考试二级教程mysql复习题练习题.docx
  8. 怎样去除excel表中的虚线_Excel教程:F1键强大的功能,你会用吗?
  9. android应用程序开发_Kotlin与Flutter:Android跨平台应用程序开发,到底选择哪个?...
  10. 算法分析 载货问题_协会发布 | 汽车市场走势分析及2021年预测报告