学习
引擎类
ContextActionService
无法创建
服务

*此内容使用人工智能(Beta)翻译,可能包含错误。若要查看英文页面,请点按 此处


概要
方法
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):()
已弃用
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):()
继承成员
代码示例
上下文操作服务工具重新加载
local ContextActionService = game:GetService("ContextActionService")
local ACTION_RELOAD = "重新加载"
local tool = script.Parent
local function handleAction(actionName, inputState, _inputObject)
if actionName == ACTION_RELOAD and inputState == Enum.UserInputState.Begin then
print("正在重新加载!")
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 参考
方法
BindAction
功能: Input
ContextActionService:BindAction(
actionName:string, functionToBind:function, createTouchButton:boolean, inputTypes:Tuple
):()
参数
actionName:string
functionToBind:function
createTouchButton:boolean
inputTypes:Tuple
返回
()
代码示例
上下文操作服务工具重新加载
local ContextActionService = game:GetService("ContextActionService")
local ACTION_RELOAD = "重新加载"
local tool = script.Parent
local function handleAction(actionName, inputState, _inputObject)
if actionName == ACTION_RELOAD and inputState == Enum.UserInputState.Begin then
print("正在重新加载!")
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)
一般操作处理器
local ContextActionService = game:GetService("ContextActionService")
local function handleAction(actionName, inputState, inputObj)
if inputState == Enum.UserInputState.Begin then
print("处理操作: " .. actionName)
print(inputObj.UserInputType)
end
-- 由于这个函数不返回任何内容,因此这个处理器将
-- "沉没" 输入,之后将不会调用其他操作处理器
-- 在这个处理器之后。
end
ContextActionService:BindAction("BoundAction", handleAction, false, Enum.KeyCode.F)
堆叠的操作处理程序
local ContextActionService = game:GetService("ContextActionService")
-- 为 FirstAction 定义操作处理程序
local function actionHandlerOne(actionName, inputState, _inputObj)
if inputState == Enum.UserInputState.Begin then
print("操作处理程序一: " .. actionName)
end
-- 此操作处理程序返回 nil,因此假定它
-- 正确处理了该操作。
end
-- 绑定操作 FirstAction(它在堆栈的底部)
ContextActionService:BindAction("FirstAction", actionHandlerOne, false, Enum.KeyCode.Z, Enum.KeyCode.X, Enum.KeyCode.C)
-- 为 SecondAction 定义操作处理程序
local function actionHandlerTwo(actionName, inputState, inputObj)
if inputState == Enum.UserInputState.Begin then
print("操作处理程序二: " .. actionName)
end
if inputObj.KeyCode == Enum.KeyCode.X then
return Enum.ContextActionResult.Pass
else
-- 返回 nil 隐性地沉没输入
return Enum.ContextActionResult.Sink
end
end
-- 在第一个操作之上绑定 SecondAction(由于它绑定得更接近,因此它在堆栈的顶部)
-- 请注意,SecondAction 使用的键与
ContextActionService:BindAction("SecondAction", actionHandlerTwo, false, Enum.KeyCode.Z, Enum.KeyCode.X)

BindActionAtPriority
功能: Input
ContextActionService:BindActionAtPriority(
actionName:string, functionToBind:function, createTouchButton:boolean, priorityLevel:number, inputTypes:Tuple
):()
参数
actionName:string
functionToBind:function
createTouchButton:boolean
priorityLevel:number
inputTypes:Tuple
返回
()
代码示例
上下文操作服务绑定操作优先级
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(`操作 [{actionName}] 发生了。 KeyCode [{inputObject.KeyCode}] 被按下。`)
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(`操作 [{actionName}] 发生了。 KeyCode [{inputObject.KeyCode}] 被按下。`)
return Enum.ContextActionResult.Sink
end
-- 如果没有指定优先级,最近绑定的操作会优先调用,
-- 因此按下 INPUT_KEY1 会打印 "Punch",然后消耗输入。
ContextActionService:BindAction("DefaultThrow", handleThrow, false, INPUT_KEY1)
ContextActionService:BindAction("DefaultPunch", handlePunch, false, INPUT_KEY1)
-- 在这里,我们以与上述相同的顺序绑定两个函数,但明确交换了优先级。
-- 即,我们给 "Throw" 更高的优先级 2,这样它将首先被调用,
-- 尽管 "Punch" 仍然是最近绑定的。
-- 按下 INPUT_KEY2 打印 "Throw",然后消耗输入。
ContextActionService:BindActionAtPriority("PriorityThrow", handleThrow, false, 2, INPUT_KEY2)
ContextActionService:BindActionAtPriority("PriorityPunch", handlePunch, false, 1, INPUT_KEY2)

