使用 pyautogui 输入 unicode 字符串 [英] Input unicode string with pyautogui

查看:293
本文介绍了使用 pyautogui 输入 unicode 字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 pyautogui 库创建一个自动测试应用程序.我想使用 typewrite 方法将文本输入到表单中.但是我的一些输入字符串中包含 unicode 字符.例如:

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:

纳斯特

根据文档typewrite 只能按单字符键.所以它只是忽略 æ 字符.

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

你能建议一些简单的解决方法吗?

Can you advise some simple workaround?

推荐答案

找到了一个很简单的方案.

Found one quite simple one.

在 Mac 和 Linux 中,有机会使用其十六进制代码输入 unicode 字符.有维基百科上的文章.我正在为 Mac 编写程序,因此我在键盘设置中启用了 Unicode 十六进制输入并编写了以下代码:

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')

这篇关于使用 pyautogui 输入 unicode 字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