學習
引擎類別
ContextActionService
無法建立
服務

*此內容是使用 AI(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 = "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 = "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(`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
-- 在不指定優先級的情況下,最近綁定的行為會最先調用,
-- 所以按下 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 = "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 是我們在美國及其他國家地區的部分註冊與未註冊商標。