Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 758
testlnord Avatar asked Oct 15 '15 14:10

testlnord


2 Answers

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çÁ!")
like image 120
Lucas Bragança Avatar answered Sep 17 '22 14:09

Lucas Bragança


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')
like image 20
testlnord Avatar answered Sep 16 '22 14:09

testlnord