在 Python 上模拟鼠标点击

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3545230/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 11:40:08  来源:igfitidea点击:

Simulate Mouse Clicks on Python

pythonlinuxmousecursorwiimote

提问by dbdii407

I'm currently in the process of making my Nintendo Wiimote (Kinda sad actually) to work with my computer as a mouse. I've managed to make the nunchuk's stick control actually move the mouse up and down, left and right on the screen! This was so exciting. Now I'm stuck.

我目前正在让我的 Nintendo Wiimote(实际上有点难过)作为鼠标与我的电脑一起工作。我已经设法让双节棍的操纵杆控制实际上在屏幕上上下左右移动鼠标!这太令人兴奋了。现在我被困住了。

I want to left/right click on things via python when I press A, When I went to do a search, All it came up with was tkinter?

当我按 A 时,我想通过 python 左/右单击事物,当我去搜索时,它想到的只是 tkinter?

So my question is, What do I call to make python left/right click on the desktop, and if it's possible, maybe provide a snippet?

所以我的问题是,我应该调用什么来使 python 在桌面上左/右单击,如果可能的话,也许提供一个片段?

Thank you for your help!

感谢您的帮助!

NOTE: I guess I forgot to mention that this is for Linux.

注意:我想我忘了提到这是针对 Linux 的。

回答by reckoner

you might find this helpful:

你可能会发现这很有帮助:

http://www.eventghost.org/

http://www.eventghost.org/

Good luck!

祝你好运!

回答by Agnius Vasiliauskas

You can try to interface XTEprogram from the Python script.

您可以尝试从 Python 脚本接口XTE程序。

回答by Epeli

Open your terminal and goto cd /usr/share/pyshared/twisted/protocols/mice
may this __init__.pymouseman.pypython script will work for you,check them out.

打开你的终端并转到cd /usr/share/pyshared/twisted/protocols/mice
可能这个__init__.pymouseman.pypython脚本对你有用,检查出来。

回答by gvalkov

The evdevpackage provides bindings to parts of the input handling subsystem in Linux. It also happens to include a pythonic interface to uinput.

了evdev包提供绑定在Linux中输入处理子系统的部件。它也碰巧包含一个到 uinput 的 pythonic 接口。

Example of sending a relative motion event and a left mouse clickwith evdev:

使用evdev发送相对运动事件和鼠标左键单击的示例:

from evdev import UInput, ecodes as e

capabilities = {
    e.EV_REL : (e.REL_X, e.REL_Y), 
    e.EV_KEY : (e.BTN_LEFT, e.BTN_RIGHT),
}

with UInput(capabilities) as ui:
    ui.write(e.EV_REL, e.REL_X, 10)
    ui.write(e.EV_REL, e.REL_Y, 10)
    ui.write(e.EV_KEY, e.BTN_LEFT, 1)
    ui.syn()

回答by Vlad

You can use PyMousewhich has now merged with PyUserInput. I installed it via pip:

您可以使用PyMouse现已合并PyUserInput。我是通过 pip 安装的:

  1. apt-get install python-pip

  2. pip install pymouse

  1. apt-get install python-pip

  2. pip install pymouse

In some cases it used the cursor and in others it simulated mouse events without the cursor.

在某些情况下,它使用光标,而在其他情况下,它在没有光标的情况下模拟鼠标事件。

from pymouse import PyMouse

m = PyMouse()
m.position() #gets mouse current position coordinates
m.move(x,y)
m.click(x,y) #the third argument "1" represents the mouse button
m.press(x,y) #mouse button press
m.release(x,y) #mouse button release

You can also specify which mouse button you want used. Ex left button:

您还可以指定要使用的鼠标按钮。前左键:

m.click(x,y,1)

Keep in mind, on Linux it requires Xlib.

请记住,在 Linux 上它需要 Xlib。

回答by sdaau

I didn't see this mentioned, so here it goes - there is also python-dogtail; see:

我没有看到提到这一点,所以在这里 - 还有python-dogtail; 看:

It requires "Enable assistive technologies" in the Gnome Desktop - but can in principle obtain e.g. names of GUI buttons of an application, and allow virtual clicks on them (rather than via x/y coordinates).

它需要在 Gnome 桌面中“启用辅助技术”——但原则上可以获得例如应用程序 GUI 按钮的名称,并允许虚拟点击它们(而不是通过 x/y 坐标)。

回答by Al Sweigart

You can install the PyAutoGUI GUI automation module from PyPI (run pip install pyautogui) and then call the pyautogui.click()to click on a certain X and Y coordinates of the screen:

您可以从 PyPI 安装 PyAutoGUI GUI 自动化模块(运行pip install pyautogui),然后调用pyautogui.click()以单击屏幕的某个 X 和 Y 坐标:

>>> import pyautogui
>>> pyautogui.click(50, 100)
>>> pyautogui.moveTo(200, 200)

PyAutoGUI works on Windows, Mac, and Linux, and on Python 2 and 3. It also can emulate the keyboard, do mouse drags, take screenshots, and do simple image recognition of the screenshots.

PyAutoGUI 可在 Windows、Mac 和 Linux 以及 Python 2 和 3 上运行。它还可以模拟键盘、拖动鼠标、截取屏幕截图以及对屏幕截图进行简单的图像识别。

Full docs are at https://pyautogui.readthedocs.org/

完整文档位于https://pyautogui.readthedocs.org/

回答by Kishore Kashyap

PyAutoGui works superb.. Thanks to Al Sweigart...

PyAutoGui 工作得很好......感谢 Al Sweigart......

An example of mine...

我的一个例子...

import pyautogui

pyautogui.FAILSAFE = False

for x in range(555, 899):
    pyautogui.moveTo(x, x)