【问题标题】:uinput emulate LWIN + S under wayland/westonuinput 在wayland/weston 下模拟 LWIN + S
【发布时间】:2019-09-18 12:05:28
【问题描述】:

我正在尝试在 wayland 下模拟左 Windows 键(超级)+ 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 键的正确键定义是什么?上面的代码对于发送一个键 combination(一次按下 2 个键)是否有效?甚至有可能实现我想要的吗?我在系统中有完全的 root 访问权限。

【问题讨论】:

    标签: c++ c wayland uinput


    【解决方案1】:

    我实际上是在发布此消息几分钟后发现的!它有效!

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-12-26
      • 1970-01-01
      • 2015-09-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-28
      相关资源
      最近更新 更多