Linux 通过写入 /dev/input/mice 来控制鼠标
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20595716/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Control mouse by writing to /dev/input/mice
提问by Vishal
I am using Ubuntu 12.04. For one of my applications I require to control the mouse in software using a script.
我正在使用 Ubuntu 12.04。对于我的一个应用程序,我需要使用脚本在软件中控制鼠标。
I understand that the mouse device is /dev/input/mice
. If I do a cat /dev/input/mice
and then move my mouse, I see a lot of output being dumped to the screen.
我知道鼠标设备是/dev/input/mice
. 如果我执行 acat /dev/input/mice
然后移动鼠标,我会看到大量输出被转储到屏幕上。
Now I wish to remove the mouse, and have a script which writes to /dev/input/mice
in order to control the mouse pointer
现在我想移除鼠标,并有一个脚本/dev/input/mice
来控制鼠标指针
Please help me with commands for the following:
(1) Perform a left click
(2) Perform a right click
(3) Move the mouse from one location to another.
请帮助我
执行以下命令:(1) 执行左键单击
(2) 执行右键单击
(3) 将鼠标从一个位置移动到另一个位置。
Kindly note that I am looking for a shell script solution, rather than a C/C++ solution.
请注意,我正在寻找 shell 脚本解决方案,而不是 C/C++ 解决方案。
采纳答案by Jappie Kerk
this is not trough the file you mentioned, but its way quicker to use this tool instead of decypering the dump of that file. And it does everything you want in bash.
这不是通过您提到的文件,而是使用此工具更快的方式,而不是破解该文件的转储。它可以在 bash 中完成您想要的一切。
xdotool does the trick in my terminal.
thisis the package site for ubuntu.
you probably can install it trough
xdotool 在我的终端中解决了这个问题。
这是 ubuntu 的软件包站点。你可能可以安装它
# apt-get install xdotool
I could just emerge it on gentoo without adding any repositories.
the tool works fairly simple:
我可以在 gentoo 上出现它而不添加任何存储库。
该工具的工作相当简单:
#! /bin/bash
# move the mouse x y
xdotool mousemove 1800 500
# left click
xdotool click 1
# right click
xdotool click 3
回答by Raydel Miranda
If you are brave, and you don't want to depend on any third party tool, you should use Xlib. Documentation can be found here. Also you can try python-xlibif you don't want to mess with C/C++.
如果你很勇敢,不想依赖任何第三方工具,你应该使用Xlib。文档可以在这里找到。如果您不想弄乱 C/C++,也可以尝试python-xlib。
Check this threadfor an example (C/C++).
检查此线程以获取示例(C/C++)。
This is an example of a program that receives a coord and simulates a mouse click at that position.
这是一个接收坐标并在该位置模拟鼠标单击的程序示例。
#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;
}
回答by tuxdna
You can capture the events using od
tool from the /dev/input/mice
and then replay them once you have decoded the sequence.
您可以使用 中的od
工具捕获事件/dev/input/mice
,然后在解码序列后重放它们。
# 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
For this you can take help of the python code here:
为此,您可以在此处获取 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
Once you have that, you can encode it back into a 3 byte sequence for each mouse move.
一旦你有了它,你可以为每次鼠标移动将它编码回一个 3 字节的序列。
To encode the binary values using bash
you can refer to this question: Passing binary data as arguments in bash
要使用对二进制值进行编码,bash
您可以参考这个问题:在 bash 中将二进制数据作为参数传递
HoweverI tried and writing to /dev/input/mice
does not work.
但是我试过了,写信/dev/input/mice
不起作用。
The reason is that, this file is only giving a streams of events for you which have already happened. So there must be some other way to inject such events.
原因是,该文件只为您提供已经发生的事件流。所以一定有其他的方式来注入这样的事件。
回答by nopper
There's an appropriate module for emulating mouse, keyboards and other kind of input devices in linux. The module is called uinput
that stands for user-space input.
有一个合适的模块用于在 linux 中模拟鼠标、键盘和其他类型的输入设备。该模块被称为uinput
代表用户空间输入。
You can easily create virtual devices that are controlled through software. For example if you know Python you can set up a virtual mouse through the use of python-uinputand issue simple commands such as move here, click there. For example to move your mouse, accordingly to the documentation:
您可以轻松创建通过软件控制的虚拟设备。例如,如果你知道Python中,你可以建立一个虚拟的鼠标在使用中的python-uinput和发出简单的指令,如移动位置,点击那里。例如,根据文档移动鼠标:
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)
I never used that binding though, but several years ago I created a mouse emulator that can be controlled through keyboard for my iBook that came with a broken touchpad. You can take a look at my codeto have a reference in order to implement the mouse/touchpad movement operation.
虽然我从未使用过这种绑定,但几年前我为我的 iBook 创建了一个可以通过键盘控制的鼠标模拟器,我的 iBook 带有损坏的触摸板。你可以看看我的代码有一个参考,以实现鼠标/触摸板移动操作。
回答by Ruud Helderman
It was this hyperlink in one of the earlier posts that put me on the right track: How to control mouse movement in Linux
正是早期帖子中的一个超链接让我走上了正轨: 如何在 Linux 中控制鼠标移动
Helped by information from various other places, I managed to port the C sample code to a Bash script. Here's a PoC that moves the mouse cursor 100 pixels to the right:
在来自其他地方的信息的帮助下,我设法将 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
Caveats:
注意事项:
- You will have to adjust event8 to whatever is your system's mouse input device. Use this command to find out: cat /proc/bus/input/devices
- You need sufficient permission (possibly root) to write to the input device.
- I assumed little-endian processor architecture (hence the byte reversal with Perl).
- 您必须将 event8 调整为您系统的鼠标输入设备。使用这个命令找出: cat /proc/bus/input/devices
- 您需要足够的权限(可能是 root)才能写入输入设备。
- 我假设了小端处理器架构(因此使用 Perl 进行字节反转)。
回答by Omidreza Bagheri
In addition to use /dev/input/mice to control your mouse, you can use command 'xte' from package 'xautomation'.
除了使用 /dev/input/mice 来控制鼠标之外,您还可以使用来自包“xautomation”的命令“xte”。
apt-get install xautomation
As an example, the following command can be noted:
例如,可以注意到以下命令:
xte 'mousemove 400 100'
So, the mouse pointer moves to the specific location of the screen. As an other example, we have:
因此,鼠标指针移动到屏幕的特定位置。作为另一个例子,我们有:
xte 'mouseclick 1'
that click the left button of mouse (1: left click, 2: middle click, 3: right click).
单击鼠标左键(1:左键单击,2:中键单击,3:右键单击)。
回答by chnyda
You can create a virtual mouse.
您可以创建一个虚拟鼠标。
As other mentioned you can use pre-made tools. But it might be funny to play with uinput.
正如其他人提到的,您可以使用预制工具。但是玩 uinput 可能很有趣。
http://thiemonge.org/getting-started-with-uinput
http://thiemonge.org/getting-started-with-uinput
Basically you just have to create a virtual device. And write in /dev/uinput.
基本上你只需要创建一个虚拟设备。并写入/dev/uinput。