uinput 在 wayland/weston 下模拟 LWIN + S
uinput emulate LWIN + S under wayland/weston
我正在尝试模拟 wayland 下的左窗口键 (Super( + S 的组合键以自动执行屏幕截图(此组合键可以正确启动 weston-screenshooter,将屏幕截图保存到主目录,但无法从终端执行(。 我发现了使用 uinput 来做到这一点,我找到了一些例子并提出了这个:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <linux/input.h>
#include <linux/uinput.h>
#define die(str) do {
perror(str);
exit(EXIT_FAILURE);
} while(0)
void emitEvent(int fd, uint16_t type, uint16_t code, int val)
{
struct input_event ie;
ie.type = type;
ie.code = code;
ie.value = val;
/* timestamp values below are ignored */
ie.time.tv_sec = 0;
ie.time.tv_usec = 0;
write(fd, &ie, sizeof(ie));
}
#define UI_DEV_SETUP _IOW(UINPUT_IOCTL_BASE, 3, struct uinput_setup)
int main(int argc, char *argv[])
{
int fd;
struct uinput_user_dev uidev;
fd = open("/dev/uinput", O_WRONLY | O_NONBLOCK);
if(fd < 0)
die("error: open");
if(ioctl(fd, UI_SET_EVBIT, EV_KEY) < 0)
die("error: ioctl");
if(ioctl(fd, UI_SET_KEYBIT, KEY_S) < 0)
die("error: ioctl");
if(ioctl(fd, UI_SET_KEYBIT, 133) < 0)
die("error: ioctl");
memset(&uidev, 0, sizeof(uidev));
snprintf(uidev.name, UINPUT_MAX_NAME_SIZE, "uinput-sample");
uidev.id.bustype = BUS_USB;
uidev.id.vendor = 0x1234; // random stuff
uidev.id.product = 0x6433;
uidev.id.version = 1;
if(write(fd, &uidev, sizeof(uidev)) < 0)
die("error: write");
if(ioctl(fd, UI_DEV_CREATE) < 0)
die("error: ioctl");
sleep(1);
// srand(time(NULL));
emitEvent(fd, EV_KEY, 133, 1);
emitEvent(fd, EV_KEY, KEY_S, 1);
emitEvent(fd, EV_SYN, SYN_REPORT, 0);
usleep(300);
emitEvent(fd, EV_KEY, KEY_S, 0);
emitEvent(fd, EV_KEY, 133, 0);
emitEvent(fd, EV_SYN, SYN_REPORT, 0);
sleep(1);
if(ioctl(fd, UI_DEV_DESTROY) < 0)
die("error: ioctl");
close(fd);
return 0;
}
我最大的问题是我不知道哪个KEY_是左赢/左超级键,键定义根本没有向我显示任何与 win 或超级或mod_s相关的内容,在 x11 上使用 xev 我发现 LWIN 应该是 133,但我认为这是不正确的,因为代码中的 KEY_S = 31,但在 xev 中它是 39。
运行应用程序时,我可以看到设备正在系统中注册:
[ 6278.405013] input: uinput-sample as /devices/virtual/input/input23
[14:02:17.556] event2 - [14:02:17.556] uinput-sample: [14:02:17.556] is tagged by udev as: Keyboard
[14:02:17.556] event2 - [14:02:17.557] uinput-sample: [14:02:17.557] device is a keyboard
[14:02:19.549] event2 - [14:02:19.550] uinput-sample: [14:02:19.550] device removed
但除此之外什么都没有发生。
左窗口键的正确键定义是什么?上面的代码对于发送组合键(一次按下 2 个键(是否有效?甚至有可能实现我想要的吗?我在系统中具有完全的根访问权限。
我实际上在发布此内容后几分钟就发现了它!它有效!
密钥代码KEY_LEFTMETA,我使用 evtest 包找到了它。截图制作成功。
相关文章:
- 如何使用Google Mock来模拟gettimeofday()
- G锁定铸造到基础上会释放模拟行为
- 有什么好的方法可以让系统调用代理允许在单元测试中进行模拟
- 落砂模拟碰撞检测C++和SFML
- 在gtest.中使用fff.h模拟系统API
- 谷歌模拟和覆盖关键字
- 用C#中的并集模拟C++嵌套结构
- 在同一模拟中使用静脉和静脉_ inet内容时出现运行时错误
- 在模拟器中使用并集来模拟CPU寄存器有多合适
- 我写了一个C++程序来模拟Enigma机器.我没有得到输出
- 如何模拟不同边数的骰子滚动?
- 模拟持久按键
- 使用SIR模型的疾病爆发模拟
- 在 c++ 中模拟输入并在 JAVA 中读取它?
- 转发变量参数列表以模拟 std::thread
- 如何在谷歌模拟中匹配 C 样式数组
- 如何使用兰德随机化模拟点击
- 模拟GPS数据,以便使用Qt与Traccar一起使用
- QKeyPress - 在Qt中模拟按键
- uinput 在 wayland/weston 下模拟 LWIN + S