Hello everyone! Following up on my post about the basics of working with the PyAutoGUI library for automating actions (in this post, I described this amazing library, explored general examples, and demonstrated working with the mouse) — it’s time (yep, almost a year later) to write about working with the keyboard. Let’s go!
The main function of keyboard control is entering characters in the desired input field. Here’s how to do it:
pyautogui.write('Hello, World!')
If you need to enter text with a delay, you can add an interval to the command:
pyautogui.write('Hello, World!', interval=0.50)
Got the text input covered. But what if you need to press specific keys? For this, we use the press() method:
pyautogui.press('enter') # Press the "Enter" key pyautogui.press('f1') # Press the F1 key pyautogui.press('left') # Press the "Left" arrow key
Interestingly, the press() function is just a wrapper for the keyDown() and keyUp() functions, which simulate pressing and releasing a key. BUT! If needed, you can simulate these actions manually:
pyautogui.keyDown('shift') # Hold down the "Shift" key pyautogui.press('left') # Press the "Left" arrow key pyautogui.press('left') # Press the "Left" arrow key pyautogui.press('left') # Press the "Left" arrow key pyautogui.keyUp('shift') # Release the "Shift" key
Okay, got that. Moving on. Now let’s optimize what we wrote above (because, let’s face it, describing every keypress is inconvenient). Here’s what we can do:
pyautogui.press(['left', 'left', 'left']) # Press the "Left" arrow key three times
Or like this:
pyautogui.press('left', presses=3) # Press the "Left" arrow key three times ;) This way is simpler, right?
By the way, nothing prevents you from holding one key while pressing another. This can be implemented in two ways. The first method:
with pyautogui.hold('shift'): pyautogui.press(['left', 'left', 'left']) # In plain terms: hold the "Shift" key and press "Left" three times
These two lines replace the code below:
pyautogui.keyDown('shift') # Hold the "Shift" key pyautogui.press('left') # Press the "Left" arrow key pyautogui.press('left') # Press the "Left" arrow key pyautogui.press('left') # Press the "Left" arrow key pyautogui.keyUp('shift') # Release the "Shift" key
Finally, today’s post on working with the keyboard using the PyAutoGUI library wouldn’t be complete without explaining how to press multiple keys simultaneously. For this, we use the hotkey() method. Here’s an example:
pyautogui.hotkey('ctrl', 'shift', 'esc')
One line! Elementary, Watson! If we described this using basic keypresses, we’d have to write:
pyautogui.keyDown('ctrl') pyautogui.keyDown('shift') pyautogui.keyDown('esc') pyautogui.keyUp('esc') pyautogui.keyUp('shift') pyautogui.keyUp('ctrl')
By the way, the interval between keypresses is still a thing 😉 Remember: interval=0.25 specifies the delay between keypresses in milliseconds.
For reference, here’s the full list of all keys you can use with the methods described today (press(), keyDown(), keyUp(), hotkey()):
'\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'
That’s all I wanted to share today about working with the keyboard using the PyAutoGUI library in Python. More updates coming soon (I promise it won’t take another year). As always, if you have questions, feel free to email me at oleksiy@lavrynenko.com or reach out on Telegram.
UPD: Video 😉
Support the Blog!
Running a blog takes a lot of effort, time, and passion. Your donations help improve the content, inspire new ideas, and keep the project going.
If you’ve enjoyed the blog’s materials, any support would mean the world to me. Thank you for being here! ❤️