Skip to main content

Tools to get Joystick events.

Project description

Python joystick event handler.

Simple Example

from pyjoystick.sdl2 import Key, Joystick, run_event_loop

def print_add(joy):
    print('Added', joy)

def print_remove(joy):
    print('Removed', joy)

def key_received(key):
    print('Key:', key)

run_event_loop(print_add, print_remove, key_received)

Qt Integration

The code below displays a label with the most recent key’s value. A green ball will move around as you press the HAT button on the controller.

# App with a green ball in the center that moves when you press the HAT buttons
import pyjoystick
from pyjoystick.sdl2 import Key, Joystick, run_event_loop
# from pyjoystick.pygame import Key, Joystick, run_event_loop
from qt_thread_updater import ThreadUpdater

from qtpy import QtWidgets, QtGui, QtCore


app = QtWidgets.QApplication()

updater = ThreadUpdater()

main = QtWidgets.QWidget()
main.setLayout(QtWidgets.QHBoxLayout())
main.resize(800, 600)
main.show()

lbl = QtWidgets.QLabel()  # Absolute positioning
main.layout().addWidget(lbl, alignment=QtCore.Qt.AlignTop)

mover = QtWidgets.QLabel(parent=main)  # Absolute positioning
mover.resize(50, 50)
mover.point = main.rect().center()
mover.move(mover.point)
mover.show()

def svg_paint_event(self, event):
    painter = QtGui.QPainter(self)
    painter.setRenderHint(painter.Antialiasing, True)

    # Get Black Background
    rect = self.rect()
    center = rect.center()
    radius = 20

    # Colors
    painter.setBrush(QtGui.QColor('green'))  # Fill color
    painter.setPen(QtGui.QColor('black'))  # Line Color

    # Draw
    painter.drawEllipse(center, radius, radius)

    painter.end()

mover.paintEvent = svg_paint_event.__get__(mover, mover.__class__)

def handle_key_event(key):
    updater.now_call_latest(lbl.setText, '{}: {} = {}'.format(key.joystick, key, key.value))

    # print(key, '-', key.keytype, '-', key.number, '-', key.value)

    if key.keytype != Key.HAT:
        return

    if key.value == Key.HAT_UP:
        mover.point.setY(mover.point.y() - 10)
    elif key.value == Key.HAT_DOWN:
        mover.point.setY(mover.point.y() + 10)
    if key.value == Key.HAT_LEFT:
        mover.point.setX(mover.point.x() - 10)
    elif key.value == Key.HAT_UPLEFT:
        mover.point.setX(mover.point.x() - 5)
        mover.point.setY(mover.point.y() - 5)
    elif key.value == Key.HAT_DOWNLEFT:
        mover.point.setX(mover.point.x() - 5)
        mover.point.setY(mover.point.y() + 5)
    elif key.value == Key.HAT_RIGHT:
        mover.point.setX(mover.point.x() + 10)
    elif key.value == Key.HAT_UPRIGHT:
        mover.point.setX(mover.point.x() + 5)
        mover.point.setY(mover.point.y() - 5)
    elif key.value == Key.HAT_DOWNRIGHT:
        mover.point.setX(mover.point.x() + 5)
        mover.point.setY(mover.point.y() + 5)
    updater.now_call_latest(mover.move, mover.point)

# If it button is held down it should be repeated
repeater = pyjoystick.HatRepeater(first_repeat_timeout=0.5, repeat_timeout=0.03, check_timeout=0.01)

mngr = pyjoystick.ThreadEventManager(event_loop=run_event_loop,
                                     handle_key_event=handle_key_event,
                                     button_repeater=repeater)
mngr.start()

# Find key functionality
btn = QtWidgets.QPushButton('Find Key:')

def find_key():
    key = mngr.find_key(timeout=float('inf'))
    if key is None:
        btn.setText('Find Key:')
    else:
        btn.setText('Find Key: {} = {}'.format(key, key.value))

btn.clicked.connect(find_key)
main.layout().addWidget(btn, alignment=QtCore.Qt.AlignTop)

app.exec_()

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

pyjoystick-1.1.9.tar.gz (1.1 MB view details)

Uploaded Source

Built Distribution

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

pyjoystick-1.1.9-py3-none-any.whl (1.2 MB view details)

Uploaded Python 3

File details

Details for the file pyjoystick-1.1.9.tar.gz.

File metadata

  • Download URL: pyjoystick-1.1.9.tar.gz
  • Upload date:
  • Size: 1.1 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/47.1.0 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.8.1

File hashes

Hashes for pyjoystick-1.1.9.tar.gz
Algorithm Hash digest
SHA256 51519ae0368bad7d0fc77dbeefae2268df54c6f5636a0553923e082a4824291e
MD5 5c417895a1053c07dc47694326673578
BLAKE2b-256 923e9cf9383af45f97e7fef48970165066392013ea63bbba6fd1d76f62baceea

See more details on using hashes here.

File details

Details for the file pyjoystick-1.1.9-py3-none-any.whl.

File metadata

  • Download URL: pyjoystick-1.1.9-py3-none-any.whl
  • Upload date:
  • Size: 1.2 MB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/47.1.0 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.8.1

File hashes

Hashes for pyjoystick-1.1.9-py3-none-any.whl
Algorithm Hash digest
SHA256 1bc656a5f7283f32e03ed43e36831dbdd0196bdb03b926a74ce95c2ded451725
MD5 5a55dafe5918a9f6f6ddf2de897aa59b
BLAKE2b-256 e4ac86b597a0b7f8829d10046770b6c4a75666424f03d5a7fedec4e9e64d1a1b

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