Skip to main content

! Create a hardware gamepad (gamepad.connect)

This function is available in versions after 20260805

Declaration

pad, err = gamepad.connect([properties])

Parameters

  • properties Optional table. Sets the following commonly used device properties; the values shown are used by default when omitted
{
VendorID = 0x5858,
ProductID = 0x0007,
VersionNumber = 1,
Product = "XXTouch Gamepad",
Manufacturer = "XXTouch",
Transport = "USB",
}

You may also set SerialNumber and LocationID. Other properties are not guaranteed to take effect.

Returns

  • pad Gamepad object or nil. A new gamepad object on success; nil if device creation fails
  • err String. Error message when device creation fails

Description

  • Each call returns a new independent gamepad object. Input state and lifecycle are independent between objects
  • The gamepad supports left and right analog sticks, a directional pad, numeric buttons 1 through 32, and a menu input
  • Waiting for the connection yields the current Lua execution thread
  • On a system that does not support this feature, gamepad.connect returns nil, error
  • A successful connection means that the gamepad object is ready to use. Whether the target app recognizes the gamepad and all of its inputs depends on the system version and the app's gamepad support
  • Transport only affects how the local system identifies the gamepad. Setting it to "Bluetooth" or "BluetoothLowEnergy" does not create a Bluetooth connection, advertise or pair the device, or make it appear in another device's Bluetooth list

Example

local pad, err = gamepad.connect({
Product = "XXTouch Gamepad",
})
assert(pad, err)

assert(pad:press("a"))
assert(pad:press("menu"))
assert(pad:down("left_shoulder"))
assert(pad:set_stick("left", 0.5, 1))
assert(pad:set_dpad(1, 0))
assert(pad:up("left_shoulder"))
assert(pad:reset())

pad:disconnect()