Skip to main content

Overview

docs

Documentation Status

package

PyPI Package latest release PyPI Wheel Supported versions

Library for xbox controllers.

  • Free software: MIT License

Installation

pip install robot-xbox-controller

You can also install the in-development version with:

pip install https://github.com/fmorton/robot-xbox-controller/archive/main.zip

Xbox Controller Example

import pygame

from robot.xbox_controller import XboxController

controller = XboxController().connect()

print("Controller connected")

running = True

while running:
  for event in pygame.event.get():
      if event.type == pygame.QUIT:
          running = False
      elif event.type == pygame.JOYAXISMOTION:
          print(f"Controller axis {event.axis} value {event.value}")
      elif event.type == pygame.JOYBUTTONDOWN:
          print(f"Button {event.button} pressed on controller {event.joy}")

          if event.button == XboxController.BUTTON_XBOX:
              running = False

Xbox Controller Example Using run() and the Controller State

The run() loop maintains the state of the controllers and all buttons for processing by your supplied method, “robot” in the example below.

from robot.xbox_controller import XboxController
from time import sleep


def robot(controller):
  print("runner", controller.state.state_string())

  if controller.state.button_down_milliseconds(XboxController.BUTTON_XBOX):
      return False

  sleep(XboxController.EVENT_LOOP_DELAY)

  return True


controller = XboxController().connect()

controller.run(robot)

The controller.state has the following methods to retrieve the selections.

controller.state.left_x()        # returns x value of left controller (-1 means max forward/1 max backward)
controller.state.left_y()        # returns y value of left controller (-1 means max left/1 max right)
controller.state.left_axis()     # returns left_x, left_y

controller.state.right_x()       # returns x value of right controller (-1 means max forward/1 max backward)
controller.state.right_y()       # returns y value of right controller (-1 means max left/1 max right)
controller.state.right_axis()    # returns right_x, right_y

controller.state.left_trigger()       # returns left trigger value (0 to 1 if fully pressed)
controller.state.right_trigger()      # returns right trigger value (0 to 1 if fully pressed)

controller.state.left_trigger_raw()   # returns left trigger unadjusted value (-1 to 1 if fully pressed)
controller.state.right_trigger_raw()   # returns right trigger unadjusted value (-1 to 1 if fully pressed)

controller.state.button_down_seconds(button)       # how many seconds "button" has been pressed
controller.state.button_down_milliseconds(button)  # how many milliseconds "button" has been pressed

The following button constant values are available in XboxController:

BUTTON_A = 0
BUTTON_B = 1
BUTTON_X = 2
BUTTON_Y = 3
BUTTON_VIEW = 4   #  left middle button
BUTTON_XBOX = 5   #  center middle button
BUTTON_MENU = 6  # right middle button
BUTTON_LEFT_JOYSTICK = 7
BUTTON_RIGHT_JOYSTICK = 8
BUTTON_LEFT_BUMPER = 9
BUTTON_RIGHT_BUMPER = 10
BUTTON_FORWARD = 11
BUTTON_BACKWARD = 12
BUTTON_LEFT = 13
BUTTON_RIGHT = 14

Testing

To run all the tests run:

pytest -s

Metadata

Release files for robot-xbox-controller 0.2.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for robot-xbox-controller 0.2.1
File Size Uploaded Provenance Actions
robot_xbox_controller-0.2.1.tar.gz 6.2 kB Without provenance Details for robot_xbox_controller-0.2.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for robot-xbox-controller 0.2.1
File Interpreter ABI Platform Actions
robot_xbox_controller-0.2.1-py3-none-any.whl Python 3 none any Details for robot_xbox_controller-0.2.1-py3-none-any.whl

Total release size: 12.2 kB

Release files / robot_xbox_controller-0.2.1.tar.gz

Download URL robot_xbox_controller-0.2.1.tar.gz
Size 6.2 kB
Tags Source
SHA-256 checksum
How to use checksums
b38ed6cf18f7a84380b20bb81fd55c332780ce203471793ae6a8273682d7dce8
BLAKE2b-256 checksum
How to use checksums
b515794332d59ec6899dcb2faea7909032215acb586fb4520dea208854b58202
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.13.2

Release files / robot_xbox_controller-0.2.1-py3-none-any.whl

Download URL robot_xbox_controller-0.2.1-py3-none-any.whl
Size 6.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
ec54b06109854b3b329213eb7c798142634fe3878ee293cea1bb93070e8cd313
BLAKE2b-256 checksum
How to use checksums
44ef495f20813723791da8779fe896d3567cc760fb11e30abf41635b292e8b4a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.13.2

Security for robot-xbox-controller 0.2.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page