Apprendre
Classe de moteur
ContextActionService
Création impossible
Service

*Ce contenu est traduit en utilisant l'IA (Beta) et peut contenir des erreurs. Pour consulter cette page en anglais, clique ici.


Résumé
Méthodes
BindAction(actionName: string,functionToBind: function,createTouchButton: boolean,inputTypes: Tuple):()
BindActionAtPriority(actionName: string,functionToBind: function,createTouchButton: boolean,priorityLevel: number,inputTypes: Tuple):()
BindActionToInputTypes(actionName: string,functionToBind: function,createTouchButton: boolean,inputTypes: Tuple):()
Déprécié
BindActivate(userInputTypeForActivation: Enum.UserInputType,keyCodesForActivation: Tuple):()
SetDescription(actionName: string,description: string):()
SetImage(actionName: string,image: string):()
SetPosition(actionName: string,position: UDim2):()
SetTitle(actionName: string,title: string):()
UnbindAction(actionName: string):()
UnbindActivate(userInputTypeForActivation: Enum.UserInputType,keyCodeForActivation: Enum.KeyCode):()
Membres hérités
Échantillons de code
Outil de rechargement du ContextActionService
local ContextActionService = game:GetService("ContextActionService")
local ACTION_RELOAD = "Recharger"
local tool = script.Parent
local function handleAction(actionName, inputState, _inputObject)
if actionName == ACTION_RELOAD and inputState == Enum.UserInputState.Begin then
print("Rechargement en cours !")
end
end
tool.Equipped:Connect(function()
ContextActionService:BindAction(ACTION_RELOAD, handleAction, true, Enum.KeyCode.R)
end)
tool.Unequipped:Connect(function()
ContextActionService:UnbindAction(ACTION_RELOAD)
end)

