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.2.4.tar.gz (1.1 MB view details)

Uploaded Source

Built Distribution

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

Uploaded Python 3

File details

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

File metadata

  • Download URL: pyjoystick-1.2.4.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.8

File hashes

Hashes for pyjoystick-1.2.4.tar.gz
Algorithm Hash digest
SHA256 d3d676888d743b9e4aa2845f16f55df7fbbfa30db0ff4f033ace0ee33a8f7d29
MD5 b2e0d4722e0beebf2d5a33584988c3f5
BLAKE2b-256 cb0c8cc48bb0f8e9e8b537e53d416de5c55570a63c84d4b5d8e4086391e543fd

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pyjoystick-1.2.4-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.8

File hashes

Hashes for pyjoystick-1.2.4-py3-none-any.whl
Algorithm Hash digest
SHA256 773989a5d796d7f32261d25338d583233ddae9a091c0c13c3fc09d250b0870a9
MD5 571a5810e107ff1bbd336e01ad90ad2a
BLAKE2b-256 a565858dc1f7f4c65b9e9c8e769019cb156e6b8b96fd7f2f8f1f6bfef8717d95

See more details on using hashes here.

Supported by

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