Basj
Basj

Reputation: 46363

Typewrite ! character with pyautogui

This works to simulate the keystrokes:

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

except that:

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:

Upvotes: 2

Views: 15368

Answers (4)

Zach Patmore
Zach Patmore

Reputation: 1

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)

Upvotes: 0

EasyWay Coder
EasyWay Coder

Reputation: 341

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)

Upvotes: 0

furas
furas

Reputation: 143095

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 !!!

Upvotes: 4

Basj
Basj

Reputation: 46363

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

Upvotes: 0

Related Questions