Python Central HubSign in

Automation and Scripting / Phase 5: GUI & System Control

Keyboard Automation - Typing and Hotkeys

Type text, press keys, and trigger keyboard shortcuts.

Type text

Python
import pyautogui
 
pyautogui.write("hello", interval=0.05)
pyautogui.press("enter")

Hotkeys

Python
import pyautogui
 
# Example: Ctrl+L (address bar in browsers)
pyautogui.hotkey("ctrl", "l")

Tip

UI automation is fragile; use stable workflows and add delays.

Three ways to send keys

pyautogui has three keyboard calls, and they do different things.

  • write(text, interval=0.05) types characters one at a time, as if from a keyboard. interval is the delay between keys. It types only characters it can produce with a single key press, so accented letters and emoji may be dropped or wrong, and it types into whichever window has focus.
  • press(key) presses one named key, such as "enter", "tab", "esc", "down", or "f5". Pass presses=3 to repeat it.
  • hotkey(*keys) holds several keys down together and releases them in reverse, such as hotkey("ctrl", "c"). Use it for shortcuts.

The full list of key names is in pyautogui.KEYBOARD_KEYS. Use "command" on macOS where Windows and Linux use "ctrl".

Typing text that is not plain ASCII

For long or non-ASCII text, typing key by key is slow and unreliable. A better way is to put the text on the clipboard and paste it with a hotkey. The pyperclip package (pip install pyperclip) sets the clipboard.

Python
import sys
import time
 
import pyautogui
import pyperclip
 
pyautogui.FAILSAFE = True
pyautogui.PAUSE = 0.2
 
PASTE_KEY = "command" if sys.platform == "darwin" else "ctrl"
 
 
def paste_text(text: str) -> None:
    pyperclip.copy(text)
    pyautogui.hotkey(PASTE_KEY, "v")
 
 
def fill_form(fields: list[str]) -> None:
    for value in fields:
        paste_text(value)
        pyautogui.press("tab")        # move to the next field
 
 
pyautogui.alert("Click into the first form field, then press OK.", title="Ready")
time.sleep(1)
fill_form(["Alice", "alice@example.com", "Résumé attached"])

This fills three fields, and it handles the accents in the last value. The alert gives you time to click into the first field, because the script cannot know where to start typing.

Note that this overwrites your clipboard. If that matters, save pyperclip.paste() first and restore it afterwards.

Timing

Programs need time to react. If a hotkey opens a dialog, wait before you type into it, for example time.sleep(1), or better, wait until the window title changes. Keys sent too early are lost or land in the previous window. Raise pyautogui.PAUSE while you develop.

Safety

Keystrokes can do damage: ctrl+a then delete erases a document, and enter confirms dialogs. Never send write() output to a window you have not checked. Do not type passwords with write, because the text sits in your script, and could go to the wrong window. Keep FAILSAFE on, and note that a stuck key can be released with pyautogui.keyUp("ctrl").

Common mistakes

  • Using write for shortcuts. write("ctrl+s") types those letters. Use hotkey.
  • Wrong key names. "return" and "enter" both work, but "Enter" with a capital may not.
  • No delay after opening a window.
  • Windows and macOS shortcut differences.
  • Typing while the script runs, which mixes your keys with its keys.

Check your understanding

quizCheck yourself
  1. How do you press Ctrl+S?

    Show answer

    B — pyautogui.hotkey("ctrl", "s") — hotkey holds the keys together. write would type the characters c, t, r, l, plus sign and s as text.

  2. Why paste from the clipboard instead of using write for text with accents?

    Show answer

    A — write types single key presses and may drop or garble characters that have no simple key — write simulates keys. Characters that need special input can fail, whereas pasting inserts the exact text.

  3. Your script opens a dialog with a hotkey and immediately types into it, but the text is lost. What is the likely cause?

    Show answer

    A — The keys were sent before the dialog had opened and taken focus — Applications need time to react. Wait for the window to appear, ideally by checking its title, before sending keys.

Was this page helpful?

Let us know how we did