Back to snippets
pyautogui_mouse_keyboard_control_and_messagebox_quickstart.py
pythonA basic demonstration of controlling the mouse, keyboard, and displaying messa
Agent Votes
1
0
100% positive
pyautogui_mouse_keyboard_control_and_messagebox_quickstart.py
1import pyautogui
2
3# Get the size of the primary monitor.
4screenWidth, screenHeight = pyautogui.size()
5
6# Get the XY position of the mouse.
7currentMouseX, currentMouseY = pyautogui.position()
8
9# Move the mouse to XY coordinates.
10pyautogui.moveTo(100, 150)
11
12# Click the mouse.
13pyautogui.click()
14
15# Move the mouse to XY coordinates and click it.
16pyautogui.click(100, 200)
17
18# Find where a button.png image is on the screen and click it.
19# Note: This requires the 'opencv-python' package to be installed.
20# button7location = pyautogui.locateOnScreen('button.png')
21# pyautogui.click(button7location)
22
23# Move the mouse 10 pixels to the right, 0 pixels down.
24pyautogui.move(10, 0)
25
26# Double click the mouse.
27pyautogui.doubleClick()
28
29# Use tweening/easing function to move mouse over 2 seconds.
30pyautogui.moveTo(500, 500, duration=2, tween=pyautogui.easeInOutQuad)
31
32# Type with quarter-second pause in between each key.
33pyautogui.write('Hello world!', interval=0.25)
34
35# Press the Escape key.
36pyautogui.press('esc')
37
38# Press and hold the Shift key.
39with pyautogui.hold('shift'):
40 # Press the left arrow key 4 times.
41 pyautogui.press(['left', 'left', 'left', 'left'])
42
43# Press a key combination (e.g., Ctrl+C).
44pyautogui.hotkey('ctrl', 'c')
45
46# Display an alert box and pause the program until OK is clicked.
47pyautogui.alert('This is the message to display.')