Pygame无法从PS3控制器读取任何值

时间:2018-07-09 20:00:48

标签: python controller pygame external

我一直在尝试设置PS3控制器并能够从中读取模拟输入值,但是每当我按下或移动任何操纵杆时,它都不会读取任何内容,并且对所有内容都返回false。

我一直在使用在控制器上在线找到的各种测试代码,但它们似乎都不起作用。我开始认为这可能是硬件问题,但我仍然不确定。

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

1 个答案:

答案 0 :(得分:0)

代码工作正常。显然,我必须下载一组特定的驱动程序,以使PS3控制器与Windows兼容,因此它可以作为XBOX 360控制器读取。

在线上有一些教程使用SCP ToolKit驱动程序安装程序来使控制器兼容,但是确实做到了,因此由于某种原因我无法使用蓝牙鼠标。