Controlling the mouse and keyboard is called GUI automation. The PyAutoGUI third-party module has many functions to control the mouse and keyboard.
Installation: pip install pyautogui
Documentation: https://pyautogui.readthedocs.io/en/latest/
In programming, Y-coordinates increase going down. This is the opposite of Y-coordinates in mathematics.
If your program gets out of control, quickly move the mouse cursor to the top-left
There are some programs that prohibit third party software from sending clicks to them. For example, many anti-virus programs will prevent this (otherwise the virus could click on the AV program to disable it). It sounds like this is what's happening. You can try running the Python script as an Administrator, but that might not work either. If not, then I don't think there's anything you can do about this.
To use PyAutoGUI
import pyautogui
pyautogui.size()
returns the screen resolution. You can use the output from the function for multiple assignment
pyautogui.size()
width, height = pyautogui.size()
width
height
pyautogui.position()
returns the mouse position, both as tuple of two ints. The tuple of (0, 0) refers to top-left corner of the screen
pyautogui.position()
pyautogui.moveTo()
moves the mouse to an x,y coordinate. The mouse move is instant unless you pass an int for the duration
keyword argument (in seconds)
pyautogui.moveTo(10, 10)
pyautogui.moveTo(10, 100, duration = 1.5)
pyautogui.moveRel()
moves the mouse relative to its current position
pyautogui.moveRel(200, 0)
pyautogui.moveRel(200, 0, duration = 2)
pyautogui.moveRel(0, -100)
pyautogui's click()
, doubleClick()
, rightClick()
, middleClick()
click the mouse buttons
pyautogui.doubleClick()
pyautogui.position()
pyautogui.click(339, 38)
pyautogui.rightClick(339, 38)
dragTo()
and dragRel() will move the mouse while holding down a mouse button
file: draw.py
# import pyautogui
# pyautogui.click() # click to put drawing program in focus
# distance = 200
# while distance > 0:
# print(distance, 0)
# pyautogui.dragRel(distance, 0, duration=0.1) # move right
# distance = distance - 25
# print(0, distance)
# pyautogui.dragRel(0, distance, duration=0.1) # move down
# print(-distance, 0)
# pyautogui.dragRel(-distance, 0, duration=0.1) # move left
# distance = distance - 25
# print(0, -distance)
# pyautogui.dragRel(0, -distance, duration=0.1)
Find screen coordinates quickly. Run the following commands in terminal i.e. command prompt, NOT in IDLE!
# python
# import pyautogui
# pyautogui.displayMousePosition()
pyautogui.click()
first and then execute next command.typewrite()
can be passed a string of characters to type. It also has a interval
keyword argument
# pyautogui.click(100, 100); pyautogui.typewrite('Hello world')
# pyautogui.click(100, 100); pyautogui.typewrite('Hello world', interval=0.2)
Passing a list of strings to typewrite()
lets you use hard-to-type keyboard keys, like 'left'
, 'shift'
or 'f1'
# pyautogui.click(100, 100); pyautogui.typewrite(['a', 'b' 'left', 'X', 'Y'], interval=1)
These keyboard key strings are in the pyautogui.KEYBOARD_KEYS list
# pyautogui.KEYBOARD_KEYS
pyautogui.press()
will press a single key
pyautogui.press('f1')
This will open the Python Docs
pyautogui.hotkey()
can be used for keyboard shortcuts, like Ctrl+O
pyautogui.hotkey('ctrl', 'o')
PyAutoGUI is installed with the pillow imaging module to use its same image data type. Pillow and working with images are beyond the scope of this course. You can learn about it from chapter 17 of the Automate the Boring Stuff with Python book
Additional intallation (on Linux): sudo apt-get install scrot
A screenshot is an image of the screen's content. The pyautogui.screenshot()
will return an Image object of the screen, or you can pass it a filename to save it in a file.
# pyautogui.screenshot()
# pyautogui.screenshot('Z:\\IT\\Python\\Snippets\\screenshot_example.png')
locateOnScreen()
is passed a sample image file, and returns the coordinates of where it is on screen: x-coordinate, y-coordinate, width, height
# pyautogui.locateOnScreen('Z:\\IT\\Python\\Snippets\\calc7key.png')
locateCenterOnScreen()
will return an (x, y) typle of where the image is on the screen
# pyautogui.locateCenterOnScreen('Z:\\IT\\Python\\Snippets\\calc7key.png')
# pyautogui.moveTo(907, 316, duration=1)
Combining the keyboard/mouse/screenshot functions let's you make awesome stuff!!. Example: Bot to Play SushiGoRound