Lernen
Engine-Klasse
ContextActionService
Nicht erstellbar
Dienst

*Dieser Inhalt wurde mit KI (Beta) übersetzt und kann Fehler enthalten. Um diese Seite auf Englisch zu sehen, klicke hier.


Zusammenfassung
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):()
Veraltet
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):()
Geerbte Mitglieder
Code-Beispiele
ContextActionService Werkzeug Nachladen
local ContextActionService = game:GetService("ContextActionService")
local ACTION_RELOAD = "Nachladen"
local tool = script.Parent
local function handleAction(actionName, inputState, _inputObject)
if actionName == ACTION_RELOAD and inputState == Enum.UserInputState.Begin then
print("Lädt nach!")
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-Referenz
Methoden
BindAction
Funktionen: Input
ContextActionService:BindAction(
actionName:string, functionToBind:function, createTouchButton:boolean, inputTypes:Tuple
):()
Parameter
actionName:string
functionToBind:function
createTouchButton:boolean
inputTypes:Tuple
Rückgaben
()
Code-Beispiele
ContextActionService Werkzeug Nachladen
local ContextActionService = game:GetService("ContextActionService")
local ACTION_RELOAD = "Nachladen"
local tool = script.Parent
local function handleAction(actionName, inputState, _inputObject)
if actionName == ACTION_RELOAD and inputState == Enum.UserInputState.Begin then
print("Lädt nach!")
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)
Allgemeiner Aktionshandler
local ContextActionService = game:GetService("ContextActionService")
local function handleAction(actionName, inputState, inputObj)
if inputState == Enum.UserInputState.Begin then
print("Aktion wird verarbeitet: " .. actionName)
print(inputObj.UserInputType)
end
-- Da diese Funktion nichts zurückgibt, wird dieser Handler
-- die Eingabe "versenken" und keine weiteren Aktionshandler
-- werden nach diesem aufgerufen.
end
ContextActionService:BindAction("BoundAction", handleAction, false, Enum.KeyCode.F)
Gestapelte Aktionshandler
local ContextActionService = game:GetService("ContextActionService")
-- Definiere einen Aktionshandler für FirstAction
local function actionHandlerOne(actionName, inputState, _inputObj)
if inputState == Enum.UserInputState.Begin then
print("Aktionshandler Eins: " .. actionName)
end
-- Dieser Aktionshandler gibt nil zurück, daher wird angenommen,
dass er die Aktion ordnungsgemäß verarbeitet.
end
-- Bindung der Aktion FirstAction (sie ist am Ende des Stapels)
ContextActionService:BindAction("FirstAction", actionHandlerOne, false, Enum.KeyCode.Z, Enum.KeyCode.X, Enum.KeyCode.C)
-- Definiere einen Aktionshandler für SecondAction
local function actionHandlerTwo(actionName, inputState, inputObj)
if inputState == Enum.UserInputState.Begin then
print("Aktionshandler Zwei: " .. actionName)
end
if inputObj.KeyCode == Enum.KeyCode.X then
return Enum.ContextActionResult.Pass
else
-- Rückgabe von nil implizit sinkt Eingaben
return Enum.ContextActionResult.Sink
end
end
-- Bindung von SecondAction über die erste Aktion (da sie neueren Datums gebunden wurde, ist sie oben im Stapel)
-- Beachte, dass SecondAction dieselben Tasten verwendet wie
ContextActionService:BindAction("SecondAction", actionHandlerTwo, false, Enum.KeyCode.Z, Enum.KeyCode.X)

BindActionAtPriority
Funktionen: Input
ContextActionService:BindActionAtPriority(
actionName:string, functionToBind:function, createTouchButton:boolean, priorityLevel:number, inputTypes:Tuple
):()
Parameter
actionName:string
functionToBind:function
createTouchButton:boolean
priorityLevel:number
inputTypes:Tuple
Rückgaben
()
Code-Beispiele
ContextActionService BindAction Prioritäten
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(`Aktion [{actionName}] trat auf. KeyCode [{inputObject.KeyCode}] gedrückt.`)
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(`Aktion [{actionName}] trat auf. KeyCode [{inputObject.KeyCode}] gedrückt.`)
return Enum.ContextActionResult.Sink
end
-- Ohne die Priorität anzugeben, wird die zuletzt gebundene Aktion zuerst aufgerufen,
-- also druckt das Drücken von INPUT_KEY1 "Punch" und unterdrückt dann die Eingabe.
ContextActionService:BindAction("DefaultThrow", handleThrow, false, INPUT_KEY1)
ContextActionService:BindAction("DefaultPunch", handlePunch, false, INPUT_KEY1)
-- Hier binden wir beide Funktionen in der gleichen Reihenfolge wie oben, aber mit explizit getauschten Prioritäten.
-- Das heißt, wir geben "Throw" eine höhere Priorität von 2, damit sie zuerst aufgerufen wird,
-- obwohl "Punch" noch kürzlich gebunden wurde.
-- Das Drücken von INPUT_KEY2 druckt "Throw" und unterdrückt dann die Eingabe.
ContextActionService:BindActionAtPriority("PriorityThrow", handleThrow, false, 2, INPUT_KEY2)
ContextActionService:BindActionAtPriority("PriorityPunch", handlePunch, false, 1, INPUT_KEY2)

