It doesn't send ascii char ! to program - it sends keyboard's code to system (probably code for key 1 which in standard layout is used for char !) and system decides what char send to program. If your system has non-standard layout then system may send wrong char.

Probably only using clipboad you can send it correctly. If you will use clipboard to copy single char and wait 0.1s between chars then you can get similar result.

Copyimport time
import pyperclip
import pyautogui

time.sleep(2)

for char in 'Hello World!':
    pyperclip.copy(char)
    pyautogui.hotkey('ctrl', 'v', interval=0.1)

BTW: using print(pyautogui.__file__) you can find folder with source code and in file _pyautogui_win.py you can see what key codes it uses in Windows.

You should see key codes assigned to chars using also

Window:

Copyprint(pyautogui._pyautogui_win.keyboardMapping)

Linux:

Copyprint(pyautogui._pyautogui_x11.keyboardMapping)

Maybe if you change values in keyboardMapping then it will send it correctly but for every layout you would have to set different values.

For example on Linux this

Copyimport pyautogui

#pyautogui._pyautogui_win.keyboardMapping['!'] = 12
pyautogui._pyautogui_x11.keyboardMapping['!'] = 12

pyautogui.typewrite('!!!')

gives me ### instead of !!!

Answer from furas on Stack Overflow
🌐
PyAutoGUI
pyautogui.readthedocs.io › en › latest › keyboard.html
Keyboard Control Functions — PyAutoGUI documentation
>>> pyautogui.press('enter') # press the Enter key >>> pyautogui.press('f1') # press the F1 key >>> pyautogui.press('left') # press the left arrow key
🌐
PyAutoGUI
pyautogui.readthedocs.io › en › latest › quickstart.html
Cheat Sheet — PyAutoGUI documentation - Read the Docs
This is a quickstart reference to using PyAutoGUI. PyAutoGUI is cross-platform GUI automation module that works on Python 2 & 3.
🌐
PyPI
pypi.org › project › PyAutoGUI
PyAutoGUI · PyPI
PyAutoGUI lets Python control the mouse and keyboard, and other GUI automation tasks. For Windows, macOS, and Linux, on Python 3 and 2.
      » pip install PyAutoGUI
    
Published   May 24, 2023
Version   0.9.54
🌐
GitHub
github.com › asweigart › pyautogui
GitHub - asweigart/pyautogui: A cross-platform GUI automation Python module for human beings. Used to programmatically control the mouse & keyboard. · GitHub
A cross-platform GUI automation Python module for human beings. Used to programmatically control the mouse & keyboard. - asweigart/pyautogui
Starred by 12.5K users
Forked by 1.4K users
Languages   Python
🌐
PyAutoGUI
pyautogui.readthedocs.io › en › latest › install.html
Installation — PyAutoGUI documentation - Read the Docs
PyAutoGUI install the modules it depends on, including PyTweening, PyScreeze, PyGetWindow, PymsgBox, and MouseInfo.
Top answer
1 of 4
5

It doesn't send ascii char ! to program - it sends keyboard's code to system (probably code for key 1 which in standard layout is used for char !) and system decides what char send to program. If your system has non-standard layout then system may send wrong char.

Probably only using clipboad you can send it correctly. If you will use clipboard to copy single char and wait 0.1s between chars then you can get similar result.

Copyimport time
import pyperclip
import pyautogui

time.sleep(2)

for char in 'Hello World!':
    pyperclip.copy(char)
    pyautogui.hotkey('ctrl', 'v', interval=0.1)

BTW: using print(pyautogui.__file__) you can find folder with source code and in file _pyautogui_win.py you can see what key codes it uses in Windows.

You should see key codes assigned to chars using also

Window:

Copyprint(pyautogui._pyautogui_win.keyboardMapping)

Linux:

Copyprint(pyautogui._pyautogui_x11.keyboardMapping)

Maybe if you change values in keyboardMapping then it will send it correctly but for every layout you would have to set different values.

For example on Linux this

Copyimport pyautogui

#pyautogui._pyautogui_win.keyboardMapping['!'] = 12
pyautogui._pyautogui_x11.keyboardMapping['!'] = 12

pyautogui.typewrite('!!!')

gives me ### instead of !!!

2 of 4
1

This seems to be a known issue:

https://github.com/asweigart/pyautogui/issues/38

User on Windows 7, Python 3.4, running PyAutoGUI 0.9.30 and a French "AZERTY" keyboard reported being unable to simulate pressing :
Running the unit tests, they got these results:
[...]
a
ba
.Hello world§

