pyPS4Controller is a light module (less than 30KB) without any dependencies designed to provide hooks for PS4 Controller events on Linux.
sudo pip install pyPS4Controller
- Connect your controller to Bluetooth. Either via GUI, bluetoothctl, or ds4drv (Not recommended, see why at the bottom).
- Check which interfaces you have available at
/dev/input
vials -la /dev/input
command. You are looking forjsN
interfaces. When you connect a new device, there should be a new file created and its typically starts atjs0
. The more controllers/joysticks are connected the morejsN
interfaces you will see. So if you have only one controller/joystick connected, you should only see 1 interface:/dev/input/js0
, take a note of it as we will need it in next step. If you have multiple controllers and not sure which interface belongs to which controller, you can disconnect it see which interface is no longer available. - Copy the code bellow into a Python file, lets say
test.py
from pyPS4Controller.controller import Controller class MyController(Controller): def __init__(self, **kwargs): Controller.__init__(self, **kwargs) controller = MyController(interface="/dev/input/js0", connecting_using_ds4drv=False) controller.listen()
- Now run that file like so
python test.py
and use your controller. You will see output on your screen based on the input to your controller. You can bind your own logic to each one of those events. Lets say you want print "Hello world" on X press and "Goodbye world" on X release then the code would look like this: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)
Here is a list of all the events you can override in a similar manner:
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
This module was originally written to work with ds4drv but I have experienced a number of problems like:
- Random events for R3 joystick and some button presses were coming through when controller was not even used and they would also be generated when controller was in use. This was the biggest issue I could not get over.
- My controller would frequently disconnect from ds4drv.
- Pairing it back in rare cases took many retries.
- Not all events are sent to the interface. If you decide to connect through ds4drv those button events are not supported:
- L3 & R3 Clicks/Releases (Joysticks work, its just the click on the top of the joystick is not detected)
- Events on the Playstation button is not detected
- Share button events are not detected
If you still want to connect using ds4drv here is a video tutorial on how to do it or you can follow the steps bellow:
-
First, you have to make sure you can connect your PS4 controller with ds4drv. You can do it 1 of 2 ways and you only need to do this once.
- Manually by following this instructions
- Automatically by running
py3ps4c init
(if you are using python3) orpy2ps4c init
(if you are using python2) in your terminal.
-
(Optional) connect your controller directly to the computer's Bluetooth module. Once successful, disconnect the controller. If you don't do this, in the next step you may see this error:
Unable to connect to detected device: Failed to set operational mode: [Errno 107] Transport endpoint is not connected
-
Now start
sudo ds4drv
and pressSHARE
+PS4
button on your controller. If pairing fails, you want to try in again, it should eventually connect.
If default button mapping is off (see issue #1), you may want to try to overload the event detection for those buttons. In order to do that you will need to know 3 pieces of information
- button id
- button type
- value the button carries
All of that info you can acquire by turning on debug mode and pressing buttons on the controller to acquire the info above. Here is an example on how to do it.
from pyPS4Controller.controller import Controller, Event
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")
class MyEventDefinition(Event):
def __init__(self, **kwargs):
Event.__init__(self, **kwargs)
# each overloaded function, has access to:
# - self.button_id
# - self.button_type
# - self.value
# use those variables to determine which button is being pressed
def x_pressed(self):
return self.button_id == 0 and self.button_type == 1 and self.value == 1
def x_released(self):
return self.button_id == 0 and self.button_type == 1 and self.value == 0
controller = MyController(interface="/dev/input/js0", connecting_using_ds4drv=False, event_definition=MyEventDefinition)
controller.debug = True # you will see raw data stream for any button press, even if that button is not mapped
# you can start listening before controller is paired, as long as you pair it within the timeout window
controller.listen(timeout=60)
- Mouse pad events and clicks are not detected
- Sensor information (accelerometers and/or gyro) is not detected