Linux 使用uinput创建虚拟input设备
linux-dash
A beautiful web dashboard for Linux
项目地址:https://gitcode.com/gh_mirrors/li/linux-dash
免费下载资源
·
参考了这里: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;
}
GitHub 加速计划 / li / linux-dash
10.39 K
1.2 K
下载
A beautiful web dashboard for Linux
最近提交(Master分支:2 个月前 )
186a802e
added ecosystem file for PM2 4 年前
5def40a3
Add host customization support for the NodeJS version 4 年前
更多推荐
已为社区贡献6条内容
所有评论(0)