PyDirectInput

raw JSON →
1.0.4 verified Fri May 01 auth: no python maintenance

A Python library for automating mouse and keyboard input on Windows using Direct Input (SendInput), bypassing limitations of pyautogui in DirectX games and applications. Current version: 1.0.4, updated 2021. No active development, but stable.

pip install pydirectinput
error ModuleNotFoundError: No module named 'pywin32'
cause Missing dependency for SendInput API.
fix
pip install pywin32
error OSError: [WinError 87] The parameter is incorrect.
cause Invalid mouse coordinates or button number passed to click() or moveRel(). Coordinates must be integers and within the screen bounds.
fix
Ensure coordinates are positive integers and check screen size: from win32api import GetSystemMetrics; print(GetSystemMetrics(0), GetSystemMetrics(1))
error AttributeError: module 'pydirectinput' has no attribute 'moveRel'
cause Outdated version (<1.0.0) or incorrect installation. Some attributes renamed.
fix
Upgrade to latest: pip install --upgrade pydirectinput. Check version: pip show pydirectinput.
breaking PyDirectInput is Windows-only. It will fail on macOS or Linux with ImportError.
fix Use conditional imports: if sys.platform == 'win32': import pydirectinput
deprecated The community fork 'pynput' or 'mouse' libraries may be preferred for cross-platform, but PyDirectInput remains best for DirectX games.
fix Consider using pynput for cross-platform needs; stick with PyDirectInput for DirectX game automation.
gotcha PyDirectInput does not support all keyboard characters; complex Unicode or Korean/Japanese characters may not work. Use write() with ASCII characters only.
fix Test your input strings on a text field first. For special characters, map them to keys using keyDown/keyUp.
gotcha If you install both pyautogui and pydirectinput, pydirectinput will override pyautogui's functions globally. This can break code expecting pyautogui behavior.
fix Import pydirectinput after pyautogui if you want to override, or import pydirectinput as a separate namespace: import pydirectinput as di

Basic mouse and keyboard automation using direct input simulation. Note: PyDirectInput overrides pyautogui's functions, so you can use both together.

import pydirectinput
import pyautogui

# Move mouse relative (works in DirectX)
pydirectinput.moveRel(100, 0)

# Click at current position
pydirectinput.click()

# Type text
pydirectinput.write('Hello World', interval=0.1)

# Press key
pydirectinput.press('enter')

# Hold key
pydirectinput.keyDown('shift')
pydirectinput.keyUp('shift')