Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

encoding of backslash in pythons pyautogui

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?

like image 539
user2160180 Avatar asked Sep 19 '25 02:09

user2160180


2 Answers

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

like image 132
sytech Avatar answered Sep 20 '25 15:09

sytech


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.

like image 42
user2160180 Avatar answered Sep 20 '25 15:09

user2160180