Reputation: 125
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?
Upvotes: 4
Views: 4970
Reputation:
It looks hard but is actually easy what you have to do is just make a variable and type '\\' in it then just write the variable in pyautogui like this:
x = "\\"
pyautogui.write(x)
I think your problem is now solved.
Upvotes: 0
Reputation: 85
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!
Upvotes: 0
Reputation: 125
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.
Upvotes: 2
Reputation: 40891
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
Upvotes: 2
Reputation: 1247
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.
Upvotes: 0