Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python simulating 'enter' keypress in a game

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.

like image 841
Ronan Avatar asked Nov 15 '16 20:11

Ronan


People also ask

Can Python simulate keyboard input?

Simulate Keyboard Using the PyAutoGUI Library in Python This library can move the mouse cursor and click over windows and applications, send key events to type characters and execute hotkeys, take screenshots, move, resize, minimize, maximize, and locate applications on the screen, and display alert messages, etc.

How do you simulate keystrokes?

To simulate native-language keystrokes, you can also use the [Xnn] or [Dnn] constants in the string passed to the Keys method. nn specifies the virtual-key code of the key to be “pressed”. For instance, [X221]u[X221]e will “type” the u and e characters with the circumflex accent.


2 Answers

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

like image 183
Ronan Avatar answered Sep 24 '22 00:09

Ronan


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.

like image 31
Long Luu Avatar answered Sep 24 '22 00:09

Long Luu