Skip to main content

Python bindings for vJoy

Project description

pyvjoystick

pyvjoystick is a set of python binding for different virtual devices. Currently vJoy and ViGEm are supported.

vJoy(on github from jshafer817 and njz3). The implementation of vjoy bindings are inspired on pyvjoy's package.

ViGEm bindings are inspired on vgamepad's package.

Requirements

To be able to use vJoy device install vJoy from sourceforge or github. It is recommended to also install the vJoy Monitor and Configure vJoy programs. These should be an option during installation.

To be able to use ViGem device install release. This package ships with a ViGemClient binaries.

Installation

Simple! This package is installable by pip

pip install pyvjoystick

Usage

vJoy example

With this library you can easily set Axis and Button values on any vJoy device. Low-level bindings are provided in pyvjoy._sdk.

import pyvjoystick.vjoy as vjoy

# Pythonic API, item-at-a-time
j = vjoy.VJoyDevice(1)

# turn button number 15 on
j.set_button(15, 1)

# Notice the args are (buttonID,state) whereas vJoy's native API is the other way around.


# turn button 15 off again
j.set_button(15, 0)

# Set X axis to fully left
j.set_axis(vjoy.HID_USAGE.X, 0x1)

# Set X axis to fully right
j.set_axis(vjoy.HID_USAGE.X, 0x8000)

# Also implemented:

j.reset()
j.reset_buttons()
j.reset_povs()


# The 'efficient' method as described in vJoy's docs - set multiple values at once

print(j._data)
# >> > <pyvjoystick.vjoy._sdk._JOYSTICK_POSITION_V2 at 0x.... >


j._data.lButtons = 19  # buttons number 1,2 and 5 (1+2+16)
j._data.wAxisX = 0x2000
j._data.wAxisY = 0x7500

# send data to vJoy device
j.update()


# Lower-level API just wraps the functions in the DLL as thinly as possible, with some attempt to raise exceptions instead of return codes.

XBox360 gamepad

The following python script creates a virtual XBox360 gamepad:

import pyvjoystick.vigem as vg

gamepad = vg.VX360Gamepad()

As soon as the VX360Gamepad object is created, the virtual gamepad is connected to your system via the ViGEmBus driver, and will remain connected until the object is destroyed.

Buttons can be pressed and released through press_button and release_button:

gamepad.press_button(button=vg.XUSB_BUTTON.XUSB_GAMEPAD_A)  # press the A button
gamepad.press_button(button=vg.XUSB_BUTTON.XUSB_GAMEPAD_DPAD_LEFT)  # press the left hat button

gamepad.update()  # send the updated state to the computer

# (...) A and left hat are pressed...

gamepad.release_button(button=vg.XUSB_BUTTON.XUSB_GAMEPAD_A)  # release the A button

gamepad.update()  # send the updated state to the computer

# (...) left hat is still pressed...

All available buttons are defined in XUSB_BUTTON

To control the triggers (1 axis each) and the joysticks (2 axis each), two options are provided by the API.

It is possible to input raw integer values directly:

gamepad.left_trigger(value=100)  # value between 0 and 255
gamepad.right_trigger(value=255)  # value between 0 and 255
gamepad.left_joystick(x_value=-10000, y_value=0)  # values between -32768 and 32767
gamepad.right_joystick(x_value=-32768, y_value=15000)  # values between -32768 and 32767

gamepad.update()

Or to input float values:

gamepad.left_trigger_float(value_float=0.5)  # value between 0.0 and 1.0
gamepad.right_trigger_float(value_float=1.0)  # value between 0.0 and 1.0
gamepad.left_joystick_float(x_value_float=-0.5, y_value_float=0.0)  # values between -1.0 and 1.0
gamepad.right_joystick_float(x_value_float=-1.0, y_value_float=0.8)  # values between -1.0 and 1.0

gamepad.update()

Reset to default state:

gamepad.reset()

gamepad.update()

DualShock4 gamepad

Using a virtual DS4 gamepad is similar to X360:

import pyvjoystick.vigem as vg

gamepad = vg.VDS4Gamepad()

Press and release buttons:

gamepad.press_button(button=vg.DS4_BUTTONS.DS4_BUTTON_TRIANGLE)
gamepad.update()

# (...)

gamepad.release_button(button=vg.DS4_BUTTONS.DS4_BUTTON_TRIANGLE)
gamepad.update()

Available buttons are defined in DS4_BUTTONS

Press and release special buttons:

gamepad.press_special_button(special_button=vg.DS4_SPECIAL_BUTTONS.DS4_SPECIAL_BUTTON_PS)
gamepad.update()

# (...)

gamepad.release_special_button(special_button=vg.DS4_SPECIAL_BUTTONS.DS4_SPECIAL_BUTTON_PS)
gamepad.update()

Special buttons are defined in DS4_SPECIAL_BUTTONS

Triggers and joysticks (integer values):

gamepad.left_trigger(value=100)  # value between 0 and 255
gamepad.right_trigger(value=255)  # value between 0 and 255
gamepad.left_joystick(x_value=0, y_value=128)  # value between 0 and 255
gamepad.right_joystick(x_value=0, y_value=255)  # value between 0 and 255

gamepad.update()

Triggers and joysticks (float values):

gamepad.left_trigger_float(value_float=0.5)  # value between 0.0 and 1.0
gamepad.right_trigger_float(value_float=1.0)  # value between 0.0 and 1.0
gamepad.left_joystick_float(x_value_float=-0.5, y_value_float=0.0)  # values between -1.0 and 1.0
gamepad.right_joystick_float(x_value_float=-1.0, y_value_float=0.8)  # values between -1.0 and 1.0

gamepad.update()
  • Note: The Y axis on joysticks is inverted for consistency with the X360 API.

Directional pad (hat):

gamepad.directional_pad(direction=vg.DS4_DPAD_DIRECTIONS.DS4_BUTTON_DPAD_NORTHWEST)
gamepad.update()

Directions for the directional pad are defined in DS4_DPAD_DIRECTIONS

Reset to default state:

gamepad.reset()

gamepad.update()

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

pyvjoystick-1.2.0.tar.gz (133.4 kB view details)

Uploaded Source

Built Distribution

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

pyvjoystick-1.2.0-py3-none-any.whl (139.0 kB view details)

Uploaded Python 3

File details

Details for the file pyvjoystick-1.2.0.tar.gz.

File metadata

  • Download URL: pyvjoystick-1.2.0.tar.gz
  • Upload date:
  • Size: 133.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.2.1 CPython/3.13.7 Windows/11

File hashes

Hashes for pyvjoystick-1.2.0.tar.gz
Algorithm Hash digest
SHA256 7cf65fa598dfc50de04a985a6514435e89cf2227e8199133f48acb725ee14a3c
MD5 10abb7711b3c06294b4fd39be36011c4
BLAKE2b-256 a526e9f309b8369c0634f3b09fb4a5a00bc639cda56b67dc5d38062fb06f74b5

See more details on using hashes here.

File details

Details for the file pyvjoystick-1.2.0-py3-none-any.whl.

File metadata

  • Download URL: pyvjoystick-1.2.0-py3-none-any.whl
  • Upload date:
  • Size: 139.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.2.1 CPython/3.13.7 Windows/11

File hashes

Hashes for pyvjoystick-1.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 f2a0ea3d34b09a39ee9290e97f7d5c95f989eb2e98c6c49e5500d3df24b53a61
MD5 d722d19996b07d6995e35c975e464a8f
BLAKE2b-256 3d1b25053d7a39343d8d2acd3ab73a134e7d9143a61d70d8e49e8a21c362a3b7

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