Python can be used to control the keyboard and mouse, which allows us to automate any program that uses these as inputs.
Graphical User Interface (GUI) Automation is particularly useful for repetative clicking or keyboard entry. The program's own module will probably deliver better programmatic performance, but GUI automation is more broadly applicable.
We will be using the pyautogui
module. Lesson 48 details how to install this package.
In [2]:
import pyautogui
This lesson will control all the keyboard controlling functions in the module.
The typewrite()
function will type text into a given textbox. It may be useful to use the mouse operators to navigate and click into a field before running.
In [20]:
# Writes to the cell right below (70 pixels down)
pyautogui.moveRel(0,70)
pyautogui.click()
pyautogui.typewrite('Hello world!')
Again, to simulate more human interaction, we can an interval
parameter like duration
before.
In [21]:
# Writes to the cell right below (70 pixels down)
pyautogui.moveRel(0,70)
pyautogui.click()
pyautogui.typewrite('Hello world!', interval=0.2)
For more complex characters, we can pass a list of complex characters, like the arrow keys, shift, etc.
In [25]:
# Writes to the cell right below (70 pXixels down)
pyautogui.moveRel(0,70)
pyautogui.click()
pyautogui.typewrite(['a','b','left','left','X','Y'], interval=1)
In [ ]:
XYab
A list of keys are available in the KEYBOARD_KEYS
In [26]:
pyautogui.KEYBOARD_KEYS
Out[26]:
These are case-sensitive, but often map to the same function anyway.
In [28]:
pyautogui.typewrite('F1')
pyautogui.typewrite('f1')
We can also pass variables in hotkey mode, i.e. pressed together.
In [33]:
# Simulates ctrl + alt + delete
pyautogui.hotkey('ctrl','alt','delete')
pyautogui
module has many functions to control the mouse and keyboard.pyautogui.typewrite()
function pases virtual keypresses from a string to a highlighted textbox.interval
parameter for speed.typewrite()
lets you use complex keyboard keys, like 'shift' or 'f1'.pyautogui.KEYBOARD_KEYS
list.pyautogui.press()
will press a single key. pyautogui.hotkey()
can be used for combined keys and keyboard shortcuts, like Ctrl+Alt+Delete.