PcoWSkbVqDnWTu_dm2ix
We use cookies on this site to enhance your user experience
Collapse Sidebar

GetSupportedGamepadKeyCodes

This function returns an array of Enum/KeyCode|KeyCodes that the gamepad associated with the given Enum/UserInputType supports.

This function can be used to determine which KeyCodes are supported and not supported by a connected gamepad. To determine if a specific KeyCode is supported, use UserInputService/GamepadSupports.

If called on a non existent, or non connected, gamepad, this function will return an empty array.

As UserInputService is client-side only, this function can only be used in a LocalScript.

Check out this article to learn more about adding support for gamepad input into your game and this page for articles on cross-platform development.

See also

  • UserInputService/GamepadConnected
  • UserInputService/GamepadDisconnected
  • UserInputService/GetConnectedGamepads
  • UserInputService/GetNavigationGamepads
  • UserInputService/SetNavigationGamepad
  • UserInputService/IsNavigationGamepad
  • UserInputService/IsGamepadButtonDown
  • UserInputService/GetGamepadState
  • UserInputService/GetGamepadConnected
  • UserInputService/GamepadSupports
  • UserInputService/GamepadEnabled

Parameters

Name Type Default Description

The Enum/UserInputType of the gamepad

Returns

Return Type Summary

An array of Enum/KeyCode|KeyCodes supported by the given gamepad


Code Samples


Binding Supported Gamepad KeyCodes

This example gets a list of navigation gamepads and a list of their supported Enum/KeyCodes. Then, it iterates through the supported KeyCode list and binds the ButtonX and X keys to functions if they are supported by a gamepad using the ContextActionService/BindAction|ContextActionService.

Check out this article to learn more about adding support for gamepad input into your game.

local UserInputService = game:GetService("UserInputService")
local ContextActionService = game:GetService(“ContextActionService”)

local function actionHandler(actionName, inputState, inputObj)
	if inputState == Enum.UserInputState.Begin then 
		print("Action Handler: " .. actionName)
	end

	-- Since this function does not return anything, this handler will
	-- "sink" the input and no other action handlers will be called after
	-- this one.
end

local navGamepads = UserInputService:GetNavigationGamepads()
for _, gamepad in pairs(navGamepads) do
    local supportedKeyCodes = UserInputService:GetSupportedGamepadKeyCodes()

    for _, keycode in pairs(supportedKeyCodes) do
        if (keycode == Enum.KeyCode.ButtonX) then
            ContextActionService:BindAction(“SampleAction”, actionHandler, false, Enum.KeyCode.ButtonX)
        end
        if (keycode == Enum.KeyCode.X) then
            ContextActionService:BindAction(“SampleAction”, actionHandler, false, Enum.KeyCode.X)
        end
    end
end