Controlling the Mouse with Python
Wanting to move and click the mouse cursor on Windows using Python? Here's how it's done:
Solution:
After installing pywin32, follow these steps:
<code class="python">import win32api, win32con</code>
<code class="python">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)</code>
<code class="python">click(10, 10) # Example: Click at coordinates (10, 10)</code>
This code has been tested on Windows XP and Python 2.6 and 3.x.
The above is the detailed content of How to Automate Mouse Clicks and Movements in Windows Using Python?. For more information, please follow other related articles on the PHP Chinese website!