Bartłomiej 'furas' Burek
furas.pl
# prywatne notatki - Python, Linux, Machine Learning, etc.

PyAutoGui type native chars with delay [GB]

pyautogui doesn't sends ascii char to program - it sends to system key code normally used for this char and system uses this key code to send char to program. But if system uses non-standard layout for keyboard then it can send different char then we expect.

Normally

import time
import pyautogui

time.sleep(2) # time to change window

pyautogui.typewrite('!')

sends ! (using key code for key 1 which normally is used to type !) but for some layouts (ie. France layout) it can send § (because key 1 is used to type §)

It can't send also native chars because it doesn't have defined key codes for native chars.

This code will send nothing.

import time
import pyautogui

time.sleep(2) # time to change window

pyautogui.typewrite('ĄĘŚĆ!')

Probably only using clipboad you can send it correctly.

import time
import pyperclip
import pyautogui

time.sleep(2) # time to change window

pyperclip.copy('ĄĘŚĆ!')
pyautogui.hotkey('ctrl', 'v')

If you will use clipboard to copy single char and wait 0.1s between chars then you can get similar result to pyautogui.typewrite(..., interval=0.1).

import time
import pyperclip
import pyautogui

time.sleep(2) # time to change window

for char in 'ĄĘŚĆ!':
    pyperclip.copy(char)
    pyautogui.hotkey('ctrl', 'v', interval=0.1)

BTW: you can see all key codes using

import pyautogui

#print(pyautogui._pyautogui_win.keyboardMapping) # Window
print(pyautogui._pyautogui_x11.keyboardMapping) # Linux

You may even change values in keyboardMapping to send different key code

For example this

import pyautogui

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

pyautogui.typewrite('!!!')

can give ### instead of !!! because 12 is key code for 3/#

Książki: python-dla-kazdego-podstawy-programowania python-wprowadzenie python-leksykon-kieszonkowy python-receptury python-programuj-szybko-i-wydajnie python-projekty-do-wykorzystania black-hat-python-jezyk-python-dla-hackerow-i-pentesterow efektywny-python-59-sposobow-na-lepszy-kod tdd-w-praktyce-niezawodny-kod-w-jezyku-python aplikacje-internetowe-z-django-najlepsze-receptury