Python simulating 'enter' keypress in a game

Ronan picture Ronan · Nov 15, 2016 · Viewed 12.3k times · Source

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.

Answer

Ronan picture Ronan · Nov 16, 2016

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