pythons pyautogui中反斜杠的编码 [英] encoding of backslash in pythons pyautogui
问题描述
我正在尝试编写一个Python脚本,该脚本通过单击按钮自动运行外部程序,并使用pyautogui提供键盘输入(通常是文件路径)。
现在,如果我尝试使用功能pyautogui.typewriter(filepath),它将始终将反斜杠键入为问号。
这个最小示例:
I am trying write a python script that runs an external program automatically by clicking buttons and give keyboard input (usually file paths) with pyautogui. Now if I try use the funktion pyautogui.typewriter(filepath) it always types the backslashes as questionmarks. This minimal example:
pyautogui.typewrite('\\')
简单地返回?
现在,如果我在系统设置为英语,它会正确返回\
Now, if I change my keyboard layout in the system settings to english, it correctly returns \
我的默认布局是德语,我无法真正更改它,因为由于错误而使程序的后期工作变得混乱日期格式。
关于解决此问题的任何想法吗?
My default layout is german, and I cant really change that because this messes up the later stages of the program due to wrong date formats. Any ideas how to solve this problem?
推荐答案
好的,基于此讨论,我终于有了一个解决方法:
https://github.com/asweigart/pyautogui/issues/46
Ok, I finally have a workaround, based on this discussion: https://github.com/asweigart/pyautogui/issues/46
我在_pyautogui_win.py中进行了修改。并将键盘输入更改为 \。
我使用此方便的工具在键盘上获得了虚拟按键代码:
http://www.delphiforfun.org/Programs/Utilities/KeyCodes.htm#Download
并将其转换为十六进制代码。然后,我通过添加以下内容更改了_keyDown函数:
I tinkered around in _pyautogui_win.py. and changed the keyboard input for '\'. I got the virtual keycodes on my keyboard with this handy tool: http://www.delphiforfun.org/Programs/Utilities/KeyCodes.htm#Download and converted them to hex codes. I then changed the _keyDown function with the addition of this:
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
现在一切正常。我认为该解决方案可以应用于任何非英语键盘布局。
Now everything works fine. I think this solution can be applied to any non-english keyboard layouts.
这篇关于pythons pyautogui中反斜杠的编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!