testlnord
testlnord

Reputation: 458

Input unicode string with pyautogui

I'm creating an autotesting app with pyautogui lib. I want to use typewrite method to input text into forms. But some of my input strings have unicode characters in them. For example:

Næst

According to documentation typewrite can only press single-character keys. So it just ignores the æ character.

Can you advise some simple workaround?

Upvotes: 15

Views: 26496

Answers (6)

FagnerPontes
FagnerPontes

Reputation: 1

import pyperclip
import pyautogui
from time import sleep


def TextAuto(myText):
    i = 0
    f = 0
    for char in myText:
        f += 1
        if char in 'áéíóúâêôãõçàÁÉÍÓÚÂÊÔÃÕÇÀ':
            pyautogui.typewrite(myText[i:f], interval=0.04)
            i = f
            pyperclip.copy(char)
            pyautogui.hotkey("ctrl", "v")
    if i != f:
        pyautogui.typewrite(myText[i:f], interval=0.04)

sleep(4)
TextAuto("""áéíóúâêôãõçàÁÉÍÓÚÂÊÔÃÕÇÀ""")

This code works, just install pyautogui.

Upvotes: 0

tungngt
tungngt

Reputation: 41

Try pynput instead I found it more easy to type Unicode text. Install it using pip install pynput or pip3 install pynput

from pynput.keyboard import Controller

keyboard = Controller()

keyboard.type("Næst")

Upvotes: 4

from pynput.keyboard import Controller
import time 
time.sleep(3)
Controller().type("Næst")

This code works perfectly. Just need to install pynput with pip command.

Upvotes: 3

honestSalami
honestSalami

Reputation: 257

I tried trestlnord's answer, but it did not work. I adapted the idea to this:

import pyautogui as px

def type_unicode(word):
    for char in word:
        num = hex(ord(char))
        px.hotkey('ctrl', 'shift', 'u')
        for n in num:
            px.typewrite(n)
        px.typewrite('\n')

works on arch linux

Upvotes: 4

Lucas Bragança
Lucas Bragança

Reputation: 607

I know this thread is old, but for the sake of the topic I managed to get around it using pyperclip in an easier manner in my opinion.

Rather than trying to make pyautogui to type special characters, copy them to the clipboard using pyperclip and then use pyautogui to paste them. For instance on Windows:

import pyautogui
import pyperclip

pyperclip.copy("It's leviOsa, not lêvioçÁ!")
pyautogui.hotkey("ctrl", "v")

EDIT:

We can make it work in multiple platforms as below (thanks @karlo for pointing it out):

import pyautogui
import pyperclip
import platform

def type(text: str):    
    pyperclip.copy(text)
    if platform.system() == "Darwin":
        pyautogui.hotkey("command", "v")
    else:
        pyautogui.hotkey("ctrl", "v")


type("It's leviOsa, not lêvioçÁ!")

Upvotes: 44

testlnord
testlnord

Reputation: 458

Found one quite simple one.

In Mac and Linux there is an opportunity to input unicode characters using their hex codes. There is article on wikipedia about that. I'm writing my program for Mac so I enabled Unicode Hex Input in my keyboard settings and wrote this code:

def type_unicode(word):
    for c in word:
        c = '%04x' % ord(c)
        pyautogui.keyDown('optionleft')
        pyautogui.typewrite(c)
        pyautogui.keyUp('optionleft')

Upvotes: 4

Related Questions