Skip to main content

Python mouse and keyboard input automation for Windows using Direct Input.

Project description

PyDirectInput

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!

This package is intended to be used in conjunction with PyAutoGUI. You can continue to use PyAutoGUI for all of its cool features and simply substitute in PyDirectInput for the inputs that aren't working. The function interfaces are the same, but may not implement all optional parameters and features.

Want to see a missing feature implemented? Why not give it a try yourself! I welcome all pull requests and will be happy to work with you to get a solution fleshed out. Get involved in open source! Learn more about programming! Pad your resume! Have fun!

Example Usage

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

Documentation

The DirectInput key codes can be found by following the breadcrumbs in the documentation here: https://docs.microsoft.com/en-us/windows/win32/api/winuser/ns-winuser-input

You might also be interested in the main SendInput documentation here: https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-sendinput

You can find a discussion of the problems with using vkCodes in video games here: https://stackoverflow.com/questions/14489013/simulate-python-keypresses-for-controlling-a-game

Testing

To run the supplied tests: first setup a virtualenv. Then you can pip install this project in an editable state by doing pip install -e .. This allows any edits you make to these project files to be reflected when you run the tests. Run the test file with python3 tests.

I have been testing with Half-Life 2 to confirm that these inputs work with DirectX games.

Features Implemented

  • Fail Safe Check
  • Pause
  • position()
  • size()
  • moveTo(x, y)
  • move(x, y) / moveRel(x, y)
  • mouseDown()
  • mouseUp()
  • click()
  • keyDown()
  • keyUp()
  • press()
  • write() / typewrite()

Features NOT Implemented

  • scroll functions
  • drag functions
  • hotkey functions
  • support for special characters (ie. '!', '@', '#'...)
  • ignored parameters on mouse functions: duration, tween, logScreenshot
  • ignored parameters on keyboard functions: logScreenshot

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

PyDirectInput-1.0.0.tar.gz (6.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

PyDirectInput-1.0.0-py3-none-any.whl (7.9 kB view details)

Uploaded Python 3

File details

Details for the file PyDirectInput-1.0.0.tar.gz.

File metadata

  • Download URL: PyDirectInput-1.0.0.tar.gz
  • Upload date:
  • Size: 6.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/45.2.0 requests-toolbelt/0.9.1 tqdm/4.42.1 CPython/3.8.0

File hashes

Hashes for PyDirectInput-1.0.0.tar.gz
Algorithm Hash digest
SHA256 92a2c7ee7c9c41c37e46d1047cb9643da2a10026e8222e4207992de8f33e855b
MD5 5e3cae4ca42a06cbe84a3c52848e96ca
BLAKE2b-256 9e61e552cef6e5ff96a72d83e571114d05e7666e380dd9c7eff75d46119d986a

See more details on using hashes here.

File details

Details for the file PyDirectInput-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: PyDirectInput-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 7.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/45.2.0 requests-toolbelt/0.9.1 tqdm/4.42.1 CPython/3.8.0

File hashes

Hashes for PyDirectInput-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 3cbd33d5eeb6eba709e5a26c7718d7ee01820e0c21b7467698cbf3bfe6659c76
MD5 b3acacb1164e0d5410ddeac5434d8d52
BLAKE2b-256 2c3755742bc281cfc48ef1f1be5570f44d8fd1b92e5f261c8d2542516be99e70

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page