BindActionToInputTypes
Veraltet

BindActivate
Funktionen: Input
ContextActionService:BindActivate(
userInputTypeForActivation:Enum.UserInputType, keyCodesForActivation:Tuple
):()
Parameter
userInputTypeForActivation:Enum.UserInputType
keyCodesForActivation:Tuple
Rückgaben
()

GetAllBoundActionInfo
Funktionen: Input
ContextActionService:GetAllBoundActionInfo():Dictionary
Rückgaben

GetBoundActionInfo
Funktionen: Input
ContextActionService:GetBoundActionInfo(actionName:string):Dictionary
Parameter
actionName:string
Rückgaben

GetButton
Angehalten
Funktionen: Input
ContextActionService:GetButton(actionName:string):Instance
Parameter
actionName:string
Rückgaben

GetCurrentLocalToolIcon
Funktionen: Input
ContextActionService:GetCurrentLocalToolIcon():string
Rückgaben

SetDescription
Funktionen: Input
ContextActionService:SetDescription(
actionName:string, description:string
):()
Parameter
actionName:string
description:string
Rückgaben
()
Code-Beispiele
ContextActionService Touch Button
local ContextActionService = game:GetService("ContextActionService")
local ACTION_INSPECT = "Inspect"
local INPUT_INSPECT = Enum.KeyCode.E
local IMAGE_INSPECT = "rbxassetid://1826746856" -- Bild einer Sprechblase mit ? darin
local function handleAction(actionName, inputState, _inputObject)
if actionName == ACTION_INSPECT and inputState == Enum.UserInputState.End then
print("Inspektieren")
end
end
-- Für Touch-Geräte wird automatisch ein Button auf dem Bildschirm erstellt über den 3. Parameter
ContextActionService:BindAction(ACTION_INSPECT, handleAction, true, INPUT_INSPECT)
-- Wir können diese Funktionen verwenden, um den Button anzupassen:
ContextActionService:SetImage(ACTION_INSPECT, IMAGE_INSPECT)
ContextActionService:SetTitle(ACTION_INSPECT, "Schau")
ContextActionService:SetDescription(ACTION_INSPECT, "Etwas inspizieren.")
ContextActionService:SetPosition(ACTION_INSPECT, UDim2.new(0, 0, 0, 0))
-- Wir können den Button direkt manipulieren, indem wir ContextActionService:GetButton verwenden
local imgButton = ContextActionService:GetButton(ACTION_INSPECT)
if imgButton then -- Denk daran: Nicht-Touch-Geräte geben nichts zurück!
imgButton.ImageColor3 = Color3.new(0.5, 1, 0.5) -- Färbe den ImageButton grün
end

SetImage
Funktionen: Input
ContextActionService:SetImage(
actionName:string, image:string
):()
Parameter
actionName:string
image:string
Rückgaben
()
Code-Beispiele
ContextActionService Touch Button
local ContextActionService = game:GetService("ContextActionService")
local ACTION_INSPECT = "Inspect"
local INPUT_INSPECT = Enum.KeyCode.E
local IMAGE_INSPECT = "rbxassetid://1826746856" -- Bild einer Sprechblase mit ? darin
local function handleAction(actionName, inputState, _inputObject)
if actionName == ACTION_INSPECT and inputState == Enum.UserInputState.End then
print("Inspektieren")
end
end
-- Für Touch-Geräte wird automatisch ein Button auf dem Bildschirm erstellt über den 3. Parameter
ContextActionService:BindAction(ACTION_INSPECT, handleAction, true, INPUT_INSPECT)
-- Wir können diese Funktionen verwenden, um den Button anzupassen:
ContextActionService:SetImage(ACTION_INSPECT, IMAGE_INSPECT)
ContextActionService:SetTitle(ACTION_INSPECT, "Schau")
ContextActionService:SetDescription(ACTION_INSPECT, "Etwas inspizieren.")
ContextActionService:SetPosition(ACTION_INSPECT, UDim2.new(0, 0, 0, 0))
-- Wir können den Button direkt manipulieren, indem wir ContextActionService:GetButton verwenden
local imgButton = ContextActionService:GetButton(ACTION_INSPECT)
if imgButton then -- Denk daran: Nicht-Touch-Geräte geben nichts zurück!
imgButton.ImageColor3 = Color3.new(0.5, 1, 0.5) -- Färbe den ImageButton grün
end