BindActionToInputTypes
已弃用

BindActivate
功能: Input
ContextActionService:BindActivate(
userInputTypeForActivation:Enum.UserInputType, keyCodesForActivation:Tuple
):()
参数
userInputTypeForActivation:Enum.UserInputType
keyCodesForActivation:Tuple
返回
()

GetAllBoundActionInfo
功能: Input
ContextActionService:GetAllBoundActionInfo():Dictionary

GetBoundActionInfo
功能: Input
ContextActionService:GetBoundActionInfo(actionName:string):Dictionary
参数
actionName:string

GetButton
暂停
功能: Input
ContextActionService:GetButton(actionName:string):Instance
参数
actionName:string
返回

GetCurrentLocalToolIcon
功能: Input
ContextActionService:GetCurrentLocalToolIcon():string
返回

SetDescription
功能: Input
ContextActionService:SetDescription(
actionName:string, description:string
):()
参数
actionName:string
description:string
返回
()
代码示例
上下文操作服务触摸按钮
local ContextActionService = game:GetService("ContextActionService")
local ACTION_INSPECT = "Inspect"
local INPUT_INSPECT = Enum.KeyCode.E
local IMAGE_INSPECT = "rbxassetid://1826746856" -- 带 ? 的对话框图像
local function handleAction(actionName, inputState, _inputObject)
if actionName == ACTION_INSPECT and inputState == Enum.UserInputState.End then
print("正在检查")
end
end
-- 对于触摸设备,通过第三个参数在屏幕上自动创建一个按钮
ContextActionService:BindAction(ACTION_INSPECT, handleAction, true, INPUT_INSPECT)
-- 我们可以使用这些函数来自定义按钮:
ContextActionService:SetImage(ACTION_INSPECT, IMAGE_INSPECT)
ContextActionService:SetTitle(ACTION_INSPECT, "查看")
ContextActionService:SetDescription(ACTION_INSPECT, "检查某物。")
ContextActionService:SetPosition(ACTION_INSPECT, UDim2.new(0, 0, 0, 0))
-- 我们可以直接使用 ContextActionService:GetButton 操作按钮
local imgButton = ContextActionService:GetButton(ACTION_INSPECT)
if imgButton then -- 请记住:非触摸设备将不会返回任何内容!
imgButton.ImageColor3 = Color3.new(0.5, 1, 0.5) -- 将 ImageButton 染成绿色
end

SetImage
功能: Input
ContextActionService:SetImage(
actionName:string, image:string
):()
参数
actionName:string
image:string
返回
()
代码示例
上下文操作服务触摸按钮
local ContextActionService = game:GetService("ContextActionService")
local ACTION_INSPECT = "Inspect"
local INPUT_INSPECT = Enum.KeyCode.E
local IMAGE_INSPECT = "rbxassetid://1826746856" -- 带 ? 的对话框图像
local function handleAction(actionName, inputState, _inputObject)
if actionName == ACTION_INSPECT and inputState == Enum.UserInputState.End then
print("正在检查")
end
end
-- 对于触摸设备,通过第三个参数在屏幕上自动创建一个按钮
ContextActionService:BindAction(ACTION_INSPECT, handleAction, true, INPUT_INSPECT)
-- 我们可以使用这些函数来自定义按钮:
ContextActionService:SetImage(ACTION_INSPECT, IMAGE_INSPECT)
ContextActionService:SetTitle(ACTION_INSPECT, "查看")
ContextActionService:SetDescription(ACTION_INSPECT, "检查某物。")
ContextActionService:SetPosition(ACTION_INSPECT, UDim2.new(0, 0, 0, 0))
-- 我们可以直接使用 ContextActionService:GetButton 操作按钮
local imgButton = ContextActionService:GetButton(ACTION_INSPECT)
if imgButton then -- 请记住:非触摸设备将不会返回任何内容!
imgButton.ImageColor3 = Color3.new(0.5, 1, 0.5) -- 将 ImageButton 染成绿色
end

