我正在尝试编写一个脚本来编写脚本,编写脚本等等。为此,我一直在使用 pyautogui 模块。问题是,当我尝试打印出字符 "时,它会写入字符 Ä。这是我目前的脚本,我使用的是瑞典语键盘:
import pyautogui
#command + n creates a new file in python it is the same as inputting ctrl + n
pyautogui.hotkey("command","n")
pyautogui.typewrite("import pyautogui \npyautogui.hotkey('command','n')")
我发现了一个类似的问题 answer在 stackoverflow 中,但他们说他们已经“修补”以使其工作。但我的水平无法理解他们为完成这项工作所做的工作,因此我的问题是:
有人可以解释一下如何使用我的瑞典语键盘进行书写吗?
请您参考如下方法:
There are lots of Problems with Keyboard Layouts in pyautogui (when not using the default english layout)
您可以尝试以下方法之一:
- 如果您使用的是 Windows:Load Keyboard Layout
win32api.LoadKeyboardLayout int = LoadKeyboardLayout(KLID, Flags)
- 你可以tinker around在 _pyautogui_win.py 中
if key == '\\': ctypes.windll.user32.keybd_event(0x11, 0, 0, 0) # should be left control ctypes.windll.user32.keybd_event(0x12, 0, 0, 0) # should be alt ctypes.windll.user32.keybd_event(0xDB, 0, 0, 0) # should be alt ß ctypes.windll.user32.keybd_event(0x11, 0, KEYEVENTF_KEYUP, 0) ctypes.windll.user32.keybd_event(0x12, 0, KEYEVENTF_KEYUP, 0) ctypes.windll.user32.keybd_event(0xDB, 0, KEYEVENTF_KEYUP, 0) return
- Linux 和 Mac 的另一个解决方案:
在 Mac 和 Linux 中,可以使用十六进制代码输入 unicode 字符。维基百科上有关于此的文章。我正在为 Mac 编写程序,所以我在键盘设置中启用了 Unicode Hex Input 并编写了以下代码:
def type_unicode(word): for c in word: c = '%04x' % ord(c) pyautogui.keyDown('optionleft') pyautogui.typewrite(c) pyautogui.keyUp('optionleft')