SetPosition
Funktionen: Input
ContextActionService:SetPosition(
actionName:string, position:UDim2
):()
Parameter
actionName:string
position:UDim2
Rückgaben
()
Code-Beispiele
ContextActionService Touch Button
local ContextActionService = game:GetService("ContextActionService")
local ACTION_INSPECT = "Inspect"
local INPUT_INSPECT = Enum.KeyCode.E
local IMAGE_INSPECT = "rbxassetid://1826746856" -- Bild einer Sprechblase mit ? darin
local function handleAction(actionName, inputState, _inputObject)
if actionName == ACTION_INSPECT and inputState == Enum.UserInputState.End then
print("Inspektieren")
end
end
-- Für Touch-Geräte wird automatisch ein Button auf dem Bildschirm erstellt über den 3. Parameter
ContextActionService:BindAction(ACTION_INSPECT, handleAction, true, INPUT_INSPECT)
-- Wir können diese Funktionen verwenden, um den Button anzupassen:
ContextActionService:SetImage(ACTION_INSPECT, IMAGE_INSPECT)
ContextActionService:SetTitle(ACTION_INSPECT, "Schau")
ContextActionService:SetDescription(ACTION_INSPECT, "Etwas inspizieren.")
ContextActionService:SetPosition(ACTION_INSPECT, UDim2.new(0, 0, 0, 0))
-- Wir können den Button direkt manipulieren, indem wir ContextActionService:GetButton verwenden
local imgButton = ContextActionService:GetButton(ACTION_INSPECT)
if imgButton then -- Denk daran: Nicht-Touch-Geräte geben nichts zurück!
imgButton.ImageColor3 = Color3.new(0.5, 1, 0.5) -- Färbe den ImageButton grün
end

SetTitle
Funktionen: Input
ContextActionService:SetTitle(
actionName:string, title:string
):()
Parameter
actionName:string
title:string
Rückgaben
()
Code-Beispiele
ContextActionService Touch Button
local ContextActionService = game:GetService("ContextActionService")
local ACTION_INSPECT = "Inspect"
local INPUT_INSPECT = Enum.KeyCode.E
local IMAGE_INSPECT = "rbxassetid://1826746856" -- Bild einer Sprechblase mit ? darin
local function handleAction(actionName, inputState, _inputObject)
if actionName == ACTION_INSPECT and inputState == Enum.UserInputState.End then
print("Inspektieren")
end
end
-- Für Touch-Geräte wird automatisch ein Button auf dem Bildschirm erstellt über den 3. Parameter
ContextActionService:BindAction(ACTION_INSPECT, handleAction, true, INPUT_INSPECT)
-- Wir können diese Funktionen verwenden, um den Button anzupassen:
ContextActionService:SetImage(ACTION_INSPECT, IMAGE_INSPECT)
ContextActionService:SetTitle(ACTION_INSPECT, "Schau")
ContextActionService:SetDescription(ACTION_INSPECT, "Etwas inspizieren.")
ContextActionService:SetPosition(ACTION_INSPECT, UDim2.new(0, 0, 0, 0))
-- Wir können den Button direkt manipulieren, indem wir ContextActionService:GetButton verwenden
local imgButton = ContextActionService:GetButton(ACTION_INSPECT)
if imgButton then -- Denk daran: Nicht-Touch-Geräte geben nichts zurück!
imgButton.ImageColor3 = Color3.new(0.5, 1, 0.5) -- Färbe den ImageButton grün
end

UnbindAction
Funktionen: Input
ContextActionService:UnbindAction(actionName:string):()
Parameter
actionName:string
Rückgaben
()
Code-Beispiele
ContextActionService Werkzeug Nachladen
local ContextActionService = game:GetService("ContextActionService")
local ACTION_RELOAD = "Nachladen"
local tool = script.Parent
local function handleAction(actionName, inputState, _inputObject)
if actionName == ACTION_RELOAD and inputState == Enum.UserInputState.Begin then
print("Lädt nach!")
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
Funktionen: Input
ContextActionService:UnbindActivate(
userInputTypeForActivation:Enum.UserInputType, keyCodeForActivation:Enum.KeyCode
):()
Parameter
userInputTypeForActivation:Enum.UserInputType
keyCodeForActivation:Enum.KeyCode
Standardwert: "Unknown"
Rückgaben
()

UnbindAllActions
Funktionen: Input
ContextActionService:UnbindAllActions():()
Rückgaben
()

Events
LocalToolEquipped
Funktionen: Input
ContextActionService.LocalToolEquipped(toolEquipped:Instance):RBXScriptSignal
Parameter
toolEquipped:Instance

LocalToolUnequipped
Funktionen: Input
ContextActionService.LocalToolUnequipped(toolUnequipped:Instance):RBXScriptSignal
Parameter
toolUnequipped:Instance

©2026 Roblox Corporation. Roblox, das Roblox-Logo und "Powering Imagination" gehören zu unseren eingetragenen und nicht eingetragenen Markenzeichen in den USA und anderen Ländern.