首页 文章

用Python控制鼠标

提问于
浏览
160

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

8 回答

  • 88

    在安装pywin32(在我的情况下为pywin32-214.win32-py2.6.exe)之后在WinXP,Python 2.6上测试过:

    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)
    
  • 275

    尝试使用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的东西容易得多 .

  • 68

    您可以使用 win32apictypes 模块使用win32 apis来控制鼠标或任何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
    
  • 19

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

  • 21

    另一种选择是使用跨平台AutoPy package . 这个包有两个不同的移动鼠标选项:

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

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

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

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

    Linux

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

    资料来源:Python mouse move in 5 lines of code (Linux only) .

  • 6

    快速而脏的功能,使用ctypes库在Windows 7上的任何地方 clicks 次点击 . 无需下载 .

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

    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)
    

相关问题