1

I am trying to write a python script that will type out lines from a text document as if they were coming from a keyboard. I already have a code snippet working in some apps (see below), and this types every line from the file I open correctly, I tested the output into notepad++ for example and it types it all out.

import keyboard
import time

time.sleep(3) """ this gives me enough time to alt-tab into the game (Witcher 3) 
that I am trying to have the keypresses inserted into, I also tried some code with 
win32gui that brough the Witcher 3 app to the front, but this is simpler. """

with open('w3recipes.txt', 'r', encoding='utf-8') as recipes:
    for line in recipes:
        keyboard.write(line)
        time.sleep(0.05)

The issue is that these keystrokes are not registered by Witcher 3, the game I am trying to write all these keystrokes to. I tried changing the game from fullscreen to windowed with no luck, and I tried compiling the script to a .exe and running it as an admin as well, no dice. I also tried the pynput library as opposed to the keyboard library used here and that yielded the same result.

Any help would be appreciated, I am trying to write a few hundred console commands to this game and there is no newline character in the game's console; it only supports 1 command at a time before hitting enter. My only other option is sitting here copy-pasting all the lines in which would be tiresome.

Thanks in advance.

1 Answer 1

1

Use another library, pydirectinput. It's an updated version of pyautogui, and I've found it works with most (if not all) games.

Quoting from docs:

This library aims to replicate the functionality of the PyAutoGUI mouse and keyboard inputs, but by utilizing DirectInput scan codes and the more modern SendInput() win32 function. PyAutoGUI uses Virtual Key Codes (VKs) and the deprecated mouse_event() and keybd_event() win32 functions. You may find that PyAutoGUI does not work in some applications, particularly in video games and other software that rely on DirectX. If you find yourself in that situation, give this library a try!

Write functions:

>>> import pyautogui
>>> import pydirectinput
>>> pydirectinput.moveTo(100, 150) # Move the mouse to the x, y coordinates 100, 150.
>>> pydirectinput.click() # Click the mouse at its current location.
>>> pydirectinput.click(200, 220) # Click the mouse at the x, y coordinates 200, 220.
>>> pydirectinput.move(None, 10)  # Move mouse 10 pixels down, that is, move the mouse relative to its current position.
>>> pydirectinput.doubleClick() # Double click the mouse at the
>>> pydirectinput.press('esc') # Simulate pressing the Escape key.
>>> pydirectinput.keyDown('shift')
>>> pydirectinput.keyUp('shift')
# And this is the one you want,
>>> pydirectinput.write('string') # Write string
>>> pydirectinput.typewrite("string")
4
  • Hi, thanks for the reply! I tried pydirectinput, but it seemed that certain characters were not being typed from the strings. As it turns out, I went on the website for the library (pypi.org/project/PyDirectInput) and at the bottom it says that certain characters don't work, particularly any key that is the Shift of another key so abc78 works, but ABC&* will not type anything at all. Unfortunately, my commands have parenthesis and capital letters in them, so this isn't going to work for me, but thanks!
    – Mitchell M
    Commented Mar 28, 2022 at 5:15
  • Nevermind, I guess I'm pretty dumb. I can just split every string in my command list when a capital letter or shifted character appears and use keyDown('Shift') and then keyUp. I guess it will work after all!
    – Mitchell M
    Commented Mar 28, 2022 at 5:21
  • @MitchellM so would it work for you? Does it work in game?
    – Codeman
    Commented Mar 28, 2022 at 6:41
  • Yes I got it to work after about an hour. Thanks for your help!
    – Mitchell M
    Commented Mar 28, 2022 at 21:30

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.