6

So I've been attempting to get a script to type out a string of text in a video game (Guild Wars 2). Mainly I'm using pyautogui and for the most part it works fine. My issue is it seems I can't get the game to recognize 'enter'. For example if I have the code:

import pyautogui, time
time.sleep(2) #to allow me to have time to switch windows
pyautogui.press('enter')
pyautogui.typewrite("This is a test")
pyautogui.press('enter')

The two "press enter" function will not open and submit the text. If however I manually hit enter, the 3rd line types things out just fine.

I've also tried replacing press('enter') with keyDown followed by keyUp, but with still no results.

I've managed to create a workaround by having python hit F10, and then a separate Autohotkey script hitting enter when F10 is hit, but that is far from ideal. Are there any suggestions?

Extra note from comments: The script by itself works fine in other programs such as notepad. It seems to fail exclusively for the game client.

Ronan
  • 407
  • 3
  • 5
  • 13
  • 2
    try `'return'` or `'\n'` which are other acceptable entries for enter key when using pyautogui. No idea why `'enter'` doesn't work, the problem could be elsewhere ,, – Dart Feld Nov 15 '16 at 20:52
  • return or \n did not work either. I should also specify that this code works just fine in, say notepad++. It seems to be the game client that doesn't like to recognize python input for the enter key. – Ronan Nov 15 '16 at 20:57
  • first you should say what game it is. Game may read keys from system using own method and recognize ENTER in different way. Is `typewrite()` working with this game ? How about `typewrite("\n")` ? – furas Nov 15 '16 at 21:01
  • Seemed like a good idea, but this doesn't seem to be interpreted either. – Ronan Nov 15 '16 at 21:13
  • I also didn't see the first part of the comment. the game is Guild Wars 2. – Ronan Nov 15 '16 at 22:15

2 Answers2

5

For anyone finding their way to this post. The answer is essentially that this can't be done in this fashion due to how most games interpret keypresses. A working system is shown here: Simulate Python keypresses for controlling a game

Community
  • 1
  • 1
Ronan
  • 407
  • 3
  • 5
  • 13
  • Nice to know, thanks for following with what you've found. Pretty sure this could help some people in the future. – Dart Feld Nov 16 '16 at 17:59
  • Since This still seems to come up, I'll add a side note someone told me. Another possible reason for this issue is running the python program in non-admin mode and the game/software in admin. – Ronan Jun 13 '20 at 02:28
1

None of \n nor return works to me, so I have to use pydirectinput: https://github.com/learncodebygaming/pydirectinput

Install it using pip install pydirectinput, then import it and use as the README. Note that the file also needs to run as admin.

Minh-Long Luu
  • 2,393
  • 1
  • 17
  • 39