Meer informatie
Engine-klasse
ContextActionService
Niet maakbaar
Service

*Deze inhoud is vertaald met behulp van AI (bèta) en kan fouten bevatten. Klik hier om deze pagina in het Engels te bekijken.


Samenvatting

Methoden
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):()
Verouderd
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):()
Overgenomen leden
Codevoorbeelden
ContextActionService Tool Herladen
local ContextActionService = game:GetService("ContextActionService")
local ACTION_RELOAD = "Herladen"
local tool = script.Parent
local function handleAction(actionName, inputState, _inputObject)
if actionName == ACTION_RELOAD and inputState == Enum.UserInputState.Begin then
print("Herladen!")
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)

API-referentie
Methoden
BindAction
Mogelijkheden: Input
ContextActionService:BindAction(
actionName:string, functionToBind:function, createTouchButton:boolean, inputTypes:Tuple
):()
Parameters
actionName:string
functionToBind:function
createTouchButton:boolean
inputTypes:Tuple
Teruggaves
()
Codevoorbeelden
ContextActionService Tool Herladen
local ContextActionService = game:GetService("ContextActionService")
local ACTION_RELOAD = "Herladen"
local tool = script.Parent
local function handleAction(actionName, inputState, _inputObject)
if actionName == ACTION_RELOAD and inputState == Enum.UserInputState.Begin then
print("Herladen!")
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)
Algemene Actiehandler
local ContextActionService = game:GetService("ContextActionService")
local function handleAction(actionName, inputState, inputObj)
if inputState == Enum.UserInputState.Begin then
print("Actie verwerken: " .. actionName)
print(inputObj.UserInputType)
end
-- Aangezien deze functie niets retourneert, zal deze handler
-- de invoer "sink" en zullen geen andere actiehandlers na
-- deze worden aangeroepen.
end
ContextActionService:BindAction("BoundAction", handleAction, false, Enum.KeyCode.F)
Gestapelde Actiehandlers
local ContextActionService = game:GetService("ContextActionService")
-- Definieer een actiehandler voor FirstAction
local function actionHandlerOne(actionName, inputState, _inputObj)
if inputState == Enum.UserInputState.Begin then
print("Actiehandler Eén: " .. actionName)
end
-- Deze actiehandler retourneert nil, dus het wordt aangenomen dat
-- het de actie correct afhandelt.
end
-- Binding van de actie FirstAction (het staat onderaan de stapel)
ContextActionService:BindAction("FirstAction", actionHandlerOne, false, Enum.KeyCode.Z, Enum.KeyCode.X, Enum.KeyCode.C)
-- Definieer een actiehandler voor SecondAction
local function actionHandlerTwo(actionName, inputState, inputObj)
if inputState == Enum.UserInputState.Begin then
print("Actiehandler Twee: " .. actionName)
end
if inputObj.KeyCode == Enum.KeyCode.X then
return Enum.ContextActionResult.Pass
else
-- Nil retourneren onderdrukt invoeren impliciet
return Enum.ContextActionResult.Sink
end
end
-- Binding van SecondAction boven de eerste actie (aangezien het recentelijker is gebonden, staat het bovenaan de stapel)
-- Let op dat SecondAction dezelfde toetsen gebruikt als
ContextActionService:BindAction("SecondAction", actionHandlerTwo, false, Enum.KeyCode.Z, Enum.KeyCode.X)

BindActionAtPriority
Mogelijkheden: Input
ContextActionService:BindActionAtPriority(
actionName:string, functionToBind:function, createTouchButton:boolean, priorityLevel:number, inputTypes:Tuple
):()
Parameters
actionName:string
functionToBind:function
createTouchButton:boolean
priorityLevel:number
inputTypes:Tuple
Teruggaves
()
Codevoorbeelden
ContextActionService ActieBinden Prioriteiten
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(`Actie [{actionName}] uitgevoerd. KeyCode [{inputObject.KeyCode}] ingedrukt.`)
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(`Actie [{actionName}] uitgevoerd. KeyCode [{inputObject.KeyCode}] ingedrukt.`)
return Enum.ContextActionResult.Sink
end
-- Zonder prioriteit op te geven, wordt de meest recent gebonden actie als eerste aangeroepen,
-- dus het indrukken van INPUT_KEY1 print "Punch" en sinkt vervolgens de invoer.
ContextActionService:BindAction("DefaultThrow", handleThrow, false, INPUT_KEY1)
ContextActionService:BindAction("DefaultPunch", handlePunch, false, INPUT_KEY1)
-- Hier binden we beide functies in dezelfde volgorde als hierboven, maar met expliciet verwisselde prioriteiten.
-- Dat wil zeggen, we geven "Throw" een hogere prioriteit van 2 zodat deze als eerste wordt aangeroepen,
-- ondanks dat "Punch" nog steeds recentelijker is gebonden.
-- Het indrukken van INPUT_KEY2 print "Throw" en sinkt vervolgens de invoer.
ContextActionService:BindActionAtPriority("PriorityThrow", handleThrow, false, 2, INPUT_KEY2)
ContextActionService:BindActionAtPriority("PriorityPunch", handlePunch, false, 1, INPUT_KEY2)

