HOWTO · Python
How to Simulate Keyboard Inputs in Python
Learn how to simulate keyboard inputs in Python.
On this page
Use the keyboard package or PyAutoGUI to type text from a Python script. The first provides write() for text and press_and_release() for key events. PyAutoGUI provides write(), keyDown(), keyUp(), and press() for text and individual key events. Install the library you choose with pip install keyboard or pip install pyautogui.
Both libraries send input to whichever field currently has focus; neither example selects an application or field. Prepare the target first and avoid typing sensitive text into an unintended window. Simulated text appears in the target application, not as terminal output. The following standard-library example prints a plan of the actions used below; it does not send keyboard events.
Treat a text string and a key event as different kinds of input. Use a text-writing call for ordinary words and punctuation, and a key-event call when the target must receive a named key such as Enter or a modifier combination. Keep the target ready before starting a script, and use a harmless test field when adapting a sequence. The sample plan makes the order explicit: text first, then Enter, then a modified key event, followed by the remaining letters. Reorder or remove actions to match the form or editor you intend to use.
Run this example:
text = "Python is an amazing programming language."
actions = [("type", text), ("press", "enter"), ("press", "shift+p"), ("type", "ython")]
for action, value in actions:
print(f"{action}: {value}")
type: Python is an amazing programming language.
press: enter
press: shift+p
type: ython
Simulate Keyboard Input with the keyboard Library
Install the package with pip install keyboard. To type a sentence, press Enter, and then type Python with Shift held for its first letter, the package-specific sequence is keyboard.write("Python is an amazing programming language."), keyboard.press_and_release("enter"), keyboard.press_and_release("shift+p"), and keyboard.write("ython"), in that order. These calls require the installed package and an interactive focused text field, so they are API instructions rather than an executed example. Place the caret deliberately before running them. The target text is the sentence, followed by a newline from Enter and Python on the next line. write() accepts text to type, while press_and_release() sends a named key or combination and releases it. Here, shift+p types the uppercase first letter of Python. These calls do not choose a window or move the caret. Pressing Enter may submit a form or activate a control, so omit that call if the target should not receive Enter. If only text entry is needed, the write() calls are sufficient. See the keyboard documentation for the API details.
Simulate Keyboard Input with PyAutoGUI
Install PyAutoGUI with pip install pyautogui. To type the sentence, hold Shift while pressing a, b, and c, then type lowercase xyz; the package-specific call sequence is pyautogui.write("Python is an amazing programming language."), pyautogui.keyDown("shift"), pyautogui.press("a"), pyautogui.press("b"), pyautogui.press("c"), pyautogui.keyUp("shift"), and pyautogui.write("xyz"), in that order. These calls require the installed package and an interactive text target, so they are API instructions rather than an executed example. Focus a text field deliberately before running them. The target receives Python is an amazing programming language.ABCxyz: keyDown("shift") keeps Shift held for the three individual press() calls, so those letters are uppercase, and keyUp("shift") releases it before xyz is typed. write() types the supplied string. keyDown() holds a key until keyUp() releases it. press() sends a press-and-release event for an individual key, such as one letter. To hold a modifier across several key presses, put keyDown() and keyUp() around those calls instead of treating press() as an arbitrary key-combination function. When only text is needed, write() is enough; keep the key-event sequence when the modifier behavior is needed. See the PyAutoGUI documentation for further details.
When changing the sequence, keep keyDown() and keyUp() paired around only the key presses that need the modifier. Calls after keyUp() run without that held modifier. For a simple sentence, remove the key-event calls entirely. This keeps the script focused on the text entry task and makes it easier to identify which operation produced an unexpected character.