How to | Connect a Gamepad or Other Device to Mathematica
Beyond using a keyboard or mouse, you can control Mathematica with a joystick, gamepad, 3D mouse, or any device that follows the industry-standard human interface device specification.
To connect a new device to Mathematica, make sure that it is plugged in and turned on if necessary. If the device came with special drivers, make sure they are installed.
Any device that follows the industry-standard human interface device (HID) specification will be immediately available to Mathematica.
All of the devices detected by Mathematica are listed in the output of ControllerInformation. There are several built-in devices here in addition to a Logitech Dual Action gamepad that is connected via USB:
Once your device is detected, Mathematica is ready to use it.
By default, gamepads and other controllers can be used with Mathematica functions like Manipulate and Graphics3D. For instance, you can use a connected gamepad to adjust Manipulate controls or rotate 3D graphics with no additional configuration. For more information, see How to: Use Built-in Gamepad Support.
At a lower level, Mathematica allows any of the information visible in the output of ControllerInformation to be used with ControllerState.
Here, the state of every control on the gamepad is returned as a list of rules:
The names of the controls in this list, like , are taken directly from low-level information returned by the device.
Devices from different manufacturers often use different names for controls. Mathematica attempts to standardize these names to , , , , and so on for one-dimensional analog controls, and , , and so on for buttons.
To see how Mathematica maps its standardized names onto the hardware names, use with the controller name in ControllerState. Short is used here to display only a few lines of this mapping:
Here, the standardized names , , and are used in a list to get the current state of the first three one-dimensional analog axes:
If you wrap this input with Dynamic, the output will update continuously, thus letting you view the state of each axis in real time.
For information on using these standardized names in functions like Manipulate, see How to: Use Built-in Gamepad Support.
If ControllerState does not immediately respond to your controller, you may need to adjust the ControllerPath option. Similar to the way Mathematica searches the directories in $Path when it looks for files, ControllerState searches for available controllers based on the ControllerPath setting.
The default setting for ControllerPath is to use gamepads, if any are present:
Because of this, you can omit as the first argument to ControllerState and get the same result:
If there are no gamepads connected, then ControllerState by default will fall back to other joystick devices, 3D mice, and finally other controller devices, including infrared controllers and digitizers.
By changing the order of devices that appear in ControllerPath, you can change the order they are searched for by ControllerState.
Here, a preference for the sudden motion sensor that is built into some portable computers is specified in ControllerPath: