简体   繁体   中英

Using python to emulate keypresses in an application

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.

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")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM