Python3 模拟鼠标点击
1. 安装 pywin32
适用于 Microsoft Windows 的 Python 扩展 提供对大部分 Win32 API 的访问、创建和使用 COM 对象的能力以及 Pythonwin 环境。
Project description
Python extensions for Microsoft Windows Provides access to much of the Win32 API,
the ability to create and use COM objects, and the Pythonwin environment.
安装:
pip install pywin32
或
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple pywin32
- 使用国内镜像
2. 安装 py-Hook
pyHook 包为 Windows 中的全局鼠标和键盘事件提供回调。Python 应用程序为用户输入事件(例如鼠标左键按下、鼠标左键按下、按键按下等)注册事件处理程序,并设置键盘和/或鼠标挂钩。底层 C 库报告诸如事件时间、发生事件的窗口名称、事件值、任何键盘修饰符等信息。
Project description
The pyHook package provides callbacks for global mouse and keyboard events in
Windows. Python applications register event handlers for user input events such as
left mouse down, left mouse up, key down, etc. and set the keyboard and/or mouse
hook. The underlying C library reports information like the time of the event, the
name of the window in which the event occurred, the value of the event, any keyboard
modifiers, etc.
安装:
由于 PytHook 没有与 python3.9 相对应的版本
直接下再PyHook3, 例: PyHook3-1.6.1.tar.gz. (网 址:https://pypi.org/project/PyHook3/#files)
-
方法 1
下载成功后, 将压缩包解压:
-
打开cmd
-
到达解压的文件目录中
-
python setup.py build
。 -
将生成的文件放到 C:\Python3.7\Lib\site-packages\ 中。
-
-
方法 2
下载成功后, 将压缩包解压:
-
打开cmd
-
到达解压的文件目录中
-
python setup.py build
-
python setup.py install
-
3. 安装 PyUserInput
一个
简单易用的python中跨平台控制鼠标和键盘的模块。
Project description
PyUserInput
A module for cross-platform control of the mouse and keyboard in python that is
simple to use.
安装:
pip install PyUserInput
或
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple PyUserInput
- 使用国内镜像
测试
from mouse import PyMouse
x = PyMouse()
txt = x.position() # 获取当前坐标的位置
print(a)
x.move(50, 50) # 鼠标移动到(x,y)位置
x.click(50, 50) # 鼠标左击(x,y)
x.click(50, 50, 2) # 鼠标右击(x,y)
模块详情:https://pypi.org/
参考: https://www.cnblogs.com/newmiracle/p/12409037.html