【问题标题】:Control mouse by writing to /dev/input/mice通过写入 /dev/input/mice 来控制鼠标
【发布时间】:2014-01-02 22:42:39
【问题描述】:

我使用的是 Ubuntu 12.04。对于我的一个应用程序,我需要使用脚本在软件中控制鼠标。

我知道鼠标设备是/dev/input/mice。如果我执行cat /dev/input/mice 然后移动鼠标,我会看到很多输出被转储到屏幕上。

现在我想移除鼠标,并有一个脚本写入/dev/input/mice 以控制鼠​​标指针

请帮助我执行以下命令:
(1) 执行左键单击
(2) 执行右键
(3) 将鼠标从一个位置移动到另一个位置。

请注意,我正在寻找一个 shell 脚本解决方案,而不是 C/C++ 解决方案。

【问题讨论】:

标签: linux shell ubuntu mouse


【解决方案1】:

这不是通过您提到的文件,而是使用此工具而不是解密该文件的转储的方式更快。它可以在 bash 中完成您想要的一切。

xdotool 在我的终端中发挥作用。
this 是 ubuntu 的软件包站点。 你可能可以安装它

# apt-get install xdotool

我可以直接在 gentoo 上显示它而不添加任何存储库。
the tool works fairly simple:

#! /bin/bash
# move the mouse  x    y
xdotool mousemove 1800 500
# left click
xdotool click 1
# right click
xdotool click 3

found it here

【讨论】:

  • 其实我想要一个直接的解决方案,这样我就可以在 Android 设备上尝试同样的方法。您的解决方案使用单独的工具。我不能接受它作为正确答案。无论如何谢谢...
  • @vishal 这在您的问题中没有说明...您说您使用的是 ubuntu。无论如何,这个问题是关于 android 键输入的:stackoverflow.com/questions/18244693/… 还请注意,android 不使用此处所述的 x 服务器:stackoverflow.com/questions/4579573/android-graphics-internals 所以操作输入设备可能与在 ubuntu 上不同。 android 也不包含 gnu,它是大多数 linux 桌面发行版之上的层:gnu.org/philosophy/android-and-users-freedom.html 第 5 段
  • 实际上我同意我并不完全清楚。通过将问题提出为“写入 /dev/input/mice”,我希望该解决方案可以移植到 Android。感谢您的参考,它们为我提供了很多关于 Android 预期的线索。
  • 我喜欢这个答案,因为它非常简单,尽管它可能不是 100% 适合所提出的问题。其他答案非常有创意,但我只是尝试了 xdotool 并移动鼠标并单击所有工作正常!我可以使用它来登录通常需要我手动执行此操作的网站。 :)
【解决方案2】:

如果你很勇敢,不想依赖任何第三方工具,你应该使用 Xlib。可以在here 找到文档。如果你不想乱用 C/C++,也可以试试python-xlib

查看此thread 以获取示例 (C/C++)。

这是一个接收坐标并在该位置模拟鼠标点击的程序示例。

#include <X11/Xlib.h>
#include<stdio.h>
#include<unistd.h>
#include <stdlib.h>
#include <string.h>

#include <unistd.h>

#include <X11/Xlib.h>
#include <X11/Xutil.h>

void mouseClick(int button)
{
    Display *display = XOpenDisplay(NULL);

    XEvent event;

    if(display == NULL)
    {
        fprintf(stderr, "Errore nell'apertura del Display !!!\n");
        exit(EXIT_FAILURE);
    }

    memset(&event, 0x00, sizeof(event));

    event.type = ButtonPress;
    event.xbutton.button = button;
    event.xbutton.same_screen = True;

    XQueryPointer(display, RootWindow(display, DefaultScreen(display)), &event.xbutton.root, &event.xbutton.window, &event.xbutton.x_root, &event.xbutton.y_root, &event.xbutton.x, &event.xbutton.y, &event.xbutton.state);

    event.xbutton.subwindow = event.xbutton.window;

    while(event.xbutton.subwindow)
    {
        event.xbutton.window = event.xbutton.subwindow;

        XQueryPointer(display, event.xbutton.window, &event.xbutton.root, &event.xbutton.subwindow, &event.xbutton.x_root, &event.xbutton.y_root, &event.xbutton.x, &event.xbutton.y, &event.xbutton.state);
    }

    if(XSendEvent(display, PointerWindow, True, 0xfff, &event) == 0) fprintf(stderr, "Error\n");

    XFlush(display);

    usleep(100000);

    event.type = ButtonRelease;
    event.xbutton.state = 0x100;

    if(XSendEvent(display, PointerWindow, True, 0xfff, &event) == 0) fprintf(stderr, "Error\n");

    XFlush(display);

    XCloseDisplay(display);
}
int main(int argc,char * argv[]) {
    int i=0;
    int x , y;
    x=atoi(argv[1]);
    y=atoi(argv[2]);
    Display *display = XOpenDisplay(0);
    Window root = DefaultRootWindow(display);

    XWarpPointer(display, None, root, 0, 0, 0, 0, x, y);

    mouseClick(Button1);
    XFlush(display);


    XCloseDisplay(display);
    return 0;
}

