How does one control the mouse cursor in Python, i.e. move it to certain position and click, under Windows?
-
2Do you nee to make the mouse movement in code without user intervention?rahul– rahul2009-07-25 07:19:54 +00:00Commented Jul 25, 2009 at 7:19
-
More information would be really helpful...Gabriel Hurley– Gabriel Hurley2009-07-25 07:20:07 +00:00Commented Jul 25, 2009 at 7:20
-
I wotk in Windows and i don't use any toolkit. I am really new to Python and I never worked with any GUI before. Where I start from? What mannual should I read?Sasha– Sasha2009-07-25 07:26:55 +00:00Commented Jul 25, 2009 at 7:26
-
2why you need a python to do that, you can do that yourself? on a more serious note, why you need it, what is the purpose, a bit more details would be greatAnurag Uniyal– Anurag Uniyal2009-07-25 07:48:55 +00:00Commented Jul 25, 2009 at 7:48
-
3Just FYI if you are using this to keep your machine from locking you could be violating your company or organizations security policy as it is a means of circumventing auto computer locking properties. Ive used this in some places no problem just always make sure to check with your Sys Admins. Ive seen people lose their jobs over minor things like this.AlienAnarchist– AlienAnarchist2016-03-04 15:37:34 +00:00Commented Mar 4, 2016 at 15:37
19 Answers
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)
10 Comments
win32api.SetCursorPos((x,y)) is better to be replaced by win32api.mouse_event(win32con.MOUSEEVENTF_MOVE | win32con.MOUSEEVENTF_ABSOLUTE, int(x/SCREEN_WIDTH*65535.0), int(y/SCREEN_HEIGHT*65535.0)) in my experience for better integration with other application such as games.python3.x works too, feel free to edit the answerTry 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.
4 Comments
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
4 Comments
SetCursorPos?As of 2022, you can use mouse:
import mouse
mouse.move("500", "500")
mouse.click() # default to left click
# mouse.right_click()
# mouse.double_click(button='left')
# mouse.double_click(button='right')
# mouse.press(button='left')
# mouse.release(button='left')
Full Api documentation
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 / 3
- Includes high level API (e.g. record and play).
- Events automatically captured in separate thread, doesn't block main program.
- Tested and documented.
Installation
- Windows:
pip install mouse - Linux:
sudo pip install mouse
4 Comments
mouse.move("500", "500", False) to move relatively.OSError: Unsupported platform 'Darwin'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)
1 Comment
ObjC Class 'b'NSEvent'' couldn't be found despite my bests attempts to fix it)Linux
from Xlib import X, display
d = display.Display()
s = d.screen()
root = s.root
root.warp_pointer(300,300)
d.sync()
4 Comments
Xlib.ext.xtest.fake_input(d, X.ButtonPress, 1); d.sync(); time.sleep(0.001); Xlib.ext.xtest.fake_input(d, X.ButtonRelease, 1); d.sync(); the sleep() call between press and release may or may not be required depending on the target application.Check out the cross platform PyMouse: https://github.com/pepijndevos/PyMouse/
3 Comments
tap space and tap screen shot key by using PyUswerInput?PyUserInput is broken. Can't say if it works well or not because I can't even get it to install due to broken dependencies.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)
4 Comments
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.
Comments
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
Comments
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)
Comments
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()
1 Comment
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 :
1 Comment
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.
2 Comments
pyautogui.moveTo(x, y) because move() moves the cursor relative to its current position.moveTo() moves to an absolute location, while move() moves relative to the current cursor location.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)
Comments
Try Clicknium, https://www.clicknium.com/documents/references/python/mouse/ It can control the mouse and keyboard and help you to locate the UI elements in web browsers and desktop applications.
This is a sample of moving the mouse along a circle
from time import sleep
import math
from clicknium import clicknium as cc
def circle():
a,b = cc.mouse.position()
w = 20
m = (2*math.pi)/w
r = 200
while 1:
for i in range(0, w+1):
x = int(a+r*math.sin(m*i))
y = int(b+r*math.cos(m*i))
cc.mouse.move(x,y)
sleep(0.2)
if __name__ == "__main__":
circle()
Comments
The reality is automating mouse and keyboard using python is a bit complicated. Specially if you're building in Linux. Anyways...
Do you really need to do it in Python, tho? Cause there's an easier way to do it, if you're in Windows. Just use AutoHotkey scripting language