BindActionToInputTypes
Verouderd

BindActivate
Mogelijkheden: Input
ContextActionService:BindActivate(
userInputTypeForActivation:Enum.UserInputType, keyCodesForActivation:Tuple
):()
Parameters
userInputTypeForActivation:Enum.UserInputType
keyCodesForActivation:Tuple
Teruggaves
()

GetAllBoundActionInfo
Mogelijkheden: Input
ContextActionService:GetAllBoundActionInfo():Dictionary
Teruggaves

GetBoundActionInfo
Mogelijkheden: Input
ContextActionService:GetBoundActionInfo(actionName:string):Dictionary
Parameters
actionName:string
Teruggaves

GetButton
Opbrengst
Mogelijkheden: Input
ContextActionService:GetButton(actionName:string):Instance
Parameters
actionName:string
Teruggaves

GetCurrentLocalToolIcon
Mogelijkheden: Input
ContextActionService:GetCurrentLocalToolIcon():string
Teruggaves

SetDescription
Mogelijkheden: Input
ContextActionService:SetDescription(
actionName:string, description:string
):()
Parameters
actionName:string
description:string
Teruggaves
()
Codevoorbeelden
ContextActionService Aanraakknop
local ContextActionService = game:GetService("ContextActionService")
local ACTION_INSPECT = "Inspect"
local INPUT_INSPECT = Enum.KeyCode.E
local IMAGE_INSPECT = "rbxassetid://1826746856" -- Afbeelding van een spraakballon met ? erin
local function handleAction(actionName, inputState, _inputObject)
if actionName == ACTION_INSPECT and inputState == Enum.UserInputState.End then
print("Inspecteren")
end
end
-- Voor aanraakapparaten wordt er automatisch een knop op het scherm gemaakt via de 3e parameter
ContextActionService:BindAction(ACTION_INSPECT, handleAction, true, INPUT_INSPECT)
-- We kunnen deze functies gebruiken om de knop aan te passen:
ContextActionService:SetImage(ACTION_INSPECT, IMAGE_INSPECT)
ContextActionService:SetTitle(ACTION_INSPECT, "Kijk")
ContextActionService:SetDescription(ACTION_INSPECT, "Inspecteer iets.")
ContextActionService:SetPosition(ACTION_INSPECT, UDim2.new(0, 0, 0, 0))
-- We kunnen de knop direct manipuleren met ContextActionService:GetButton
local imgButton = ContextActionService:GetButton(ACTION_INSPECT)
if imgButton then -- Vergeet niet: niet-aanraakapparaten geven niets terug!
imgButton.ImageColor3 = Color3.new(0.5, 1, 0.5) -- Tint de ImageButton groen
end

SetImage
Mogelijkheden: Input
ContextActionService:SetImage(
actionName:string, image:string
):()
Parameters
actionName:string
image:string
Teruggaves
()
Codevoorbeelden
ContextActionService Aanraakknop
local ContextActionService = game:GetService("ContextActionService")
local ACTION_INSPECT = "Inspect"
local INPUT_INSPECT = Enum.KeyCode.E
local IMAGE_INSPECT = "rbxassetid://1826746856" -- Afbeelding van een spraakballon met ? erin
local function handleAction(actionName, inputState, _inputObject)
if actionName == ACTION_INSPECT and inputState == Enum.UserInputState.End then
print("Inspecteren")
end
end
-- Voor aanraakapparaten wordt er automatisch een knop op het scherm gemaakt via de 3e parameter
ContextActionService:BindAction(ACTION_INSPECT, handleAction, true, INPUT_INSPECT)
-- We kunnen deze functies gebruiken om de knop aan te passen:
ContextActionService:SetImage(ACTION_INSPECT, IMAGE_INSPECT)
ContextActionService:SetTitle(ACTION_INSPECT, "Kijk")
ContextActionService:SetDescription(ACTION_INSPECT, "Inspecteer iets.")
ContextActionService:SetPosition(ACTION_INSPECT, UDim2.new(0, 0, 0, 0))
-- We kunnen de knop direct manipuleren met ContextActionService:GetButton
local imgButton = ContextActionService:GetButton(ACTION_INSPECT)
if imgButton then -- Vergeet niet: niet-aanraakapparaten geven niets terug!
imgButton.ImageColor3 = Color3.new(0.5, 1, 0.5) -- Tint de ImageButton groen
end