【讨论】:

  • 这取决于X,它是一个第三方工具:)。与uinput 交谈是在与内核本身进行交互。另外,我完全为我的一个应用窃取了这个 sn-p!
  • @étale-cohomology,很高兴你能使用它!
【解决方案3】:

您可以使用/dev/input/mice 中的od 工具捕获事件,然后在解码序列后重放它们。

# cat /dev/input/mice | od -t x1 -w3
0000000 08 02 00
0000003 08 08 00
0000006 08 09 00
0000011 08 07 00
0000014 08 04 00
0000017 08 01 01
0000022 08 00 02
0000025 08 02 02

为此,您可以在此处获取 python 代码的帮助:

Get mouse deltas using Python! (in Linux)

L:0, M: 0, R: 0, x: -1, y: -1

L:0, M: 0, R: 0, x: 0, y: -1

L:0, M: 0, R: 0, x: 0, y: -1

L:0, M: 0, R: 0, x: 0, y: 2

L:0, M: 0, R: 0, x: 0, y: 1

L:0, M: 0, R: 0, x: 0, y: -1

L:0, M: 0, R: 0, x: 1, y: -1

一旦你有了它,你就可以为每次鼠标移动将它编码回一个 3 字节的序列。

要使用bash 对二进制值进行编码,您可以参考这个问题:Passing binary data as arguments in bash

但是我尝试写信给/dev/input/mice 不起作用。

原因是,这个文件只是给你一个已经发生的事件流。所以必须有一些其他的方式来注入这样的事件。

How to control mouse movement in linux?

【讨论】:

【解决方案4】:

在 linux 中有一个合适的模块用于模拟鼠标、键盘和其他类型的输入设备。该模块名为uinput,代表用户空间输入。

您可以轻松创建通过软件控制的虚拟设备。例如,如果您了解 Python,则可以通过使用 python-uinput 设置虚拟鼠标并发出简单的命令,例如 move hereclick there。例如,根据文档移动鼠标:

import uinput

device = uinput.Device([uinput.REL_X, uinput.REL_Y])

for i in range(20):
    device.emit(uinput.REL_X, 5)
    device.emit(uinput.REL_Y, 5)

虽然我从未使用过这种绑定,但几年前我为我的 iBook 创建了一个鼠标模拟器,它可以通过键盘控制,但触摸板损坏。你可以看看我的code有一个参考,以实现鼠标/触摸板的移动操作。

【讨论】:

  • +1 表示不依赖于 Xorg。为了让示例正常工作,我必须修改 uinput,在发出鼠标事件之前添加 time.sleep(1),然后以 root 身份运行脚本或将自己添加到 uinput 组
【解决方案5】:

正是之前一篇文章中的这个超链接让我走上了正轨: How to control mouse movement in Linux

在来自其他地方的信息的帮助下,我设法将 C 示例代码移植到了 Bash 脚本。这是一个将鼠标光标向右移动 100 像素的 PoC:

seconds=$(date +%s)
type=2      # EV_REL
code=0      # REL_X
value=100   # 100 pixels

printf '%08X%04X%04X%08X%08X\n' $value $code $type 0 $seconds | xxd -r -p | perl -0777e 'print scalar reverse <>' > /dev/input/event8

type=0      # EV_SYN
code=0      # SYN_REPORT
value=0

printf '%08X%04X%04X%08X%08X\n' $value $code $type 0 $seconds | xxd -r -p | perl -0777e 'print scalar reverse <>' > /dev/input/event8

注意事项:

  • 您必须将 event8 调整为系统的鼠标输入设备。使用此命令找出:cat /proc/bus/input/devices
  • 您需要足够的权限(可能是 root)才能写入输入设备。
  • 我采用 little-endian 处理器架构(因此使用 Perl 进行字节反转)。

【讨论】:

    【解决方案6】:

    除了使用 /dev/input/mice 来控制你的鼠标,你还可以使用包 'xautomation' 中的命令 'xte'。

    apt-get install xautomation
    

    例如,可以注意以下命令:

    xte 'mousemove 400 100'
    

    因此,鼠标指针移动到屏幕的特定位置。作为另一个例子,我们有:

    xte 'mouseclick 1'
    

    点击鼠标左键(1:左键,2:中键,3:右键)。

    【讨论】:

      【解决方案7】:

      您可以创建一个虚拟鼠标。

      如前所述,您可以使用预制工具。 但是玩 uinput 可能会很有趣。

      http://thiemonge.org/getting-started-with-uinput

      基本上你只需要创建一个虚拟设备。 并写入/dev/uinput。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-09-19
        • 2012-09-19
        • 1970-01-01
        • 1970-01-01
        • 2023-03-20
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多