使用 Python 控制鼠标
- 2025-01-13 08:53:00
- admin 原创
- 168
问题描述:
在 Windows 下,如何在 Python 中控制鼠标光标,即将其移动到某个位置并单击?
解决方案 1:
安装pywin32(我的情况是 pywin32-214.win32-py2.6.exe)后在 WinXP、Python 2.6(也测试了 3.x)上进行了测试:
import win32api, win32con
def click(x,y):
win32api.SetCursorPos((x,y))
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN,x,y,0,0)
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP,x,y,0,0)
click(10,10)
解决方案 2:
尝试使用PyAutoGUI模块。它是多平台的。
pip install pyautogui
所以:
import pyautogui
pyautogui.click(100, 100)
它还具有其他特点:
import pyautogui
pyautogui.moveTo(100, 150)
pyautogui.moveRel(0, 10) # move mouse 10 pixels down
pyautogui.dragTo(100, 150)
pyautogui.dragRel(0, 10) # drag mouse 10 pixels down
这比经历所有的 win32con 内容要容易得多。
解决方案 3:
您可以使用win32api
或ctypes
模块来使用 win32 api 来控制鼠标或任何 gui
这是一个使用 win32api 控制鼠标的有趣示例:
import win32api
import time
import math
for i in range(500):
x = int(500+math.sin(math.pi*i/100)*500)
y = int(500+math.cos(i)*100)
win32api.SetCursorPos((x,y))
time.sleep(.01)
使用 ctypes 的点击:
import ctypes
# see http://msdn.microsoft.com/en-us/library/ms646260(VS.85).aspx for details
ctypes.windll.user32.SetCursorPos(100, 20)
ctypes.windll.user32.mouse_event(2, 0, 0, 0,0) # left down
ctypes.windll.user32.mouse_event(4, 0, 0, 0,0) # left up
解决方案 4:
自2022 年起,您可以使用鼠标:
import mouse
mouse.move("500", "500")
mouse.click() # default to left click
# mouse.right_click()
# mouse.double_click(button='left')
# mouse.double_click(button='right')
# mouse.press(button='left')
# mouse.release(button='left')
完整的 Api 文档
特征
所有鼠标设备上的全局事件挂钩(无论焦点如何都捕获事件)。
监听并发送鼠标事件。
适用于Windows 和 Linux(需要 sudo)。
纯 Python,无需编译 C 模块。
零依赖。安装和部署非常简单,只需复制文件即可。
Python 2 / 3
包括高级 API(例如录制和播放)。
在单独的线程中自动捕获事件,不会阻塞主程序。
已测试并记录。
安装
视窗:
pip install mouse
Linux:
sudo pip install mouse
解决方案 5:
另一个选择是使用跨平台的AutoPy 包。此包有两种不同的鼠标移动选项:
此代码片段将立即将光标移动到位置 (200,200):
import autopy
autopy.mouse.move(200,200)
如果您希望光标在屏幕上明显移动到给定位置,则可以使用 smooth_move 命令:
import autopy
autopy.mouse.smooth_move(200,200)
解决方案 6:
Linux
from Xlib import X, display
d = display.Display()
s = d.screen()
root = s.root
root.warp_pointer(300,300)
d.sync()
来源:Python 鼠标移动(5 行代码)(仅限 Linux)。
解决方案 7:
查看跨平台 PyMouse:https://github.com/pepijndevos/PyMouse/
解决方案 8:
Pynput是我发现的最好的解决方案,适用于 Windows 和 Mac。编程超级简单,而且运行良好。
例如,
from pynput.mouse import Button, Controller
mouse = Controller()
# Read pointer position
print('The current pointer position is {0}'.format(
mouse.position))
# Set pointer position
mouse.position = (10, 20)
print('Now we have moved it to {0}'.format(
mouse.position))
# Move pointer relative to current position
mouse.move(5, -5)
# Press and release
mouse.press(Button.left)
mouse.release(Button.left)
# Double click; this is different from pressing and releasing
# twice on Mac OSX
mouse.click(Button.left, 2)
# Scroll two steps down
mouse.scroll(0, 2)
解决方案 9:
快捷而简单的功能,可clicks
在 Windows 7 上使用ctypes
库随时随地单击鼠标左键。无需下载。
import ctypes
SetCursorPos = ctypes.windll.user32.SetCursorPos
mouse_event = ctypes.windll.user32.mouse_event
def left_click(x, y, clicks=1):
SetCursorPos(x, y)
for i in xrange(clicks):
mouse_event(2, 0, 0, 0, 0)
mouse_event(4, 0, 0, 0, 0)
left_click(200, 200) #left clicks at 200, 200 on your screen. Was able to send 10k clicks instantly.
解决方案 10:
另一种选择是鼠标库,我个人使用它,因为它相对简单且跨平台。
使用方法如下:
import mouse
# move 100 right and 100 down with a duration of 0.5 seconds
mouse.move(100, 100, absolute=False, duration=0.5)
# left click
mouse.click('left')
# right click
mouse.click('right')
来源:如何使用 Python 控制鼠标
解决方案 11:
接受的答案对我有用,但它不稳定(有时点击不会注册),所以我添加了一个额外的MOUSEEVENTF_LEFTUP。然后它就可以可靠地工作了
import win32api, win32con
def click(x,y):
win32api.SetCursorPos((x,y))
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP,x,y,0,0)
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN,x,y,0,0)
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP,x,y,0,0)
click(10,10)
解决方案 12:
import ctypes
from time import sleep
SetCursorPos = ctypes.windll.user32.SetCursorPos
print("Woohoo!
Take Rest!
Mouse pointer will keep moving!
Press ctrl+c to stop...!")
while True:
SetCursorPos(300, 300)
sleep(2)
SetCursorPos(500, 500)
sleep(4)
解决方案 13:
如果您需要玩游戏。正如这篇文章https://www.learncodebygaming.com/blog/pyautogui-not-working-use-directinput中所述,一些游戏(如 Minecraft 或 Fortnite)有自己的注册鼠标/键盘事件的方式。控制鼠标和键盘事件的方法是使用全新的PyDirectInput 库。他们的 github 存储库是https://github.com/learncodebygaming/pydirectinput,并且有很多很棒的信息。
这是一个执行鼠标循环和点击的快速代码:
import pydirectinput # pip install pydirectinput
pydirectinput.moveTo(0, 500)
pydirectinput.click()
解决方案 14:
非常简单 1-安装包:
pip install mouse
2-将库添加到项目:
import mouse
3- 例如使用它:
mouse.right_click()
在此 URL 中描述了您可以使用的所有功能:
https://github.com/boppreh/mouse
解决方案 15:
如果您想移动鼠标,请使用以下命令:
import pyautogui
pyautogui.moveTo(x,y)
如果您想点击,请使用以下命令:
import pyautogui
pyautogui.click(x,y)
如果你没有pyautogui
安装,你必须将python附加到CMD。转到CMD并输入:pip install pyautogui
这将安装pyautogui
Python 2.x。
对于 Python 3.x,您可能必须使用pip3 install pyautogui
或python3 -m pip install pyautogui
。
解决方案 16:
在屏幕上随机移动鼠标
它将根据您的屏幕分辨率在屏幕上随机移动鼠标。检查下面的代码。
pip install pyautogui
使用此命令安装。
import pyautogui
import time
import random as rnd
#calculate height and width of screen
w, h = list(pyautogui.size())[0], list(pyautogui.size())[1]
while True:
time.sleep(1)
#move mouse at random location in screen, change it to your preference
pyautogui.moveTo(rnd.randrange(0, w),
rnd.randrange(0, h))#, duration = 0.1)
解决方案 17:
尝试使用 pyautogui,简单而且还可以模拟在 keaboard 上按键
解决方案 18:
尝试 Clicknium,
https://www.clicknium.com/documents/references/python/mouse/
它可以控制鼠标和键盘,并帮助您定位 Web 浏览器和桌面应用程序中的 UI 元素。
这是鼠标沿圆圈移动的示例
from time import sleep
import math
from clicknium import clicknium as cc
def circle():
a,b = cc.mouse.position()
w = 20
m = (2*math.pi)/w
r = 200
while 1:
for i in range(0, w+1):
x = int(a+r*math.sin(m*i))
y = int(b+r*math.cos(m*i))
cc.mouse.move(x,y)
sleep(0.2)
if __name__ == "__main__":
circle()
解决方案 19:
实际情况是,使用 Python 实现鼠标和键盘的自动化有点复杂。特别是如果你在 Linux 中构建。无论如何...
但是你真的需要用 Python 来做吗?因为如果你在 Windows 中,有一种更简单的方法。只需使用 AutoHotkey 脚本语言
扫码咨询,免费领取项目管理大礼包!