如何在 Python 中从操纵杆获取输入?

How to Get Input from Joystick in Python?

我正在尝试使用 Python 程序从我拥有的操纵杆(特别是 Logitech Extreme 3D Pro)获取输入。不幸的是,我不知道该怎么做。

我目前有一个使用 PyGame 的工作原型,但我不想使用 PyGame,因为我已经想要同时打开另一个 Tkinter window。

我试过 inputs 库,但每次插入操纵杆时,我总是得到一个 inputs.UnpluggedError

除了PyGame之外,还有其他获取操纵杆输入的方法吗?

我正在使用 MacBook Air 运行 Big Sur。

工作代码:

import os
import pprint
import pygame
import threading

class PS4Controller(object):
    """Class representing the PS4 controller. Pretty straightforward functionality."""

    controller = None
    axis_data = None
    button_data = None
    hat_data = None

    def init(self):
        """Initialize the joystick components"""
        
        pygame.init()
        pygame.joystick.init()
        self.controller = pygame.joystick.Joystick(0)
        self.controller.init()

    def listen(self):
        """Listen for events to happen"""
        
        if not self.axis_data:
            self.axis_data = {}

        if not self.button_data:
            self.button_data = {}
            for i in range(self.controller.get_numbuttons()):
                self.button_data[i] = False

        if not self.hat_data:
            self.hat_data = {}
            for i in range(self.controller.get_numhats()):
                self.hat_data[i] = (0, 0)

        while True:
            for event in pygame.event.get():
                if event.type == pygame.JOYAXISMOTION:
                    self.axis_data[event.axis] = round(event.value,2)
                elif event.type == pygame.JOYBUTTONDOWN:
                    self.button_data[event.button] = True
                elif event.type == pygame.JOYBUTTONUP:
                    self.button_data[event.button] = False
                elif event.type == pygame.JOYHATMOTION:
                    self.hat_data[event.hat] = event.value

                # Insert your code on what you would like to happen for each event here!
                # In the current setup, I have the state simply printing out to the screen.
                
                os.system('clear')
                pprint.pprint(self.button_data)
                pprint.pprint(self.axis_data)
                pprint.pprint(self.hat_data)


def controller_main():
    ps4 = PS4Controller()
    ps4.init()
    ps4.listen()

controller_main()

试试这个 py-joystick 模块: https://pypi.org/project/pyjoystick/ 希望对您有所帮助!

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('received', key)
    if key.value == Key.HAT_UP:
        #do something
    elif key.value == Key.HAT_DOWN:
        #do something
    if key.value == Key.HAT_LEFT:
        #do something
    elif key.value == Key.HAT_UPLEFT:
        #do something
    elif key.value == Key.HAT_DOWNLEFT:
        #do something
    elif key.value == Key.HAT_RIGHT:
        #do something
    elif key.value == Key.HAT_UPRIGHT:
        #do something
    elif key.value == Key.HAT_DOWNRIGHT:
        #do something

run_event_loop(print_add, print_remove, key_received)