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
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.
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)
8 回答
在安装pywin32(在我的情况下为pywin32-214.win32-py2.6.exe)之后在WinXP,Python 2.6上测试过:
尝试使用PyAutoGUI模块 . 它是多平台的 .
所以:
它还有其他功能:
这比通过所有win32con的东西容易得多 .
您可以使用
win32api
或ctypes
模块使用win32 apis来控制鼠标或任何gui这是一个使用win32api控制鼠标的有趣示例:
点击使用ctypes:
查看跨平台PyMouse:https://github.com/pepijndevos/PyMouse/
另一种选择是使用跨平台AutoPy package . 这个包有两个不同的移动鼠标选项:
此代码段将立即将光标移动到位置(200,200):
如果您希望光标在屏幕上明显移动到给定位置,则可以使用smooth_move命令:
Linux
资料来源:Python mouse move in 5 lines of code (Linux only) .
快速而脏的功能,使用ctypes库在Windows 7上的任何地方
clicks
次点击 . 无需下载 .Pynput是我找到的最佳解决方案,适用于Windows和Mac . 超级易于编程,并且工作得很好 .
例如,