Codectionary / Developer documentation / Luau

ContextActionService

Bind a named action to one or more input methods.

Why use ContextActionService?

ContextActionService maps inputs to an action callback. It can also create touch controls when requested, making it useful for controls intended to work beyond a single keyboard key.

Try it in Roblox Studio

Put this LocalScript in StarterPlayerScripts. Test E and a gamepad X button in Studio; on a touch device the service may display a touch button.

Syntax

ContextActionService:BindAction(name, callback, true, Enum.KeyCode.E)

Examples

ContextActionService

Bind an interaction action and ignore End events.

local ContextActionService = game:GetService("ContextActionService")
local function interact(actionName, inputState)
    if inputState == Enum.UserInputState.Begin then
        print(actionName, "started")
    end
    return Enum.ContextActionResult.Pass
end

ContextActionService:BindAction(
    "PracticeInteract", interact, true,
    Enum.KeyCode.E, Enum.KeyCode.ButtonX
)
  1. BindAction associates a unique name with a callback and input keys.
  2. The third argument requests a touch button. The callback checks Begin so one press is not handled again on End.
  3. Returning Pass allows other bound handlers to see the input. UnbindAction when this control is no longer needed.

Best practices

  • Give every action a distinctive name and unbind it when its UI or mode ends.
  • Test mouse, touch and gamepad behavior instead of assuming one control scheme.

Runtime & versions

Luau host; Roblox engine examples require Roblox Studio. Feature availability depends on the language and runtime version used by your project; consult the official reference below.

Luau documentation

Specifications & further reading