This Python script uses the pygame library to read and print joystick input details such as axis movements, button presses/releases, and D-pad (hat) movements.
- Python 3.x
- pygame library
-
Ensure you have Python 3 installed. You can download it from the official Python website.
-
Install the
pygamelibrary using pip:pip install pygame
-
Connect your joystick to your computer.
-
Save the script to a file, for example,
joystick_reader.py. -
Run the script:
python joystick_reader.py
-
The script will print the joystick's name and the number of axes, buttons, and hats it has.
-
It will then continuously monitor and print details about axis movements, button presses/releases, and D-pad movements. The script uses a deadzone threshold of
0.1to filter out minor noise in axis movements. -
To exit the script, press
Ctrl+Cin the terminal.
-
Joystick Initialization:
- The script initializes pygame and the joystick.
- It checks if at least one joystick is connected and exits if none are found.
- It prints the joystick's name and the number of axes, buttons, and hats.
-
Event Handling:
- The script enters a loop where it processes events.
- For
JOYAXISMOTIONevents, it prints the value of the axis that moved. - For
JOYBUTTONDOWNandJOYBUTTONUPevents, it prints the button that was pressed or released. - For
JOYHATMOTIONevents, it prints the direction of the D-pad movement.
-
Button and Axis Names:
- The script uses predefined lists for button, axis, and hat names. These can be customized as needed.
Joystick name: Xbox 360 Controller Number of axes: 6 Number of buttons: 11 Number of hats: 1 Left Stick X value: 0.235 Button A pressed D-pad moved up
- The script uses a deadzone threshold of
0.1for axis movements to avoid printing small, unintentional movements. - Customize the
button_names,axis_names, andhat_nameslists to match your joystick's configuration if necessary.
This project is licensed under the MIT License.