| ❌ | ❌ | ❌ | ❌ | ❌ | ❌ | ✅ |
|---|
PlayStation Controller
- class PlayStationController(joystick_deadzone=10)
Use a Sony® PlayStation® controller as a sensor in your projects to control them remotely.
This has been tested on the DUALSHOCK®4 wireless controller for PlayStation®4 and the DualSense™ wireless controller for PlayStation®5, but similar gamepads may also work.
Pair the controller in the hub menu first. When your program runs, it uses the controller that is currently connected.
For tips on connectivity and pairing, see below.
Added in version 4.1.
- Parameters:
joystick_deadzone (Number, %) – Joystick deadzone (0 to 100). Values below this threshold in both axes will be reported as 0 to prevent stick drift.
- Raises:
OSError – If the controller is not connected.
- name() str | None
Gets the Bluetooth name of the connected controller.
- Returns:
Bluetooth name of the controller, or
Noneif it is not known.- Raises:
OSError – If the controller is not connected.
- buttons.pressed() set[Button]
Checks which buttons are currently pressed.
- Returns:
Set of pressed buttons.
Buttons include:
Button.A(cross),Button.B(circle),Button.X(square), andButton.Y(triangle).Button.UP,Button.DOWN,Button.LEFT,Button.RIGHT(direction pad). At most two of these can be pressed at the same time.Button.LBandButton.RB(L1 and R1).Button.LJandButton.RJ(L3 and R3, pressing the joysticks).Button.VIEW(share or create),Button.MENU(options),Button.GUIDE(the PlayStation logo), andButton.UPLOAD(pressing the touchpad).
Note
The buttons are currently reported using the names of the equivalent buttons on the Xbox controller, as listed above. This will change in a future version.
- joystick_left() tuple[int, int]: %
Gets the left joystick position as percentages between -100% and 100%. The center position is (0, 0). A square deadzone is applied: if both axes are within the deadzone, both are reported as 0.
- Returns:
Tuple of X (horizontal) and Y (vertical) position.
- Raises:
OSError – If the controller is not connected.
- joystick_right() tuple[int, int]: %
Gets the right joystick position as percentages between -100% and 100%. The center position is (0, 0). A square deadzone is applied: if both axes are within the deadzone, both are reported as 0.
- Returns:
Tuple of X (horizontal) and Y (vertical) position.
- Raises:
OSError – If the controller is not connected.
- triggers() tuple[int, int]: %
Gets the left (L2) and right (R2) trigger positions as percentages between 0% and 100%.
- Returns:
Tuple of left and right trigger positions.
- Raises:
OSError – If the controller is not connected.
- dpad() int
Gets the direction-pad value.
1is up,2is up-right,3is right,4is down-right,5is down,6is down-left,7is left,8is up-left, and0is not pressed.This is essentially the same as reading the state of the
Button.UP,Button.RIGHT,Button.DOWN, andButton.LEFTbuttons, but this method conveniently returns a number that indicates a direction.- Returns:
Direction-pad position, indicating a direction.
- Raises:
OSError – If the controller is not connected.
PlayStation Controller Pairing Instructions
The first time you use a controller with a hub, you will need to pair them using the hub menu:
On the hub, go to the settings tab and choose
Add new gamepad.Put the controller in pairing mode: press and hold the share (or create) button and the PlayStation button at the same time until the light starts flashing rapidly.
Select the controller on the hub when it appears in the list.
When the connection is successful, the light on the controller stops flashing. You can now start your program. Only one controller can be connected at a time.
Repeat Connections
If you keep using the same controller with the same hub, you can simply press the PlayStation button on the controller the next time and it will connect to the hub automatically.
If you connect the controller to your console or to another device in the meantime, you will need to pair it with the hub again as described above.