The Linux input subsystem exposes a connected joystick as a device file, letting applications read its button and axis activity. Joystick device files are created automatically when controllers connect, commonly as /dev/input/js0, then /dev/input/js1 and /dev/input/js2 for additional devices. This provides a direct path from physical controller movement to a Python application. section 7.5.1 section 7.8
$ ls /dev/input/js* /dev/input/js0
List joystick device files before choosing the controller to inspect or read. section 7.5.1
Use jstest with a joystick device file for a live view of detected axes and buttons. The display identifies available controls, reports every button as on or off, and shows axis values. Press controls one at a time to record the mapping your application needs: on the illustrated PlayStation controller, cross is button 0, circle is button 1, triangle is button 2, and square is button 3. The same process identifies A, B, X, and Y on an Xbox controller. section 7.5.1
A practical joystick workflow is:
- Connect the joystick and list /dev/input/js* to find its device file. section 7.5.1
- Install and run jstest on that file to see its controls and current event state. section 7.5.1
- Press buttons and move axes to note controller-specific numbers and values. section 7.5.1
- Read and parse the resulting joystick events in Python with struct, then use the input in a joystick application. section 7.2 section 7.8
Joystick event record
A joystick event has four fields: a millisecond timestamp, a value, an event type, and an axis or button number. These fields let an application distinguish which control changed and obtain its state or position while reading raw device events. section 7.5.1
struct js_event {
__u32 time; /* event timestamp in milliseconds */
__s16 value; /* value */
__u8 type; /* event type */
__u8 number; /* axis/button number */
};
Inspect the js_event layout used when parsing raw joystick events with Python struct. section 7.5.1
For a remotely controlled robot, joystick data can become movement requests sent over HTTP to a robot server. Pygame offers an initial route for working with joystick devices, while direct struct parsing gives access to Linux input events; Bluetooth drivers can provide a wireless controller connection. section 7.2 section 7.8
