- Gamepads, Axis, and Button IDs
- Global variables
- Controller Layouts
- gamepad.pressed()
- gamepad.released()
- gamepad.isDown()
- gamepad.getPOV()
- gamepad.getAxis()
- gamepad.getCount()
Both gamepads and buttons start with an index of 1. That is, the first detected gamepad had an ID of 1, and the first button on that gamepad has a button ID of 1.
Up to 16 gamepads are supported. Up to 32 buttons and 6 axis per gamepad are supported. Any gamepads you wish to read input from should be plugged in before starting a script in which you intend to use it for.
To make working with the POV hat (aka D-pad) a bit easier, a few variables have been added to global scope:
Global variable | Description |
---|---|
The "Down" button; A value of 180 | |
The "Up" button; A value of 0 | |
No direction; A value of 655 | |
The "Left" button; A value of 270 | |
The "Right" button; A value of 90 |
Every gamepa model will have it's own unique button & axis map. In modern times, Xbox and PlayStation controllers have become a bit of a standard for PC gaming, however there are thousands of USB gamepads that do not conform to any standard.
Unfortunately, this means that you will likely need to discover how buttons & axis are mapped for your gamepad if they are not a common and well-recognized controller. You may do this by conecting the gamepad and running the included 'keytest.lua' script. Once the script has been run, it will continue to dump information on any keys, mouse buttons, or gamepad inputs that it can detect.
For your convenience, details on a few gamepads will be detailed below.
XBox 360
XBox 360 controllers have a few quirks to them. First and foremost, it needs to be noted that the
"Guide" button (that big round button in the center that usually as an XBox logo on it) does not
actually trigger a gamepad button press, but rather a normal virtual key as if from a keyboard.
Pressing the Guide button sends a vk up/down event for VK
The controller only has one POV-hat and responds with a
The two analog sticks each have 2 axis while the left and right triggers share an axis. The left analog stick uses Axis 1 in the horizontal direction (0.0 = left, 100.0 = right) and Axis 2 in the vertical direction (0.0 = up, 100.0 = down) while the right analog stick uses Axis 5 for the horizontal direction and Axis 4 in the vertical direction. LT (left trigger) and RT (right trigger) share an axis (Axis 3) such that the right trigger only provides data in the 50.0 (unpressed state) to 0.0 (fully pressed state) range and the left trigger only provides data in the 50.0 (unpressed state) to 100.0 (fully pressed state) range.
Name | ID | Value(s) |
---|---|---|
POV-hat | 0-360 in 45° increments, or 655 (centered) | |
A | 1 | 0/1 |
B | 2 | 0/1 |
X | 3 | 0/1 |
Y | 4 | 0/1 |
LB | 5 | 0/1 |
RB | 6 | 0/1 |
Back/menu | 7 | 0/1 |
Start | 8 | 0/1 |
Left Analog (pressed) | 9 | 0/1 |
Right Analog (pressed) | 10 | 0/1 |
Left Analog - Horizontal | Axis 1 | 0.0 - 100.0, left to right |
Left Analog - Vertical | Axis 2 | 0.0 - 100.0, top to bottom |
LT | Axis 3 | 50.0 - 100.0, unpressed to pressed |
RT | Axis 3 | 50.0 - 0.0, unpressed to pressed |
Right Analog - Horizontal | Axis 5 | 0.0 - 100.0, left to right |
Right Analog - Vertical | Axis 4 | 0.0 - 100.0, top to bottom |
Guide | 0x07 | 0/1 |
Generic USB PS2-like
You may test this using the keytest.lua script provided with MicroMacro.
While a PS2-style gamepad may be more straight-forward than XBox 360 controllers, there is one slightly unusual thing: the analog toggle button. When this toggle is off, both analog sticks instead behave more like digital inputs and will instead result in values 0.0, ~50.0 (centered) or 100.0 rather than a linear progression from 0.0 to 100.0. They will act more like a POV-hat than analog input, however, it is important to note that they still output analog, dual-axis values. The analog button itself will not expose any output or be readable, therefor you will not receive any events for this button.
The controller only has one POV-hat and responds with a
Name | ID | Value(s) |
---|---|---|
POV-hat | 0-360 in 45° increments, or 655 (centered) | |
Triangle | 1 | 0/1 |
Circle | 2 | 0/1 |
X | 3 | 0/1 |
Square | 4 | 0/1 |
L1 | 5 | 0/1 |
R1 | 6 | 0/1 |
L2 | 7 | 0/1 |
R2 | 8 | 0/1 |
Select | 9 | 0/1 |
Start | 10 | 0/1 |
Left Analog (pressed) | 11 | 0/1 |
Right Analog (pressed) | 12 | 0/1 |
Left Analog - Horizontal | Axis 1 | 0.0 - 100.0, left to right |
Left Analog - Vertical | Axis 2 | 0.0 - 100.0, top to bottom |
Right Analog - Horizontal | Axis 3 | 0.0 - 100.0, left to right |
Right Analog - Vertical | Axis 4 | 0.0 - 100.0, top to bottom |
Nintendo Switch JoyCons
Known Quirks
- When paired via Bluetooth to a PC, the joycon lights still flash rather than represent controller order.
- Joysticks act more like 8-directional POV-hats rather than dual-axis controls.
- Left and Right joycons will not combine to a singular device but instead behave as two separate controllers.
- No accelerometer/gyro data is provided.
Each JoyCon has a joystick, however, please note that it acts like an 8-directional POV-hat. The orientation for each JoyCon depends on whether it is a Left (L) or Right (R) JoyCon. Both follow a 0° to 315° range in the clockwise direction and both begin (0°) pointing towards the "inside" (where the Switch would be if they were directly connected) and are therefore opposite to each other. See the diagram for clarity.
Button Layout
Left JoyCon | Right JoyCon | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
If the given gamepad has had its button pressed since last polling, returns
You would only use this function to determine the initial impulse of a button being pressed. Use gamepad.isDown() to check if the button is currently in the "down" state (either the initial press or being held down).
While you may use this function in your main loop, it is probably better to rely on capturing the gamepadpressed event.
This function works exactly like gamepad.pressed() except that it checks if the button has just been released.
While you may use this function in your main loop, it is probably better to rely on capturing the gamepadreleased event.
If the given gamepad currently has the given button held down (as of last polling), this returns
A button is considered "down" if this is the initial pressing of the button or if the button is still being held down.
Returns the angle of the POV hat (D-pad). If centered, returns
Returns the axis value for a given gamepad. The value will be between 0.0 (minimum) and 100.0(maximum). If centered, the value will be ~50.0.
This will not always be exact! Due to calibration, floating-point inaccuracies, and physical defects, you may receive a value that is a fraction off the intended value. I.e. ~49.0 - 51.0 while centered.
Returns the number of connected & detected gamepads. If your gamepad was not plugged-in when starting the script, it may not be detected and you should restart the script.
Page last updated at 2018-09-25 20:47:35