https://github.com/asweigart/pyautogui/pull/55

https://github.com/asweigart/pyautogui/issues/137

Find elsewhere
🌐
PyAutoGUI
pyautogui.readthedocs.io › en › latest
Welcome to PyAutoGUI’s documentation! — PyAutoGUI documentation
PyAutoGUI lets your Python scripts control the mouse and keyboard to automate interactions with other applications. The API is designed to be simple.
🌐
Topcoder
topcoder.com › thrive › articles › python-for-gui-automation-pyautogui
PYTHON FOR GUI AUTOMATION – PYAUTOGUI
December 15, 2020 - This is used to enter text and to enter a new line. pyautogui.typewrite(‘Test’, interval=secs_between_keys)
🌐
Automate the Boring Stuff
automatetheboringstuff.com › 1e › chapter18
Controlling the Keyboard and Mouse with GUI Automation
The pyautogui module has functions for simulating mouse movements, button clicks, and scrolling the mouse wheel.
🌐
Python Central
pythoncentral.io › pyautogui-tutorial-how-to-automate-gui-tasks-with-python
PyAutoGUI Tutorial: How to Automate GUI Tasks with Python | Python Central
March 4, 2025 - PyAutoGUI is one of the famous Python modules. It enables automation of GUI interactions such as moving the cursor, press keyboard strokes, and read screen information.
🌐
Medium
medium.com › @khushalkathad2512 › python-pyautogui-easy-to-automate-013e064c1451
Python PyAutoGUI : Easy to Automate. | by Khushal Kathad | Medium
October 19, 2023 - A package enables you to control your laptop’s mouse and keyboard to automate a variety of operations. It is cross-platform and functional with Linux, macOS, and Windows. When doing repeated tasks or requiring interaction with graphical user interfaces (GUIs), PyAutoGUI is very helpful.
🌐
Medium
medium.com › @dattanikaran25 › get-started-with-pyautogui-75962f3088ce
Get Started With PyAutoGUI 🐍. Python + PyAutoGUI | by Dattanikaran | Medium
October 15, 2023 - So, when you put “py” (Python) together with “rat” (mouse) and “keyboard,” you get “PyAutoGUI,” which is a Python library for automating tasks involving mouse and keyboard input.
🌐
Stack Abuse
stackabuse.com › getting-started-with-python-pyautogui
Getting Started with Python PyAutoGUI
August 29, 2023 - In this tutorial, we're going to learn how to use pyautogui library in Python 3. The PyAutoGUI library provides cross-platform support for managing mouse and keyboard operations through code to enable automation of tasks.
🌐
HotExamples
python.hotexamples.com › examples › pyautogui › - › alert › python-alert-function-examples.html
Python alert Examples, pyautogui.alert Python Examples - HotExamples
def runner(*args): message = '' ... = (anaesthetist, endoscopist, nurse, asa, upper, colon, banding, clips, consult, message, op_time, fund, in_theatre, out_theatre, varix_flag, varix_lot) print(in_data) ... import pyautogui import os import time import tkinter if ...
🌐
PyAutoGUI
pyautogui.readthedocs.io › en › latest › mouse.html
Mouse Control Functions — PyAutoGUI documentation
#! python import pyautogui, sys print('Press Ctrl-C to quit.') try: while True: x, y = pyautogui.position() positionStr = 'X: ' + str(x).rjust(4) + ' Y: ' + str(y).rjust(4) print positionStr, print '\b' * (len(positionStr) + 2), sys.stdout.flush() except KeyboardInterrupt: print '\n'
🌐
Readthedocs
autogui.readthedocs.io › en › latest › quickstart.html
Cheat Sheet — PyAutoGUI documentation
This is a quickstart reference to using PyAutoGUI. PyAutoGUI is cross-platform GUI automation module that works on Python 2 & 3.
🌐
Mwyoung
mwyoung.github.io › pages › python-automation
Python Automation
- list all key codes that can be used - pyautogui.press('key') - press a single key - pyautogui.hotkey('ctrl','o') - press keyboard shortcut 50 - Screenshots and Image Recognition Use pyautogui/pillow (for images) - pyautogui.screenshot('filename') - take screenshot, save image to filesystem - pyautogui.locateOnScreen('filename.png') - locate png, x,y coord of where to find - return x, y, width, height of image - pyautogui.locateCenterOnScreen('filename.png') - locate center of image Note: can take up to 1 second, image needs to be exact same to locate 51 - End ========================================================================================== ...