SetPosition
Mogelijkheden: Input
ContextActionService:SetPosition(
actionName:string, position:UDim2
):()
Parameters
actionName:string
position:UDim2
Teruggaves
()
Codevoorbeelden
ContextActionService Aanraakknop
local ContextActionService = game:GetService("ContextActionService")
local ACTION_INSPECT = "Inspect"
local INPUT_INSPECT = Enum.KeyCode.E
local IMAGE_INSPECT = "rbxassetid://1826746856" -- Afbeelding van een spraakballon met ? erin
local function handleAction(actionName, inputState, _inputObject)
if actionName == ACTION_INSPECT and inputState == Enum.UserInputState.End then
print("Inspecteren")
end
end
-- Voor aanraakapparaten wordt er automatisch een knop op het scherm gemaakt via de 3e parameter
ContextActionService:BindAction(ACTION_INSPECT, handleAction, true, INPUT_INSPECT)
-- We kunnen deze functies gebruiken om de knop aan te passen:
ContextActionService:SetImage(ACTION_INSPECT, IMAGE_INSPECT)
ContextActionService:SetTitle(ACTION_INSPECT, "Kijk")
ContextActionService:SetDescription(ACTION_INSPECT, "Inspecteer iets.")
ContextActionService:SetPosition(ACTION_INSPECT, UDim2.new(0, 0, 0, 0))
-- We kunnen de knop direct manipuleren met ContextActionService:GetButton
local imgButton = ContextActionService:GetButton(ACTION_INSPECT)
if imgButton then -- Vergeet niet: niet-aanraakapparaten geven niets terug!
imgButton.ImageColor3 = Color3.new(0.5, 1, 0.5) -- Tint de ImageButton groen
end

SetTitle
Mogelijkheden: Input
ContextActionService:SetTitle(
actionName:string, title:string
):()
Parameters
actionName:string
title:string
Teruggaves
()
Codevoorbeelden
ContextActionService Aanraakknop
local ContextActionService = game:GetService("ContextActionService")
local ACTION_INSPECT = "Inspect"
local INPUT_INSPECT = Enum.KeyCode.E
local IMAGE_INSPECT = "rbxassetid://1826746856" -- Afbeelding van een spraakballon met ? erin
local function handleAction(actionName, inputState, _inputObject)
if actionName == ACTION_INSPECT and inputState == Enum.UserInputState.End then
print("Inspecteren")
end
end
-- Voor aanraakapparaten wordt er automatisch een knop op het scherm gemaakt via de 3e parameter
ContextActionService:BindAction(ACTION_INSPECT, handleAction, true, INPUT_INSPECT)
-- We kunnen deze functies gebruiken om de knop aan te passen:
ContextActionService:SetImage(ACTION_INSPECT, IMAGE_INSPECT)
ContextActionService:SetTitle(ACTION_INSPECT, "Kijk")
ContextActionService:SetDescription(ACTION_INSPECT, "Inspecteer iets.")
ContextActionService:SetPosition(ACTION_INSPECT, UDim2.new(0, 0, 0, 0))
-- We kunnen de knop direct manipuleren met ContextActionService:GetButton
local imgButton = ContextActionService:GetButton(ACTION_INSPECT)
if imgButton then -- Vergeet niet: niet-aanraakapparaten geven niets terug!
imgButton.ImageColor3 = Color3.new(0.5, 1, 0.5) -- Tint de ImageButton groen
end

UnbindAction
Mogelijkheden: Input
ContextActionService:UnbindAction(actionName:string):()
Parameters
actionName:string
Teruggaves
()
Codevoorbeelden
ContextActionService Tool Herladen
local ContextActionService = game:GetService("ContextActionService")
local ACTION_RELOAD = "Herladen"
local tool = script.Parent
local function handleAction(actionName, inputState, _inputObject)
if actionName == ACTION_RELOAD and inputState == Enum.UserInputState.Begin then
print("Herladen!")
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
Mogelijkheden: Input
ContextActionService:UnbindActivate(
userInputTypeForActivation:Enum.UserInputType, keyCodeForActivation:Enum.KeyCode
):()
Parameters
userInputTypeForActivation:Enum.UserInputType
keyCodeForActivation:Enum.KeyCode
Standaardwaarde: None
Teruggaves
()

UnbindAllActions
Mogelijkheden: Input
ContextActionService:UnbindAllActions():()
Teruggaves
()

Evenementen
LocalToolEquipped
Mogelijkheden: Input
ContextActionService.LocalToolEquipped(toolEquipped:Instance):RBXScriptSignal
Parameters
toolEquipped:Instance

LocalToolUnequipped
Mogelijkheden: Input
ContextActionService.LocalToolUnequipped(toolUnequipped:Instance):RBXScriptSignal
Parameters
toolUnequipped:Instance

©2026 Roblox Corporation. Roblox, het Roblox-logo en Powering Imagination behoren tot onze geregistreerde en niet-geregistreerde handelsmerken in de VS en andere landen.