Simulate Mouse Clicks on Python

℡╲_俬逩灬. 提交于 2019-11-30 01:21:12
Vlad

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

  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.

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

Example of sending a relative motion event and a left mouse click with 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()

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

An example of mine...

import pyautogui

pyautogui.FAILSAFE = False

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

you might find this helpful:

http://www.eventghost.org/

Good luck!

You can try to interface XTE program from the Python script.

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

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:

>>> 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.

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

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

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).

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!