Controlling mouse with Python

后端 未结 15 2486
滥情空心
滥情空心 2020-11-22 14:53

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

15条回答
  •  时光说笑
    2020-11-22 15:15

    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)
    

提交回复
热议问题