用Python控制鼠标

问题:用Python控制鼠标

在Windows下如何控制Python中的鼠标光标,即将其移动到特定位置并单击?

How does one control the mouse cursor in Python, i.e. move it to certain position and click, under Windows?


回答 0

在安装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)

Tested on WinXP, Python 2.6 (3.x also tested) after installing pywin32 (pywin32-214.win32-py2.6.exe in my case):

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)

回答 1

尝试使用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内容容易得多。

Try with the PyAutoGUI module. It’s multiplatform.

pip install pyautogui

And so:

import pyautogui
pyautogui.click(100, 100)

It also has other features:

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

This is much easier than going through all the win32con stuff.


回答 2

您可以使用win32apictypes模块来使用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

You can use win32api or ctypes module to use win32 apis for controlling mouse or any gui

Here is a fun example to control mouse using 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)

A click using 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

回答 3

另一个选择是使用跨平台的AutoPy软件包。该软件包有两个不同的移动鼠标选项:

此代码段会将光标立即移到位置(200,200):

import autopy
autopy.mouse.move(200,200)

如果您希望光标在屏幕上明显地移动到给定的位置,则可以使用smooth_move命令:

import autopy
autopy.mouse.smooth_move(200,200)

Another option is to use the cross-platform AutoPy package. This package has two different options for moving the mouse:

This code snippet will instantly move the cursor to position (200,200):

import autopy
autopy.mouse.move(200,200)

If you instead want the cursor to visibly move across the screen to a given location, you can use the smooth_move command:

import autopy
autopy.mouse.smooth_move(200,200)

回答 4

查看跨平台PyMouse:https//github.com/pepijndevos/PyMouse/


回答 5

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

Linux

from Xlib import X, display
d = display.Display()
s = d.screen()
root = s.root
root.warp_pointer(300,300)
d.sync()

Source: Python mouse move in 5 lines of code (Linux only).


回答 6

对于Windows和Mac,Pynput是我发现的最佳解决方案。超级容易编程,并且效果很好。

例如,

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)

Pynput is the best solution I have found, both for Windows and for Mac. Super easy to program, and works very well.

For example,

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)

回答 7

快速和肮脏的功能将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.

Quick and dirty function that’ll left click wherever clicks times on Windows 7 using the ctypes library. No downloads required.

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.

回答 8

2020年开始,您可以使用鼠标

import mouse
mouse.move("500", "500")
mouse.left_click()

特征

  • 全局事件挂钩在所有鼠标设备上(捕获事件,而与焦点无关)。
  • 侦听并发送鼠标事件。
  • 适用于Windows和Linux(需要sudo)。
  • 纯Python,无需编译C模块。
  • 零依赖性。安装和部署很简单,只需复制文件即可。
  • Python 2和3
  • 包括高级API(例如记录和播放)。
  • 在单独的线程中自动捕获的事件不会阻塞主程序。
  • 经过测试和记录。

As of 2020, you can use mouse:

import mouse
mouse.move("500", "500")
mouse.left_click()

Features

  • Global event hook on all mice devices (captures events regardless of focus).
  • Listen and sends mouse events.
  • Works with Windows and Linux (requires sudo).
  • Pure Python, no C modules to be compiled.
  • Zero dependencies. Trivial to install and deploy, just copy the files.
  • Python 2 and 3
  • Includes high level API (e.g. record and play).
  • Events automatically captured in separate thread, doesn’t block main program.
  • Tested and documented.

回答 9

如果要移动鼠标,请使用以下命令:

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 pyautoguipython3 -m pip install pyautogui

If you want to move the mouse, use this:

import pyautogui
pyautogui.moveTo(x,y)

If you want to click, use this:

import pyautogui
pyautogui.click(x,y)

If you don’t have pyautogui installed, you must have python attached to CMD. Go to CMD and write: pip install pyautogui

This will install pyautogui for Python 2.x.

For Python 3.x, you will probably have to use pip3 install pyautogui or python3 -m pip install pyautogui.


回答 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中控制鼠标

Another alternative would be mouse library, I personally use it as it is relatively simple and cross-platform.

Here is how you can use it:

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

Here is the source: How to Control your Mouse in 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)

The accepted answer worked for me but it was unstable (sometimes clicks wouldn’t regsiter) so I added an additional MOUSEEVENTF_LEFTUP . Then it was working reliably

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

非常简单的1-安装软件包:

pip install mouse

2-将库添加到项目:

import mouse

3-使用它例如:

mouse.right_click()

在此url中描述您可以使用的所有功能:

https://github.com/boppreh/mouse

very easy 1- install pakage :

pip install mouse

2- add library to project :

import mouse

3- use it for example :

mouse.right_click()

in this url describe all function that you can use it :

https://github.com/boppreh/mouse