Learn how to program
by playing video games.

PyAutoGUI not working? Use DirectInput

PyAutoGUI not working? Use DirectInput

February 24, 2020

Since my video game botting tutorial, where we used PyAutoGUI, multiple people have told me that PyAutoGUI doesn't work with the video game they're playing. So in this video, I'm going to explain what's going on and show you how you can fix it.

Links
GitHub repo for PyDirectInput: https://github.com/learncodebygaming/pydirectinput
PyPI for PyDirectInput: https://pypi.org/project/PyDirectInput/
PyAutoGUI botting tutorial playlist: https://www.youtube.com/playlist?list=PL1m2M8LQlzfJDe1xV1py5EusLt8FFzhMA
GitHub repo for PyAutoGUI: https://github.com/asweigart/pyautogui

So why doesn't PyAutoGUI work with some video games? On Windows, PyAutoGUI uses a slightly older, slightly deprecated Windows API to simulate mouse and keyboard inputs. Normally this is fine, but when you’re interacting with programs that make heavy use of DirectX, like most 3D video games do, you can run into problems. If you don’t know what DirectX is, it's just a collection of libraries and APIs, written by Microsoft, to make video game development easier. It's been super successful, and it’s basically why Windows dominates PC gaming. One of the subsets of DirectX is DirectInput. And DirectInput is basically an easier and more powerful interface for video game developers to use when dealing with user input for their game. A lot of times, game developers will choose to only support DirectInput, because it's less development and players aren't likely to ever notice.

So, to solve the problem we have, we just need to get PyAutoGUI to use the DirectInput Windows APIs instead of what it’s currently using. So I wrote a Python package to do exactly that. I’ve called it PyDirectInput. And just to get exact about what PyAutoGUI does wrong, and how my library fixes it, let me read the opening paragraph of the README to you, and then I'll show you how to use it in practice.

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

You install PyDirectInput simply by using: pip install pydirectinput

If you have a simple PyAutoGUI script like this:

import pyautogui
import time

time.sleep(4)
pyautogui.keyDown('w')
time.sleep(1)
pyautogui.keyUp('w')

You can get it to with with DirectInput games simply by replacing pyautogui with pydirectinput. I've made all the function declarations the same between the two projects, so you can easily swap between them.

import pyautogui
import pydirectinput
import time

time.sleep(4)
pydirectinput.keyDown('w')
time.sleep(1)
pydirectinput.keyUp('w')

I haven't implemented every feature of PyAutoGUI in PyDirectInput. All of the screen reading capabilities you should continue to use PyAutoGUI for. Another big one is mouse movement with a duration, where it slowly moves the cursor across the screen, I haven't implemented that. When you use the moveTo() features in PyDirectInput, it's just going to instantly jump to that spot on the screen. In the README I've noted all the features that I haven't had time to port.

So if you need any of these missing features, this is a great chance to get involved in an open source project. Send me a pull request when you've got something working, or I'm happy to work with you if you need some help. If you've never contributed to open source before this should be a pretty friendly way to get into it, because you've got all the PyAutoGUI source code to look at, and you've got the work I've done already to reference, so it should just be a lot of copy/pasting and fill in the blank type stuff. And testing, of course.


How To Send Inputs to Multiple Windows and Minimized Windows with Python
Let's explore using SendMessage to send automated inputs to multiple windows at once, or to windows that are minimized or in the background. I'll share …
AP Computer Science A - Study Session
Get prepared for the AP Computer Science A exam! I'll let you know what to expect and go through some example questions from the official …
How to make a Video Game in Java (2D Basics)
This project will get you making your first game in Java! Take my starter code (I explain how it works) and build your own game! …
Ben Johnson My name is Ben and I help people learn how to code by gaming. I believe in the power of project-based learning to foster a deep understanding and joy in the craft of software development. On this site I share programming tutorials, coding-game reviews, and project ideas for you to explore.