如何识别使用pygame在PS4控制器上按下哪个按钮

时间:2017-10-04 05:27:49

标签: python raspberry-pi pygame

我正在使用Raspberry Pi 3来控制机器人车辆。我已使用ds4drv成功将我的PS4控制器链接到RPi。我有以下代码正在工作并输出"按下按钮" /"按钮已发布"当使用pygame在PS4控制器上按下/释放按钮时。我想知道如何识别正在按下哪个按钮。

ps4_controller.py

import pygame

pygame.init()

j = pygame.joystick.Joystick(0)
j.init()

try:
    while True:
        events = pygame.event.get()
        for event in events:
            if event.type == pygame.JOYBUTTONDOWN:
                print("Button Pressed")
            elif event.type == pygame.JOYBUTTONUP:
                print("Button Released")

except KeyboardInterrupt:
    print("EXITING NOW")
    j.quit()

4 个答案:

答案 0 :(得分:6)

想出一个黑客:

PS4按钮的编号如下:

0 = SQUARE

1 = X

2 = CIRCLE

3 = TRIANGLE

4 = L1

5 = R1

6 = L2

7 = R2

8 = SHARE

9 = OPTIONS

10 = LEFT ANALOG PRESS

11 = RIGHT ANALOG PRESS

12 = PS4 ON BUTTON

13 = TOUCHPAD PRESS

要确定按下了哪个按钮,我使用j.get_button(int),传入匹配的按钮整数。

示例:

import pygame

pygame.init()

j = pygame.joystick.Joystick(0)
j.init()

try:
    while True:
        events = pygame.event.get()
        for event in events:
            if event.type == pygame.JOYBUTTONDOWN:
                print("Button Pressed")
                if j.get_button(6):
                    # Control Left Motor using L2
                elif j.get_button(7):
                    # Control Right Motor using R2
            elif event.type == pygame.JOYBUTTONUP:
                print("Button Released")

except KeyboardInterrupt:
    print("EXITING NOW")
    j.quit()

答案 1 :(得分:3)

你真的很亲密!通过一些调整,代码变成了这个:

import pygame

pygame.init()
j = pygame.joystick.Joystick(0)
j.init()

try:
    while True:
        events = pygame.event.get()
        for event in events:
            if event.type == pygame.JOYAXISMOTION:
                print(event.dict, event.joy, event.axis, event.value)
            elif event.type == pygame.JOYBALLMOTION:
                print(event.dict, event.joy, event.ball, event.rel)
            elif event.type == pygame.JOYBUTTONDOWN:
                print(event.dict, event.joy, event.button, 'pressed')
            elif event.type == pygame.JOYBUTTONUP:
                print(event.dict, event.joy, event.button, 'released')
            elif event.type == pygame.JOYHATMOTION:
                print(event.dict, event.joy, event.hat, event.value)

except KeyboardInterrupt:
    print("EXITING NOW")
    j.quit()

我发现写一些有用的资源包括pygame's event documentation,使用python' dir函数来查看python对象具有哪些属性,以及documentation for pygame's parent C library, SDL如果你想更深入地解释财产的实际含义。我包括字典访问版本(使用event.dict)以及属性访问版本(仅使用event.whatever_the_property_name_is)。请注意event.button仅为您提供一个数字;您可以手动创建每个按钮编号在控制器上的含义的映射。希望这能搞定它!

答案 2 :(得分:0)

有点晚了,但是如果仍然有人在寻找解决方案,我创建了一个模块:pyPS4Controller,该控制器上的所有按钮事件都已经映射,可以像这样覆盖它们:

from pyPS4Controller.controller import Controller


class MyController(Controller):

    def __init__(self, **kwargs):
        Controller.__init__(self, **kwargs)

    def on_x_press(self):
       print("Hello world")

    def on_x_release(self):
       print("Goodbye world")

controller = MyController(interface="/dev/input/js0", connecting_using_ds4drv=False)
# you can start listening before controller is paired, as long as you pair it within the timeout window
controller.listen(timeout=60)

这只是一个事件的示例。还有更多可以覆盖的事件,例如:

on_x_press
on_x_release
on_triangle_press
on_triangle_release
on_circle_press
on_circle_release
on_square_press
on_square_release
on_L1_press
on_L1_release
on_L2_press
on_L2_release
on_R1_press
on_R1_release
on_R2_press
on_R2_release
on_up_arrow_press
on_up_down_arrow_release
on_down_arrow_press
on_left_arrow_press
on_left_right_arrow_release
on_right_arrow_press
on_L3_up
on_L3_down
on_L3_left
on_L3_right
on_L3_at_rest  # L3 joystick is at rest after the joystick was moved and let go off
on_L3_press  # L3 joystick is clicked. This event is only detected when connecting without ds4drv
on_L3_release  # L3 joystick is released after the click. This event is only detected when connecting without ds4drv
on_R3_up
on_R3_down
on_R3_left
on_R3_right
on_R3_at_rest  # R3 joystick is at rest after the joystick was moved and let go off
on_R3_press  # R3 joystick is clicked. This event is only detected when connecting without ds4drv
on_R3_release  # R3 joystick is released after the click. This event is only detected when connecting without ds4drv
on_options_press
on_options_release
on_share_press  # this event is only detected when connecting without ds4drv
on_share_release  # this event is only detected when connecting without ds4drv
on_playstation_button_press  # this event is only detected when connecting without ds4drv
on_playstation_button_release  # this event is only detected when connecting without ds4drv

{@ {3}}

中提供了完整的文档

答案 3 :(得分:-2)

SmartLifecycleRoleController

按下欢乐按钮可使用,但PXV(玩家x速度) 释放控制器时不会重置为零