asweigart / pyautogui

A cross-platform GUI automation Python module for human beings. Used to programmatically control the mouse & keyboard.
BSD 3-Clause "New" or "Revised" License
10.22k stars 1.24k forks source link

How to input key with unicode? #657

Open heyouwei2021 opened 2 years ago

heyouwei2021 commented 2 years ago

How to input Unicode?

wang364 commented 2 years ago

https://github.com/asweigart/pyautogui/blob/master/docs/simplified-chinese.ipynb

import pyperclip
import pyautogui

#  PyAutoGUI中文输入需要用粘贴实现
#  Python 2版本的pyperclip提供中文复制
def paste(foo):
    pyperclip.copy(foo)
    pyautogui.hotkey('ctrl', 'v')

foo = u'学而时习之'
#  移动到文本框
pyautogui.click(130,30)
paste(foo)
heyouwei2021 commented 2 years ago

Thank you. But my question is not to input Chinese characters. I need to input the Unicode of 'a' to the game. The game can only accept Unicode not text. How should I handle it?

Regards,

arisliang commented 2 years ago

doesn't work with python 3 either.

both pyautogui.write('你好') and pyautogui.write(u'你好') outputs nothing.

JayRizzo commented 2 years ago

Hi all,

I hope everyone is well. I get this behaviour too. I tried a few tests but unless i set my terminal to Chinese or my terminal encoding enable "Unicode East Asian Ambiguous Characters are wide" i get strange behaviour as it doesn't know how to interpret the characters. it works in bash but not when in the python3 interpreter.

I believe this might be what languages are supported on your computer. I do not know if this will help, but i found these:

  1. https://developpaper.com/how-to-install-chinese-version-and-python-intelligent-perception-in-vscode/

  2. https://stackoverflow.com/questions/22528001/pip-install-on-chinese-server-ssl-error

  3. https://wiki.python.org/moin/ChineseLanguage

Good luck.

Cheers!

pendragons-code commented 2 years ago

Other than chinese characters, it seems like invisible characters and some symbols (primarily arrows, shapes and more) do not show up at all. The braille font and items also seem to fail.

codeBodger commented 3 months ago

This library seems to do the trick: https://pynput.readthedocs.io/en/latest/keyboard.html#controlling-the-keyboard

You can use keyboard.press() to input unicode characters.