Skip to main content

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_()

Release files for pyjoystick 1.2.4

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

Source distribution (sdist)

Source distribution for pyjoystick 1.2.4
File Size Uploaded
pyjoystick-1.2.4.tar.gz 1.1 MB Details

Built distribution (wheel)

Table of built distributions (wheels) for pyjoystick 1.2.4
File Interpreter ABI Platform
pyjoystick-1.2.4-py3-none-any.whl Python 3 none any Details

Total release size: 2.3 MB

Release files / pyjoystick-1.2.4.tar.gz

Download URL pyjoystick-1.2.4.tar.gz
Size 1.1 MB
Tags Source
SHA-256 checksum
How to use checksums
d3d676888d743b9e4aa2845f16f55df7fbbfa30db0ff4f033ace0ee33a8f7d29
BLAKE2b-256 checksum
How to use checksums
cb0c8cc48bb0f8e9e8b537e53d416de5c55570a63c84d4b5d8e4086391e543fd
Upload date
Uploaded using Trusted Publishing?
What is 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

Release files / pyjoystick-1.2.4-py3-none-any.whl

Download URL pyjoystick-1.2.4-py3-none-any.whl
Size 1.2 MB
Tags Python 3
SHA-256 checksum
How to use checksums
773989a5d796d7f32261d25338d583233ddae9a091c0c13c3fc09d250b0870a9
BLAKE2b-256 checksum
How to use checksums
a565858dc1f7f4c65b9e9c8e769019cb156e6b8b96fd7f2f8f1f6bfef8717d95
Upload date
Uploaded using Trusted Publishing?
What is 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

Release history Release notifications | RSS feed

This release

1.2.4 This release

2 release files

1.2.3

2 release files

1.2.2

2 release files

1.2.1

2 release files

1.2.0

2 release files

1.1.11

2 release files

1.1.10

2 release files

1.1.9

2 release files

1.1.8

2 release files

1.1.7

2 release files

1.1.6

2 release files

1.1.5

2 release files

1.1.4

2 release files

1.1.3

1 release file

1.1.2

1 release file

1.1.1

1 release file

1.1.0

1 release file

1.0.3

1 release file

1.0.2

1 release file

1.0.1

1 release file

1.0.0

1 release file

0.0.9

1 release file

0.0.8

1 release file

0.0.7

1 release file

0.0.6

1 release file

0.0.5

1 release file

0.0.4

1 release file

0.0.3

1 release file

0.0.2

1 release file

0.0.1

1 release file

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