2

This works to simulate the keystrokes:

import pyautogui
pyautogui.typewrite('hello world!', interval=0.1)

except that:

  • it writes hello world§ (with FR keyboard layout)
  • it writes hello world (with EN keyboard layout)

Of course, the desired output should be hello world!.

Is there a workaround?

NB: I don't think it is the same problem than Input unicode string with pyautogui because here it's not a non-ASCII character, but anyway the main answer with a copy/paste hack would not work in my case, as I really want the slow typing with 100ms pause between each keypress.

Here is how to reproduce the error:

  • Windows 7 x64
  • Python 3.6 or Python 2.7
  • pyautogui 0.9.41 or 0.9.48
  • the 2 lines of code mentioned at the beginning
Basj
  • 41,386
  • 99
  • 383
  • 673
  • [The documentation](https://pyautogui.readthedocs.io/en/latest/keyboard.html#the-typewrite-function) shows an example printing `'Hello world!'`, which contains a `!`. It doesn't look like you need to do anything special. Can you provide more information about your environment? – ChrisGPT was on strike Jan 05 '20 at 21:16
  • @Chris I have a standard Windows 7 x64, Python 3.6, pyautogui 0.9.41 ; I tested with both french keyboard layout and english. I updated the question with a screencast. I have no other program running (no autohotkey or similar running at the same time). – Basj Jan 05 '20 at 21:41
  • 1
    This is working for me. It prints `!` as expected. Are you on windows? Did you try deleting the explanation point and entering it again manually? – Jortega Jan 05 '20 at 21:50
  • @Jortega, Yes I tried that. I'm on Windows, and you? I just upgraded to pyautogui 0.9.48 and it's the same :/ I also tried with Py2.7 and it's the same: https://gget.it/xrig/a.gif – Basj Jan 05 '20 at 22:04
  • I just tried on another (very standard) Windows 7 x64 computer + Python3.6 + Pyautogui 0.9.48 and it's the same. – Basj Jan 06 '20 at 08:38
  • it doesn't send asci chars `!` to programs - it sends to system keyboard's codes for key `1` which in standard layout is also used for `!` and system decide what char send to program. If your system has non-standard layout then system may send wrong char. If your layout has assigned `§` to key `1` then system will send `§` to program. Probably only using clipboad you can send it correctly. If you will use clipboar to copy single char and use `time.sleep(0.1)` between chars then you can get similar result. – furas Jan 06 '20 at 22:52
  • Not sure if related, but can you also specify your region/locale settings? – Gino Mempin Jan 07 '20 at 00:10

3 Answers3

4

It doesn't send ascii char ! to program - it sends keyboard's code to system (probably code for key 1 which in standard layout is used for char !) and system decides what char send to program. If your system has non-standard layout then system may send wrong char.

Probably only using clipboad you can send it correctly. If you will use clipboard to copy single char and wait 0.1s between chars then you can get similar result.

import time
import pyperclip
import pyautogui

time.sleep(2)

for char in 'Hello World!':
    pyperclip.copy(char)
    pyautogui.hotkey('ctrl', 'v', interval=0.1)

BTW: using print(pyautogui.__file__) you can find folder with source code and in file _pyautogui_win.py you can see what key codes it uses in Windows.

You should see key codes assigned to chars using also

Window:

print(pyautogui._pyautogui_win.keyboardMapping)

Linux:

print(pyautogui._pyautogui_x11.keyboardMapping)

Maybe if you change values in keyboardMapping then it will send it correctly but for every layout you would have to set different values.

For example on Linux this

import pyautogui

#pyautogui._pyautogui_win.keyboardMapping['!'] = 12
pyautogui._pyautogui_x11.keyboardMapping['!'] = 12

pyautogui.typewrite('!!!')

gives me ### instead of !!!

furas
  • 134,197
  • 12
  • 106
  • 148
  • Hi @furas. How did you guess that the value `12` would fix the issue? I'm trying to display this caracter `~`. – Daishi Dec 04 '20 at 16:38
  • 1
    @Daishi number `12` is keycode (scancode) for key `3` on my QWERTY (US-Keyborad) and it was only example to show how to change it but it is not correct value to get `!`. It gives `#` - at least on QWERTY (US-Keyboard). On my QWERTY (US-Keyboard) original number `10` gives correct `!` without any changes so I couldn't test it with other layouts. I wrote [program using PyGame or Tkinter to get keycode (scancode) for pressed/released key](https://blog.furas.pl/how-to-get-keycode-in-python-using-tkinter-or-pygame.html) and maybe it helps you to find correct value for different – furas Dec 07 '20 at 00:19
  • In fact, the Ctrl-C - Ctrl-V trick is cruical, this should be incorporated into pyautogui as a sort of "rawtype" function. – Michael Jan 14 '22 at 14:17
0

This seems to be a known issue:

https://github.com/asweigart/pyautogui/issues/38

User on Windows 7, Python 3.4, running PyAutoGUI 0.9.30 and a French "AZERTY" keyboard reported being unable to simulate pressing :
Running the unit tests, they got these results:
[...]
a
ba
.Hello world§

https://github.com/asweigart/pyautogui/pull/55

https://github.com/asweigart/pyautogui/issues/137

Basj
  • 41,386
  • 99
  • 383
  • 673
0

Try updating your pyautogui module. If it doesn't work then try this code:

from pyautogui import *

typewrite("Hello World!")
keyDown("shift")
press("1")
keyUp("shift")

OR this code:

from pyautogui import *

a = "Hello World!"
typewrite(a)
Dharman
  • 30,962
  • 25
  • 85
  • 135
EasyWay Coder
  • 331
  • 1
  • 7