Référence API
Méthodes
BindAction
Capacités : Input
ContextActionService:BindAction(
actionName:string, functionToBind:function, createTouchButton:boolean, inputTypes:Tuple
):()
Paramètres
actionName:string
functionToBind:function
createTouchButton:boolean
inputTypes:Tuple
Retours
()
Échantillons de code
Outil de rechargement du ContextActionService
local ContextActionService = game:GetService("ContextActionService")
local ACTION_RELOAD = "Recharger"
local tool = script.Parent
local function handleAction(actionName, inputState, _inputObject)
if actionName == ACTION_RELOAD and inputState == Enum.UserInputState.Begin then
print("Rechargement en cours !")
end
end
tool.Equipped:Connect(function()
ContextActionService:BindAction(ACTION_RELOAD, handleAction, true, Enum.KeyCode.R)
end)
tool.Unequipped:Connect(function()
ContextActionService:UnbindAction(ACTION_RELOAD)
end)
Gestionnaire d'action général
local ContextActionService = game:GetService("ContextActionService")
local function handleAction(actionName, inputState, inputObj)
if inputState == Enum.UserInputState.Begin then
print("Gestion de l'action : " .. actionName)
print(inputObj.UserInputType)
end
-- Puisque cette fonction ne retourne rien, ce gestionnaire va
-- "absorber" l'entrée et aucun autre gestionnaire d'action ne sera
-- appelé après celui-ci.
end
ContextActionService:BindAction("BoundAction", handleAction, false, Enum.KeyCode.F)
Gestionnaires d'actions empilés
local ContextActionService = game:GetService("ContextActionService")
-- Définir un gestionnaire d'action pour FirstAction
local function actionHandlerOne(actionName, inputState, _inputObj)
if inputState == Enum.UserInputState.Begin then
print("Gestionnaire d'Action Un: " .. actionName)
end
-- Ce gestionnaire d'action retourne nil, donc on suppose qu'il
-- gère correctement l'action.
end
-- Liaison de l'action FirstAction (elle est en bas de la pile)
ContextActionService:BindAction("FirstAction", actionHandlerOne, false, Enum.KeyCode.Z, Enum.KeyCode.X, Enum.KeyCode.C)
-- Définir un gestionnaire d'action pour SecondAction
local function actionHandlerTwo(actionName, inputState, inputObj)
if inputState == Enum.UserInputState.Begin then
print("Gestionnaire d'Action Deux: " .. actionName)
end
if inputObj.KeyCode == Enum.KeyCode.X then
return Enum.ContextActionResult.Pass
else
-- Retourner nil coule implicitement des inputs
return Enum.ContextActionResult.Sink
end
end
-- Liaison de SecondAction sur la première action (puisqu'il est lié plus récemment, il est en haut de la pile)
-- Notez que SecondAction utilise les mêmes touches que
ContextActionService:BindAction("SecondAction", actionHandlerTwo, false, Enum.KeyCode.Z, Enum.KeyCode.X)

BindActionAtPriority
Capacités : Input
ContextActionService:BindActionAtPriority(
actionName:string, functionToBind:function, createTouchButton:boolean, priorityLevel:number, inputTypes:Tuple
):()
Paramètres
actionName:string
functionToBind:function
createTouchButton:boolean
priorityLevel:number
inputTypes:Tuple
Retours
()
Échantillons de code
Priorités de BindAction de ContextActionService
local ContextActionService = game:GetService("ContextActionService")
local INPUT_KEY1 = Enum.KeyCode.Q
local INPUT_KEY2 = Enum.KeyCode.E
local function handleThrow(actionName: string, inputState: Enum.UserInputState, inputObject: InputObject)
if inputState ~= Enum.UserInputState.Begin then
return Enum.ContextActionResult.Pass
end
print(`Action [{actionName}] occurred. KeyCode [{inputObject.KeyCode}] pressed.`)
return Enum.ContextActionResult.Sink
end
local function handlePunch(actionName: string, inputState: Enum.UserInputState, inputObject: InputObject)
if inputState ~= Enum.UserInputState.Begin then
return Enum.ContextActionResult.Pass
end
print(`Action [{actionName}] occurred. KeyCode [{inputObject.KeyCode}] pressed.`)
return Enum.ContextActionResult.Sink
end
-- Sans spécifier de priorité, l'action liée le plus récemment est appelée en premier,
-- donc appuyer sur INPUT_KEY1 imprime "Punch" puis absorbe l'entrée.
ContextActionService:BindAction("DefaultThrow", handleThrow, false, INPUT_KEY1)
ContextActionService:BindAction("DefaultPunch", handlePunch, false, INPUT_KEY1)
-- Ici, nous lions les deux fonctions dans le même ordre que ci-dessus, mais avec des priorités explicitement échangées.
-- C'est-à-dire que nous donnons à "Throw" une priorité plus élevée de 2 afin qu'elle soit appelée en premier,
-- malgré que "Punch" soit toujours lié plus récemment.
-- Appuyer sur INPUT_KEY2 imprime "Throw" puis absorbe l'entrée.
ContextActionService:BindActionAtPriority("PriorityThrow", handleThrow, false, 2, INPUT_KEY2)
ContextActionService:BindActionAtPriority("PriorityPunch", handlePunch, false, 1, INPUT_KEY2)

BindActionToInputTypes
Déprécié

BindActivate
Capacités : Input
ContextActionService:BindActivate(
userInputTypeForActivation:Enum.UserInputType, keyCodesForActivation:Tuple
):()
Paramètres
userInputTypeForActivation:Enum.UserInputType
keyCodesForActivation:Tuple
Retours
()

GetAllBoundActionInfo
Capacités : Input
ContextActionService:GetAllBoundActionInfo():Dictionary
Retours

GetBoundActionInfo
Capacités : Input
ContextActionService:GetBoundActionInfo(actionName:string):Dictionary
Paramètres
actionName:string
Retours

GetButton
Rendement
Capacités : Input
ContextActionService:GetButton(actionName:string):Instance
Paramètres
actionName:string
Retours

GetCurrentLocalToolIcon
Capacités : Input
ContextActionService:GetCurrentLocalToolIcon():string
Retours

SetDescription
Capacités : Input
ContextActionService:SetDescription(
actionName:string, description:string
):()
Paramètres
actionName:string
description:string
Retours
()
Échantillons de code
Bouton tactile du ContextActionService
local ContextActionService = game:GetService("ContextActionService")
local ACTION_INSPECT = "Inspecter"
local INPUT_INSPECT = Enum.KeyCode.E
local IMAGE_INSPECT = "rbxassetid://1826746856" -- Image d'une bulle de discours avec un ? dedans
local function handleAction(actionName, inputState, _inputObject)
if actionName == ACTION_INSPECT and inputState == Enum.UserInputState.End then
print("Inspection")
end
end
-- Pour les dispositifs tactiles, un bouton est créé à l'écran automatiquement via le 3ème paramètre
ContextActionService:BindAction(ACTION_INSPECT, handleAction, true, INPUT_INSPECT)
-- Nous pouvons utiliser ces fonctions pour personnaliser le bouton :
ContextActionService:SetImage(ACTION_INSPECT, IMAGE_INSPECT)
ContextActionService:SetTitle(ACTION_INSPECT, "Regarder")
ContextActionService:SetDescription(ACTION_INSPECT, "Inspecter quelque chose.")
ContextActionService:SetPosition(ACTION_INSPECT, UDim2.new(0, 0, 0, 0))
-- Nous pouvons manipuler le bouton directement en utilisant ContextActionService:GetButton
local imgButton = ContextActionService:GetButton(ACTION_INSPECT)
if imgButton then -- Rappelez-vous : les dispositifs non tactiles ne retourneront rien !
imgButton.ImageColor3 = Color3.new(0.5, 1, 0.5) -- Teindre l'ImageButton en vert
end

SetImage
Capacités : Input
ContextActionService:SetImage(
actionName:string, image:string
):()
Paramètres
actionName:string
image:string
Retours
()
Échantillons de code
Bouton tactile du ContextActionService
local ContextActionService = game:GetService("ContextActionService")
local ACTION_INSPECT = "Inspecter"
local INPUT_INSPECT = Enum.KeyCode.E
local IMAGE_INSPECT = "rbxassetid://1826746856" -- Image d'une bulle de discours avec un ? dedans
local function handleAction(actionName, inputState, _inputObject)
if actionName == ACTION_INSPECT and inputState == Enum.UserInputState.End then
print("Inspection")
end
end
-- Pour les dispositifs tactiles, un bouton est créé à l'écran automatiquement via le 3ème paramètre
ContextActionService:BindAction(ACTION_INSPECT, handleAction, true, INPUT_INSPECT)
-- Nous pouvons utiliser ces fonctions pour personnaliser le bouton :
ContextActionService:SetImage(ACTION_INSPECT, IMAGE_INSPECT)
ContextActionService:SetTitle(ACTION_INSPECT, "Regarder")
ContextActionService:SetDescription(ACTION_INSPECT, "Inspecter quelque chose.")
ContextActionService:SetPosition(ACTION_INSPECT, UDim2.new(0, 0, 0, 0))
-- Nous pouvons manipuler le bouton directement en utilisant ContextActionService:GetButton
local imgButton = ContextActionService:GetButton(ACTION_INSPECT)
if imgButton then -- Rappelez-vous : les dispositifs non tactiles ne retourneront rien !
imgButton.ImageColor3 = Color3.new(0.5, 1, 0.5) -- Teindre l'ImageButton en vert
end

SetPosition
Capacités : Input
ContextActionService:SetPosition(
actionName:string, position:UDim2
):()
Paramètres
actionName:string
position:UDim2
Retours
()
Échantillons de code
Bouton tactile du ContextActionService
local ContextActionService = game:GetService("ContextActionService")
local ACTION_INSPECT = "Inspecter"
local INPUT_INSPECT = Enum.KeyCode.E
local IMAGE_INSPECT = "rbxassetid://1826746856" -- Image d'une bulle de discours avec un ? dedans
local function handleAction(actionName, inputState, _inputObject)
if actionName == ACTION_INSPECT and inputState == Enum.UserInputState.End then
print("Inspection")
end
end
-- Pour les dispositifs tactiles, un bouton est créé à l'écran automatiquement via le 3ème paramètre
ContextActionService:BindAction(ACTION_INSPECT, handleAction, true, INPUT_INSPECT)
-- Nous pouvons utiliser ces fonctions pour personnaliser le bouton :
ContextActionService:SetImage(ACTION_INSPECT, IMAGE_INSPECT)
ContextActionService:SetTitle(ACTION_INSPECT, "Regarder")
ContextActionService:SetDescription(ACTION_INSPECT, "Inspecter quelque chose.")
ContextActionService:SetPosition(ACTION_INSPECT, UDim2.new(0, 0, 0, 0))
-- Nous pouvons manipuler le bouton directement en utilisant ContextActionService:GetButton
local imgButton = ContextActionService:GetButton(ACTION_INSPECT)
if imgButton then -- Rappelez-vous : les dispositifs non tactiles ne retourneront rien !
imgButton.ImageColor3 = Color3.new(0.5, 1, 0.5) -- Teindre l'ImageButton en vert
end

SetTitle
Capacités : Input
ContextActionService:SetTitle(
actionName:string, title:string
):()
Paramètres
actionName:string
title:string
Retours
()
Échantillons de code
Bouton tactile du ContextActionService
local ContextActionService = game:GetService("ContextActionService")
local ACTION_INSPECT = "Inspecter"
local INPUT_INSPECT = Enum.KeyCode.E
local IMAGE_INSPECT = "rbxassetid://1826746856" -- Image d'une bulle de discours avec un ? dedans
local function handleAction(actionName, inputState, _inputObject)
if actionName == ACTION_INSPECT and inputState == Enum.UserInputState.End then
print("Inspection")
end
end
-- Pour les dispositifs tactiles, un bouton est créé à l'écran automatiquement via le 3ème paramètre
ContextActionService:BindAction(ACTION_INSPECT, handleAction, true, INPUT_INSPECT)
-- Nous pouvons utiliser ces fonctions pour personnaliser le bouton :
ContextActionService:SetImage(ACTION_INSPECT, IMAGE_INSPECT)
ContextActionService:SetTitle(ACTION_INSPECT, "Regarder")
ContextActionService:SetDescription(ACTION_INSPECT, "Inspecter quelque chose.")
ContextActionService:SetPosition(ACTION_INSPECT, UDim2.new(0, 0, 0, 0))
-- Nous pouvons manipuler le bouton directement en utilisant ContextActionService:GetButton
local imgButton = ContextActionService:GetButton(ACTION_INSPECT)
if imgButton then -- Rappelez-vous : les dispositifs non tactiles ne retourneront rien !
imgButton.ImageColor3 = Color3.new(0.5, 1, 0.5) -- Teindre l'ImageButton en vert
end

UnbindAction
Capacités : Input
ContextActionService:UnbindAction(actionName:string):()
Paramètres
actionName:string
Retours
()
Échantillons de code
Outil de rechargement du ContextActionService
local ContextActionService = game:GetService("ContextActionService")
local ACTION_RELOAD = "Recharger"
local tool = script.Parent
local function handleAction(actionName, inputState, _inputObject)
if actionName == ACTION_RELOAD and inputState == Enum.UserInputState.Begin then
print("Rechargement en cours !")
end
end
tool.Equipped:Connect(function()
ContextActionService:BindAction(ACTION_RELOAD, handleAction, true, Enum.KeyCode.R)
end)
tool.Unequipped:Connect(function()
ContextActionService:UnbindAction(ACTION_RELOAD)
end)

UnbindActivate
Capacités : Input
ContextActionService:UnbindActivate(
userInputTypeForActivation:Enum.UserInputType, keyCodeForActivation:Enum.KeyCode
):()
Paramètres
userInputTypeForActivation:Enum.UserInputType
keyCodeForActivation:Enum.KeyCode
Valeur par défaut : "Unknown"
Retours
()

UnbindAllActions
Capacités : Input
ContextActionService:UnbindAllActions():()
Retours
()

Événements
LocalToolEquipped
Capacités : Input
ContextActionService.LocalToolEquipped(toolEquipped:Instance):RBXScriptSignal
Paramètres
toolEquipped:Instance

LocalToolUnequipped
Capacités : Input
ContextActionService.LocalToolUnequipped(toolUnequipped:Instance):RBXScriptSignal
Paramètres
toolUnequipped:Instance

©2026 Société Roblox. Roblox, le logo Roblox et Powering Imagination font partie de nos marques déposées aux États-Unis et dans d'autres pays.