Search code examples
pythoncontrollerpygameexternal

Pygame not reading any values from PS3 controller


I have been trying to set up a PS3 controller and be able to read analog input values from it, but whenever I press down or move any of the joysticks it doesn't read anything and returns false for everything.

I have been using various test codes I have found online for the controller and none of them seem to work. I'm starting to think it may be a hardware issue, but I'm still unsure.

import os
import pprint
import pygame


class PS3Controller(object):

    controller = None
    name = 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(1)
        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:
            #D - Pad
            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)


if __name__ == "__main__":
    ps3 = PS3Controller()
    ps3.init()
    ps3.listen()

Solution

  • The code works fine. Apparently, I had to download a specific set of drivers to make the PS3 controller compatible with windows so it could be read as an XBOX 360 controller.

    There were some tutorials online that used SCP ToolKit driver installer to make the controller compatible, however, it did make it so I couldn't use my Bluetooth mouse for some reason.