SetPosition
功能: Input
ContextActionService:SetPosition(
actionName:string, position:UDim2
):()
参数
actionName:string
position:UDim2
返回
()
代码示例
上下文操作服务触摸按钮
local ContextActionService = game:GetService("ContextActionService")
local ACTION_INSPECT = "Inspect"
local INPUT_INSPECT = Enum.KeyCode.E
local IMAGE_INSPECT = "rbxassetid://1826746856" -- 带 ? 的对话框图像
local function handleAction(actionName, inputState, _inputObject)
if actionName == ACTION_INSPECT and inputState == Enum.UserInputState.End then
print("正在检查")
end
end
-- 对于触摸设备,通过第三个参数在屏幕上自动创建一个按钮
ContextActionService:BindAction(ACTION_INSPECT, handleAction, true, INPUT_INSPECT)
-- 我们可以使用这些函数来自定义按钮:
ContextActionService:SetImage(ACTION_INSPECT, IMAGE_INSPECT)
ContextActionService:SetTitle(ACTION_INSPECT, "查看")
ContextActionService:SetDescription(ACTION_INSPECT, "检查某物。")
ContextActionService:SetPosition(ACTION_INSPECT, UDim2.new(0, 0, 0, 0))
-- 我们可以直接使用 ContextActionService:GetButton 操作按钮
local imgButton = ContextActionService:GetButton(ACTION_INSPECT)
if imgButton then -- 请记住:非触摸设备将不会返回任何内容!
imgButton.ImageColor3 = Color3.new(0.5, 1, 0.5) -- 将 ImageButton 染成绿色
end

SetTitle
功能: Input
ContextActionService:SetTitle(
actionName:string, title:string
):()
参数
actionName:string
title:string
返回
()
代码示例
上下文操作服务触摸按钮
local ContextActionService = game:GetService("ContextActionService")
local ACTION_INSPECT = "Inspect"
local INPUT_INSPECT = Enum.KeyCode.E
local IMAGE_INSPECT = "rbxassetid://1826746856" -- 带 ? 的对话框图像
local function handleAction(actionName, inputState, _inputObject)
if actionName == ACTION_INSPECT and inputState == Enum.UserInputState.End then
print("正在检查")
end
end
-- 对于触摸设备,通过第三个参数在屏幕上自动创建一个按钮
ContextActionService:BindAction(ACTION_INSPECT, handleAction, true, INPUT_INSPECT)
-- 我们可以使用这些函数来自定义按钮:
ContextActionService:SetImage(ACTION_INSPECT, IMAGE_INSPECT)
ContextActionService:SetTitle(ACTION_INSPECT, "查看")
ContextActionService:SetDescription(ACTION_INSPECT, "检查某物。")
ContextActionService:SetPosition(ACTION_INSPECT, UDim2.new(0, 0, 0, 0))
-- 我们可以直接使用 ContextActionService:GetButton 操作按钮
local imgButton = ContextActionService:GetButton(ACTION_INSPECT)
if imgButton then -- 请记住:非触摸设备将不会返回任何内容!
imgButton.ImageColor3 = Color3.new(0.5, 1, 0.5) -- 将 ImageButton 染成绿色
end

UnbindAction
功能: Input
ContextActionService:UnbindAction(actionName:string):()
参数
actionName:string
返回
()
代码示例
上下文操作服务工具重新加载
local ContextActionService = game:GetService("ContextActionService")
local ACTION_RELOAD = "重新加载"
local tool = script.Parent
local function handleAction(actionName, inputState, _inputObject)
if actionName == ACTION_RELOAD and inputState == Enum.UserInputState.Begin then
print("正在重新加载!")
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
功能: Input
ContextActionService:UnbindActivate(
userInputTypeForActivation:Enum.UserInputType, keyCodeForActivation:Enum.KeyCode
):()
参数
userInputTypeForActivation:Enum.UserInputType
keyCodeForActivation:Enum.KeyCode
默认值:"Unknown"
返回
()

UnbindAllActions
功能: Input
ContextActionService:UnbindAllActions():()
返回
()

活动
LocalToolEquipped
功能: Input
ContextActionService.LocalToolEquipped(toolEquipped:Instance):RBXScriptSignal
参数
toolEquipped:Instance

LocalToolUnequipped
功能: Input
ContextActionService.LocalToolUnequipped(toolUnequipped:Instance):RBXScriptSignal
参数
toolUnequipped:Instance

©2026 Roblox Corporation、Roblox、Roblox 标志及 Powering Imagination 是我们在美国及其他国家或地区的注册与未注册商标。