encoding of backslash in pythons pyautogui

蓝咒 提交于 2019-12-23 22:33:27

问题


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

returns simply ?

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?


回答1:


It's not an encoding problem, per se. It has to do with how pyautogui is sending the keyboard input. It is sending a keystroke that is, at least in this case, not considering the keyboard layout (the actual method pyautogui uses for this is platform-specific.)

There isn't a direct solution built into pyautogui for specifying keyboard locale. So, I see a couple different options...

(1) You can create a function that will change your keyboard layout as needed (exactly how you do this is platform-specific) you could even carry this out using pyautogui commands.

(2) Instead of providing the input \\ for backslashes, provide the input that will produce \ on the german keyboard, instead. If necessary, you could define a function that will accept a string argument that translates problem characters in that string into the equivalent pyautogui command(s) that will produce the desired output on the German keyboard. From looking at a picture of a German keyboard, it actually looks like \ and ? are on the same key, so you probably would just need to add a modifier like shift (edit: here's an example of someone doing this with a French keyboard and pyautogui

(3) Consider calling files by another method other than keyboard input, if possible, such as os.system or subprocess.call




回答2:


Ok, I finally have a workaround, based on this discussion: https://github.com/asweigart/pyautogui/issues/46

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.




回答3:


The problem (I think) is that you've escaped the quote also with \'

I honestly don't know how many backslashes you will need (I've never used pyautogui but I think this is a Python problem, not a problem with the package), so try '\\' and '\\' and '\\\'. Beyond that I think you should try to look for another solution.




回答4:


I am having the same problem, but I am working on Linux. The solution for me was just to use the divide.

When I am typing 'cd /home/pi' the '/' never appear, the solution was:

if key == '/':
    pyautogui.press('divide')

It works for me!



来源:https://stackoverflow.com/questions/36040370/encoding-of-backslash-in-pythons-pyautogui

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!