简体   繁体   中英

Trouble simulating key presses

I'm trying to build an AI for an NES game using a python script to interact with an emulator. I couldn't really find an API for said emulator (Mesen) so I decided to use OpenCV to look at the screen and somehow simulate keyboard presses to give inputs to the game. However, the two keyboard libraries I found (keyboard and pynput) and the Windows 32 API don't seem to be able to give inputs to the emulator. Text editors can detect the keys pressed using these libraries but the emulator seems to just ignore it. I have tried it with a different GBA emulator (Visual Boy Advance) and that can't detect the keyboard presses either.

For anyone else who stumbles by this and is having the same problem (I am also using the mesen emulator). Use the pydirectinput library.

import pyautogui
import pydirectinput

pydirectinput.press("s")

https://pypi.org/project/PyDirectInput/

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