首页 > 解决方案 > uinput 在wayland/weston 下模拟 LWIN + S

问题描述

我正在尝试在 Wayland 下模拟 Left Windows Key (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/Left Super 键,键定义根本没有向我显示与 win 或 super 或 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

但没有其他事情发生。

左 Windows 键的正确键定义是什么?上面的代码对于发送组合键(一次按下 2 个键)是否有效?甚至有可能实现我想要的吗?我在系统中有完全的 root 访问权限。

标签: c++cwaylanduinput

解决方案


我实际上在发布后几分钟就发现了它!它有效!

关键代码是 KEY_LEFTMETA,我使用 evtest 包找到了它。截图成功。


推荐阅读