linux hid 键盘鼠标设备开发

1. 运行linux的设备作为hid设备,需要连接的usb为从属设备,常见的可使用usb otg

2. 可以使用configfs + gadget,挂载及配置后,即可通过读写文件描述符实现设备功能,gadget提供的一些功能举例可以在官方文档里查看

3. gadget提供的设备配置在内核驱动里实现,官方版本中开放了部分参数,可通过挂载后读写文件描述符配置,hid驱动为 kernel/drivers/usb/gadget/hid.c,配置可以参考Linux USB HID gadget driver — The Linux Kernel documentation

4. 如果需要多个设备共用,目前找到的方法都是修改内核驱动,例如:DM816X 实现 USB HID Gadget 鼠标键盘功能【转】 - Sky&Zhang - 博客园 (cnblogs.com)

Linux3.x——USB Gadget HID keyboard + Mass storage_gadget masstorage-CSDN博客

5. 挂载操作参考​​​​​​​Linux USB gadget configured through configfs — The Linux Kernel documentation

【精选】LINUX嵌入式模拟键盘 2022-05-21_linux usb gadget configfs 键盘-CSDN博客

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Linux中实现读写HID模拟鼠标可以通过以下步骤来完成: 1. 首先,需要使用hidraw设备来读写HID设备HID设备Linux中通常以hidraw设备的形式出现。可以通过以下命令查看系统中的hidraw设备: ```shell ls /dev/hidraw* ``` 2. 打开hidraw设备以进行读写操作。可以使用C语言编写一个程序来打开hidraw设备,并使用read和write函数来读写数据。以下是一个简单的示例程序: ```c #include <stdio.h> #include <stdlib.h> #include <fcntl.h> #include <unistd.h> #include <linux/hidraw.h> int main() { int fd; char buf[8]; fd = open("/dev/hidraw0", O_RDWR); if (fd < 0) { perror("Unable to open hidraw device"); exit(1); } // 读取数据 if (read(fd, buf, sizeof(buf)) < 0) { perror("Unable to read from hidraw device"); exit(1); } // 写入数据 if (write(fd, buf, sizeof(buf)) < 0) { perror("Unable to write to hidraw device"); exit(1); } close(fd); return 0; } ``` 3. 解析HID报文。HID报文通常是由一系列的字节组成的。根据HID规范,可以解析报文中的每个字节来获取鼠标的相关信息,例如鼠标的坐标、按键状态等。 4. 模拟鼠标操作。根据解析得到的鼠标信息,可以使用X11或者uinput来模拟鼠标操作。X11是Linux下的图形系统,可以使用XTest库来模拟鼠标操作。uinput是Linux内核提供的一个虚拟输入设备接口,可以使用uinput库来模拟鼠标操作。 以下是一个使用XTest库来模拟鼠标操作的示例程序: ```c #include <stdio.h> #include <X11/Xlib.h> #include <X11/extensions/XTest.h> int main() { Display *display; Window root; XEvent event; display = XOpenDisplay(NULL); if (display == NULL) { fprintf(stderr, "Unable to open display\n"); return 1; } root = DefaultRootWindow(display); // 模拟鼠标移动 XTestFakeMotionEvent(display, -1, 100, 100, CurrentTime); // 模拟鼠标点击 XTestFakeButtonEvent(display, 1, True, CurrentTime); XTestFakeButtonEvent(display, 1, False, CurrentTime); XFlush(display); XCloseDisplay(display); return 0; } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值