PyAutoGUI Keyboard Automation

In this tutorial for PyAutoGUI, we will discuss Keyboard Automation and how we can effectively manipulate keyboard presses and clicks. PyAutoGUI offers support for even complex keyboard operations such as shortcuts and hotkeys.

Of course, in order to fully utilize the potential of Keyboard Automation, you should also know how to manipulate the mouse. I highly recommend you check out our tutorial on Mouse Automation in PyAutoGUI. Why is this important? Because if you want to enter text in an input box, how will you reach the box in the first place without knowing how to navigate the mouse?


Writing Text to the Screen

The very first function we will discuss is write(). As the name implies, this function can be used to write text to the screen. The write(string) function will type the characters in the string passed to it, like they normally would be when we press the character keys on our keyboard.

For example, passing in the string “Hello World” and executing this code will cause “Hello World” to be typed out on the screen, wherever the mouse cursor is currently.

import pyautogui

pyautogui.write("Hello World", interval = 0.1)

The interval option adds 0.1 seconds of a delay between the characters as they are typed.

Note: You may need to maneuver the mouse a bit before you run this code. If your mouse cursor is not currently in an area where you can enter text, this will not work. This is also why learning how to automate the mouse is important even when if you want to automate the keyboard.


Automating Keyboard Presses

In this section we will discuss the press() function. This function is similar to the write(), but instead of just printing out characters, it actually simulates/presses the actual key.

Passing “enter” to the write() function will cause the 5 characters in that string to be typed out. On the other hand, passing this string to the press() function will press the actual “enter” key on your keyboard. That’s a pretty big difference right there.

import pyautogui

pyautogui.write("Hello World", interval = 0.1)
pyautogui.press("enter")
pyautogui.write("Hello World", interval = 0.1)
pyautogui.press("enter")
pyautogui.write("Hello World", interval = 0.1)

Try running the above code. It’s going to type out “Hello World” three times on new lines.

Pressing multiple keys

If you want to press multiple keys in one go, instead of writing out separate function calls, you can just make a list and pass it in to the press() function.

pyautogui.press(['enter', 'enter', 'enter'])

List of Keys in PyAutoGUI

A list of all the key codes for the various key options in PyAutoGUI.

['\t', '\n', '\r', ' ', '!', '"', '#', '$', '%', '&', "'", '(',
')', '*', '+', ',', '-', '.', '/', '0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', ':', ';', '<', '=', '>', '?', '@', '[', '\\', ']', '^', '_', '`',
'a', 'b', 'c', 'd', 'e','f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '{', '|', '}', '~',
'accept', 'add', 'alt', 'altleft', 'altright', 'apps', 'backspace',
'browserback', 'browserfavorites', 'browserforward', 'browserhome',
'browserrefresh', 'browsersearch', 'browserstop', 'capslock', 'clear',
'convert', 'ctrl', 'ctrlleft', 'ctrlright', 'decimal', 'del', 'delete',
'divide', 'down', 'end', 'enter', 'esc', 'escape', 'execute', 'f1', 'f10',
'f11', 'f12', 'f13', 'f14', 'f15', 'f16', 'f17', 'f18', 'f19', 'f2', 'f20',
'f21', 'f22', 'f23', 'f24', 'f3', 'f4', 'f5', 'f6', 'f7', 'f8', 'f9',
'final', 'fn', 'hanguel', 'hangul', 'hanja', 'help', 'home', 'insert', 'junja',
'kana', 'kanji', 'launchapp1', 'launchapp2', 'launchmail',
'launchmediaselect', 'left', 'modechange', 'multiply', 'nexttrack',
'nonconvert', 'num0', 'num1', 'num2', 'num3', 'num4', 'num5', 'num6',
'num7', 'num8', 'num9', 'numlock', 'pagedown', 'pageup', 'pause', 'pgdn',
'pgup', 'playpause', 'prevtrack', 'print', 'printscreen', 'prntscrn',
'prtsc', 'prtscr', 'return', 'right', 'scrolllock', 'select', 'separator',
'shift', 'shiftleft', 'shiftright', 'sleep', 'space', 'stop', 'subtract', 'tab',
'up', 'volumedown', 'volumemute', 'volumeup', 'win', 'winleft', 'winright', 'yen',
'command', 'option', 'optionleft', 'optionright']

Holding Down Keys

Another important feature of keyboard automation in pyautogui is the keyDown() function. This is similar to press(), but instead of simply “pressing” them, this function reproduces the effect of holding down the key. This is specially important when trying to activate shortcuts.

A very popular shortcut is CTRL + V, which is used to paste some text. If you wanted to replicate this, you might try the following code.

import pyautogui

pyautogui.press("ctrlleft")
pyautogui.press("v")

This will not work however, as these are registered as two separate key presses that have no connection to each other. To trigger the shortcut, we need to hold down “ctrlleft” and then press the “v’ key. This is where the keyDown() function comes in.

import pyautogui

pyautogui.keyDown("ctrlleft")
pyautogui.keyDown("v")

pyautogui.keyUp("v")
pyautogui.keyUp("ctrlleft")

The above code will accurately paste the text in our clipboard onto the screen. Calling the keyUp() function is also nessacery, otherwise the keys will remain pressed down forever until the program exits.


Using Hotkeys in PyAutoGUI

As you might have noticed in the previous section, using keyDown() can get a little repetitive and annoying, especially since you need to add a keyUp() function call for every keyDown() call.

The hotkey() function in PyAutoGUI provides us with a handy solution, and we can combine all those lines of code into a single function call.

import pyautogui

pyautogui.hotkey("ctrlleft", "v")

The above code has the exact same utility as the one we wrote before for keyDown().


What’s next?

Once you have learnt the PyAutoGUI basics, you should enhance your knowledge and skill at automation by learning image detection in PyAutoGUI. This allows you to automatically locate certain patterns/images on the screen, to which you can navigate.


This marks the end of the PyAutoGUI Keyboard Automation Tutorial. Any suggestions or contributions for CodersLegacy are more than welcome. Questions regarding the tutorial content can be asked in the comments section below.

Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments