Controlling mouse with Python

后端 未结 15 2144
滥情空心
滥情空心 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:11

    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.
    0 讨论(0)
  • 2020-11-22 15:12

    If you need to work with games. As explained in this post https://www.learncodebygaming.com/blog/pyautogui-not-working-use-directinput, some games like Minecraft or Fortnite have their own way of registering mouse / keyboard events. The way to control mouse and keyboard events is by using the brand new PyDirectInput library. Their github repository is https://github.com/learncodebygaming/pydirectinput, and has a lot of great information.
    Here's a quick code that does a mouse loop, and clicks:

    import pydirectinput  # pip install pydirectinput
    
    
    pydirectinput.moveTo(0, 500)
    pydirectinput.click()
    
    0 讨论(0)
  • 2020-11-22 15:14

    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

    0 讨论(0)
  • 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)
    
    0 讨论(0)
  • 2020-11-22 15:16

    Move Mouse Randomly On Screen

    It will move the mouse randomly on screen according to your screen resolution. check code below.

    Install pip install pyautogui using this command.

    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)
    
    0 讨论(0)
  • 2020-11-22 15:18

    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)
    
    0 讨论(0)
提交回复
热议问题