學習
引擎類別
UserInputService
無法建立
服務
未複製

*此內容是使用 AI(Beta 測試版)翻譯,可能含有錯誤。若要以英文檢視此頁面,請按一下這裡


概要
活動
InputBegan(input: InputObject,gameProcessedEvent: boolean):RBXScriptSignal
InputChanged(input: InputObject,gameProcessedEvent: boolean):RBXScriptSignal
InputEnded(input: InputObject,gameProcessedEvent: boolean):RBXScriptSignal
PointerAction(wheel: number,pan: Vector2,pinch: number,gameProcessedEvent: boolean):RBXScriptSignal
TouchDrag(dragDirection: Enum.SwipeDirection,numberOfTouches: number,gameProcessedEvent: boolean):RBXScriptSignal
TouchEnded(touch: InputObject,gameProcessedEvent: boolean):RBXScriptSignal
TouchLongPress(touchPositions: {any},state: Enum.UserInputState,gameProcessedEvent: boolean):RBXScriptSignal
TouchMoved(touch: InputObject,gameProcessedEvent: boolean):RBXScriptSignal
TouchPan(touchPositions: {any},totalTranslation: Vector2,velocity: Vector2,state: Enum.UserInputState,gameProcessedEvent: boolean):RBXScriptSignal
TouchPinch(touchPositions: {any},scale: number,velocity: number,state: Enum.UserInputState,gameProcessedEvent: boolean):RBXScriptSignal
TouchRotate(touchPositions: {any},rotation: number,velocity: number,state: Enum.UserInputState,gameProcessedEvent: boolean):RBXScriptSignal
TouchStarted(touch: InputObject,gameProcessedEvent: boolean):RBXScriptSignal
TouchSwipe(swipeDirection: Enum.SwipeDirection,numberOfTouches: number,gameProcessedEvent: boolean):RBXScriptSignal
TouchTap(touchPositions: {any},gameProcessedEvent: boolean):RBXScriptSignal
繼承成員

API 參考
屬性
AccelerometerEnabled
唯讀
未複製
平行讀取
功能: Input
UserInputService.AccelerometerEnabled:boolean
範例程式碼
用加速度計移動球體
local Workspace = game:GetService("Workspace")
local UserInputService = game:GetService("UserInputService")
local ball = script.Parent:WaitForChild("Ball")
local mass = ball:GetMass()
local gravityForce = ball:WaitForChild("GravityForce")
local function moveBall(gravity)
gravityForce.Force = gravity.Position * Workspace.Gravity * mass
end
if UserInputService.AccelerometerEnabled then
UserInputService.DeviceGravityChanged:Connect(moveBall)
end

GamepadEnabled
唯讀
未複製
平行讀取
功能: Input
UserInputService.GamepadEnabled:boolean

GyroscopeEnabled
唯讀
未複製
平行讀取
功能: Input
UserInputService.GyroscopeEnabled:boolean

KeyboardEnabled
唯讀
未複製
平行讀取
功能: Input
UserInputService.KeyboardEnabled:boolean

ModalEnabled
已棄用

MouseBehavior
平行讀取
功能: Input
UserInputService.MouseBehavior:Enum.MouseBehavior

MouseDeltaSensitivity
未複製
平行讀取
功能: Input
UserInputService.MouseDeltaSensitivity:number

MouseEnabled
唯讀
未複製
平行讀取
功能: Input
UserInputService.MouseEnabled:boolean

MouseIcon
平行讀取
功能: Input
UserInputService.MouseIcon:ContentId
範例程式碼
自訂滑鼠圖示
local UserInputService = game:GetService("UserInputService")
-- 要恢復游標到之前的設定,需要將其儲存到一個變數中
local savedCursor = nil
local function setTemporaryCursor(cursor:
-- 只有在當前沒有儲存的游標時才更新儲存的游標
if not savedCursor then
savedCursor = UserInputService.MouseIcon
end
UserInputService.MouseIcon = cursor
end
local function clearTemporaryCursor()
-- 只有在有儲存的游標時才恢復滑鼠游標
if savedCursor then
UserInputService.MouseIcon = savedCursor
-- 不要重複恢復相同的游標(可能會覆蓋其他腳本的設定)
savedCursor = nil
end
end
setTemporaryCursor("http://www.roblox.com/asset?id=163023520")
print(UserInputService.MouseIcon)
clearTemporaryCursor()

MouseIconContent
平行讀取
功能: Input
UserInputService.MouseIconContent:Content
範例程式碼
自訂滑鼠圖示
local UserInputService = game:GetService("UserInputService")
-- 要恢復游標到之前的設定,需要將其儲存到一個變數中
local savedCursor = nil
local function setTemporaryCursor(cursor:
-- 只有在當前沒有儲存的游標時才更新儲存的游標
if not savedCursor then
savedCursor = UserInputService.MouseIcon
end
UserInputService.MouseIcon = cursor
end
local function clearTemporaryCursor()
-- 只有在有儲存的游標時才恢復滑鼠游標
if savedCursor then
UserInputService.MouseIcon = savedCursor
-- 不要重複恢復相同的游標(可能會覆蓋其他腳本的設定)
savedCursor = nil
end
end
setTemporaryCursor("http://www.roblox.com/asset?id=163023520")
print(UserInputService.MouseIcon)
clearTemporaryCursor()

MouseIconEnabled
平行讀取
功能: Input
UserInputService.MouseIconEnabled:boolean

OnScreenKeyboardPosition
唯讀
未複製
平行讀取
功能: Input
UserInputService.OnScreenKeyboardPosition:Vector2

OnScreenKeyboardSize
唯讀
未複製
平行讀取
功能: Input
UserInputService.OnScreenKeyboardSize:Vector2

OnScreenKeyboardVisible
唯讀
未複製
平行讀取
功能: Input
UserInputService.OnScreenKeyboardVisible:boolean

PreferredInput
唯讀
未複製
平行讀取
功能: Input
UserInputService.PreferredInput:Enum.PreferredInput
範例程式碼
首選輸入偵測
local UserInputService = game:GetService("UserInputService")
local function preferredInputChanged()
local preferredInput = UserInputService.PreferredInput
if preferredInput == Enum.PreferredInput.Touch then
-- 玩家正在使用觸控啟用的 設備,且沒有其他輸入類型可用/連接
print("觸控")
elseif preferredInput == Enum.PreferredInput.Gamepad then
-- 玩家已連接或最後與 一個遊戲手把互動
print("遊戲手把")
elseif preferredInput == Enum.PreferredInput.KeyboardAndMouse then
-- 玩家已連接或最後與鍵盤或滑鼠互動
print("鍵盤和滑鼠")
end
end
preferredInputChanged()
UserInputService:GetPropertyChangedSignal("PreferredInput"):Connect(function()
preferredInputChanged()
end)

TouchEnabled
唯讀
未複製
平行讀取
功能: Input
UserInputService.TouchEnabled:boolean

TouchScreenEnabled
唯讀
未複製
Roblox 指令碼安全性
平行讀取
功能: Input
UserInputService.TouchScreenEnabled:boolean

UserHeadCFrame
已棄用

VREnabled
已棄用

方法
CreateVirtualInput
功能: Input
UserInputService:CreateVirtualInput():Object
返回

GamepadSupports
功能: Input
UserInputService:GamepadSupports(
gamepadNum:Enum.UserInputType, gamepadKeyCode:Enum.KeyCode
參數
gamepadKeyCode:Enum.KeyCode
返回

GetConnectedGamepads
功能: Input
UserInputService:GetConnectedGamepads():{any}
返回

GetDeviceAcceleration
功能: Input
UserInputService:GetDeviceAcceleration():InputObject
範例程式碼
輸出裝置加速
local UserInputService = game:GetService("UserInputService")
if UserInputService.AccelerometerEnabled then
local acceleration = UserInputService:GetDeviceAcceleration().Position
print(acceleration)
else
warn("無法獲取裝置加速度,因為裝置未啟用加速計!")
end

GetDeviceGravity
功能: Input
UserInputService:GetDeviceGravity():InputObject
範例程式碼
使用陀螺儀移動物體
local UserInputService = game:GetService("UserInputService")
local bubble = script.Parent:WaitForChild("Bubble")
local camera = workspace.CurrentCamera
camera.CameraType = Enum.CameraType.Scriptable
camera.CFrame = CFrame.new(0, 20, 0) * CFrame.Angles(-math.pi / 2, 0, 0)
if UserInputService.GyroscopeEnabled then
-- 當陀螺儀偵測到變化時綁定事件
UserInputService.DeviceGravityChanged:Connect(function(accel)
-- 根據陀螺儀數據在世界中移動氣泡
bubble.Position = Vector3.new(-8 * accel.Position.X, 1.8, -8 * accel.Position.Z)
end)
end

GetDeviceRotation
功能: Input
UserInputService:GetDeviceRotation():Tuple
返回
範例程式碼
輸出設備旋轉
local UserInputService = game:GetService("UserInputService")
if UserInputService.GyroscopeEnabled then
local _, cf = UserInputService:GetDeviceRotation()
print(cf)
else
warn("無法獲取設備旋轉,因為設備沒有啟用陀螺儀!")
end

GetFocusedTextBox
功能: Input
UserInputService:GetFocusedTextBox():TextBox
返回

GetGamepadConnected
功能: Input
UserInputService:GetGamepadConnected(gamepadNum:Enum.UserInputType):boolean
參數
返回

GetGamepadState
功能: Input
UserInputService:GetGamepadState(gamepadNum:Enum.UserInputType):{InputObject}
參數
返回

GetImageForKeyCode
功能: Input
UserInputService:GetImageForKeyCode(keyCode:Enum.KeyCode):ContentId
參數
keyCode:Enum.KeyCode
返回
ContentId
範例程式碼
用戶輸入服務 - 獲取鍵碼的圖像
local UserInputService = game:GetService("UserInputService")
local imageLabel = script.Parent
local key = Enum.KeyCode.ButtonA
-- 獲取對應鍵碼的圖像
local mappedIconImage = UserInputService:GetImageForKeyCode(key)
imageLabel.Image = mappedIconImage

GetKeysPressed
功能: Input
UserInputService:GetKeysPressed():{InputObject}
返回

GetLastInputType
功能: Input
UserInputService:GetLastInputType():Enum.UserInputType
範例程式碼
檢測最後輸入類型
local UserInputService = game:GetService("UserInputService")
if UserInputService:GetLastInputType() == Enum.UserInputType.Keyboard then
print("最近的輸入是鍵盤!")
end

GetMouseButtonsPressed
功能: Input
UserInputService:GetMouseButtonsPressed():{InputObject}
返回
範例程式碼
檢查按下的滑鼠按鈕
local UserInputService = game:GetService("UserInputService")
UserInputService.InputBegan:Connect(function(input, gameProcessedEvent)
-- 返回已按下的滑鼠按鈕的陣列
local buttonsPressed = UserInputService:GetMouseButtonsPressed()
local m1, m2 = false, false
for _, button in buttonsPressed do
if button.UserInputType == Enum.UserInputType.MouseButton1 then
print("按下了 MouseButton1!")
m1 = true
end
if button.UserInputType == Enum.UserInputType.MouseButton2 then
print("按下了 MouseButton2!")
m2 = true
end
if m1 and m2 then
print("兩個滑鼠按鈕都按下了!")
end
end
end)

GetMouseDelta
功能: Input
UserInputService:GetMouseDelta():Vector2
返回
範例程式碼
獲取滑鼠增量
local RunService = game:GetService("RunService")
local UserInputService = game:GetService("UserInputService")
UserInputService.MouseBehavior = Enum.MouseBehavior.LockCenter
local function onRenderStep()
local delta = UserInputService:GetMouseDelta()
if delta ~= Vector2.new(0, 0) then
print("滑鼠已移動", delta, "自上一步以來")
end
end
RunService:BindToRenderStep("MeasureMouseMovement", Enum.RenderPriority.Input.Value, onRenderStep)

GetMouseLocation
功能: Input
UserInputService:GetMouseLocation():Vector2
返回

GetNavigationGamepads
功能: Input
UserInputService:GetNavigationGamepads():{any}
返回

GetStringForKeyCode
功能: Input
UserInputService:GetStringForKeyCode(
參數
keyCode:Enum.KeyCode
預設值:"Default"
返回

GetSupportedGamepadKeyCodes
功能: Input
UserInputService:GetSupportedGamepadKeyCodes(gamepadNum:Enum.UserInputType):{any}
參數
返回

GetUserCFrame
已棄用

IsGamepadButtonDown
功能: Input
UserInputService:IsGamepadButtonDown(
gamepadNum:Enum.UserInputType, gamepadKeyCode:Enum.KeyCode
參數
gamepadKeyCode:Enum.KeyCode
返回

IsKeyDown
功能: Input
UserInputService:IsKeyDown(keyCode:Enum.KeyCode):boolean
參數
keyCode:Enum.KeyCode
返回

IsMouseButtonPressed
功能: Input
UserInputService:IsMouseButtonPressed(mouseButton:Enum.UserInputType):boolean
參數
mouseButton:Enum.UserInputType
返回

IsNavigationGamepad
功能: Input
UserInputService:IsNavigationGamepad(gamepadEnum:Enum.UserInputType):boolean
參數
gamepadEnum:Enum.UserInputType
返回

RecenterUserHeadCFrame
功能: Input
UserInputService:RecenterUserHeadCFrame():()
返回
()

SetNavigationGamepad
功能: Input
UserInputService:SetNavigationGamepad(
gamepadEnum:Enum.UserInputType, enabled:boolean
):()
參數
gamepadEnum:Enum.UserInputType
enabled:boolean
返回
()

活動
DeviceAccelerationChanged
功能: Input
UserInputService.DeviceAccelerationChanged(acceleration:InputObject):RBXScriptSignal
參數
acceleration:InputObject
範例程式碼
使用加速度計控制玩家
local UserInputService = game:GetService("UserInputService")
local Players = game:GetService("Players")
local SENSITIVITY = 0.2
local player = Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
local ready = true
local function changeAcceleration(acceleration)
if ready then
ready = false
local accel = acceleration.Position
if accel.Y >= SENSITIVITY then
humanoid.Jump = true
end
if accel.Z <= -SENSITIVITY then
humanoid:Move(Vector3.new(-1, 0, 0))
end
if accel.Z >= SENSITIVITY then
humanoid:Move(Vector3.new(1, 0, 0))
end
if accel.X <= -SENSITIVITY then
humanoid:Move(Vector3.new(0, 0, 1))
end
if accel.X >= SENSITIVITY then
humanoid:Move(Vector3.new(0, 0, -1))
end
task.wait(1)
ready = true
end
end
if UserInputService.AccelerometerEnabled then
UserInputService.DeviceAccelerationChanged:Connect(changeAcceleration)
end

DeviceGravityChanged
功能: Input
UserInputService.DeviceGravityChanged(gravity:InputObject):RBXScriptSignal
參數
gravity:InputObject
範例程式碼
用加速度計移動球體
local Workspace = game:GetService("Workspace")
local UserInputService = game:GetService("UserInputService")
local ball = script.Parent:WaitForChild("Ball")
local mass = ball:GetMass()
local gravityForce = ball:WaitForChild("GravityForce")
local function moveBall(gravity)
gravityForce.Force = gravity.Position * Workspace.Gravity * mass
end
if UserInputService.AccelerometerEnabled then
UserInputService.DeviceGravityChanged:Connect(moveBall)
end

DeviceRotationChanged
功能: Input
UserInputService.DeviceRotationChanged(
rotation:InputObject, cframe:CFrame
參數
rotation:InputObject
cframe:CFrame

GamepadConnected
功能: Input
UserInputService.GamepadConnected(gamepadNum:Enum.UserInputType):RBXScriptSignal
參數

GamepadDisconnected
功能: Input
UserInputService.GamepadDisconnected(gamepadNum:Enum.UserInputType):RBXScriptSignal
參數

InputBegan
功能: Input
UserInputService.InputBegan(
input:InputObject, gameProcessedEvent:boolean
參數
gameProcessedEvent:boolean

InputChanged
功能: Input
UserInputService.InputChanged(
input:InputObject, gameProcessedEvent:boolean
參數
gameProcessedEvent:boolean

InputEnded
功能: Input
UserInputService.InputEnded(
input:InputObject, gameProcessedEvent:boolean
參數
gameProcessedEvent:boolean

JumpRequest
功能: Input
UserInputService.JumpRequest():RBXScriptSignal
範例程式碼
禁用默認跳躍
local UserInputService = game:GetService("UserInputService")
local Players = game:GetService("Players")
local player = Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
humanoid:SetStateEnabled(Enum.HumanoidStateType.Jumping, false)
local processJumpRequest = false
local COOLDOWN_TIME = 0.5
local function jumpRequest()
if processJumpRequest == false then
processJumpRequest = true
-- 處理自定義跳躍請求
print("跳躍請求!")
-- 冷卻後重置防跳變量
task.wait(COOLDOWN_TIME)
processJumpRequest = false
end
end
UserInputService.JumpRequest:Connect(jumpRequest)

LastInputTypeChanged
功能: Input
UserInputService.LastInputTypeChanged(lastInputType:Enum.UserInputType):RBXScriptSignal
參數
lastInputType:Enum.UserInputType

PointerAction
功能: Input
UserInputService.PointerAction(
wheel:number, pan:Vector2, pinch:number, gameProcessedEvent:boolean
參數
wheel:number
pinch:number
gameProcessedEvent:boolean

TextBoxFocused
功能: Input
UserInputService.TextBoxFocused(textboxFocused:TextBox):RBXScriptSignal
參數
textboxFocused:TextBox
範例程式碼
文本框在獲得焦點及失去焦點時的修改
local UserInputService = game:GetService("UserInputService")
local function textBoxFocused(textBox)
textBox.BackgroundTransparency = 0
end
local function textBoxFocusReleased(textBox)
textBox.BackgroundTransparency = 0.5
end
UserInputService.TextBoxFocused:Connect(textBoxFocused)
UserInputService.TextBoxFocusReleased:Connect(textBoxFocusReleased)

TextBoxFocusReleased
功能: Input
UserInputService.TextBoxFocusReleased(textboxReleased:TextBox):RBXScriptSignal
參數
textboxReleased:TextBox
範例程式碼
文本框在獲得焦點及失去焦點時的修改
local UserInputService = game:GetService("UserInputService")
local function textBoxFocused(textBox)
textBox.BackgroundTransparency = 0
end
local function textBoxFocusReleased(textBox)
textBox.BackgroundTransparency = 0.5
end
UserInputService.TextBoxFocused:Connect(textBoxFocused)
UserInputService.TextBoxFocusReleased:Connect(textBoxFocusReleased)

TouchDrag
功能: Input
UserInputService.TouchDrag(
dragDirection:Enum.SwipeDirection, numberOfTouches:number, gameProcessedEvent:boolean
參數
dragDirection:Enum.SwipeDirection
numberOfTouches:number
gameProcessedEvent:boolean

TouchEnded
功能: Input
UserInputService.TouchEnded(
touch:InputObject, gameProcessedEvent:boolean
參數
gameProcessedEvent:boolean

TouchLongPress
功能: Input
UserInputService.TouchLongPress(
touchPositions:{any}, state:Enum.UserInputState, gameProcessedEvent:boolean
參數
touchPositions:{any}
gameProcessedEvent:boolean

TouchMoved
功能: Input
UserInputService.TouchMoved(
touch:InputObject, gameProcessedEvent:boolean
參數
gameProcessedEvent:boolean

TouchPan
功能: Input
UserInputService.TouchPan(
touchPositions:{any}, totalTranslation:Vector2, velocity:Vector2, state:Enum.UserInputState, gameProcessedEvent:boolean
參數
touchPositions:{any}
totalTranslation:Vector2
velocity:Vector2
gameProcessedEvent:boolean

TouchPinch
功能: Input
UserInputService.TouchPinch(
touchPositions:{any}, scale:number, velocity:number, state:Enum.UserInputState, gameProcessedEvent:boolean
參數
touchPositions:{any}
scale:number
velocity:number
gameProcessedEvent:boolean

TouchRotate
功能: Input
UserInputService.TouchRotate(
touchPositions:{any}, rotation:number, velocity:number, state:Enum.UserInputState, gameProcessedEvent:boolean
參數
touchPositions:{any}
rotation:number
velocity:number
gameProcessedEvent:boolean

TouchStarted
功能: Input
UserInputService.TouchStarted(
touch:InputObject, gameProcessedEvent:boolean
參數
gameProcessedEvent:boolean

TouchSwipe
功能: Input
UserInputService.TouchSwipe(
swipeDirection:Enum.SwipeDirection, numberOfTouches:number, gameProcessedEvent:boolean
參數
swipeDirection:Enum.SwipeDirection
numberOfTouches:number
gameProcessedEvent:boolean

TouchTap
功能: Input
UserInputService.TouchTap(
touchPositions:{any}, gameProcessedEvent:boolean
參數
touchPositions:{any}
gameProcessedEvent:boolean

TouchTapInWorld
功能: Input
UserInputService.TouchTapInWorld(
position:Vector2, processedByUI:boolean
參數
position:Vector2
processedByUI:boolean

UserCFrameChanged
已棄用

WindowFocused
功能: Input
UserInputService.WindowFocused():RBXScriptSignal

WindowFocusReleased
功能: Input
UserInputService.WindowFocusReleased():RBXScriptSignal
範例程式碼
視窗焦點離開腳本(LocalScript)
local UserInputService = game:GetService("UserInputService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local awayEvent = ReplicatedStorage:WaitForChild("AwayEvent")
local function focusGained()
awayEvent:FireServer(false)
end
local function focusReleased()
awayEvent:FireServer(true)
end
UserInputService.WindowFocused:Connect(focusGained)
UserInputService.WindowFocusReleased:Connect(focusReleased)
窗口焦點離開腳本 (腳本)
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local awayEvent = Instance.new("RemoteEvent")
awayEvent.Name = "AwayEvent"
awayEvent.Parent = ReplicatedStorage
local function manageForceField(player, away)
if away then
local forceField = Instance.new("ForceField")
forceField.Parent = player.Character
else
local forceField = player.Character:FindFirstChildOfClass("ForceField")
if forceField then
forceField:Destroy()
end
end
end
awayEvent.OnServerEvent:Connect(manageForceField)

©2026 Roblox Corporation、Roblox、Roblox 標誌及 Powering Imagination 是我們在美國及其他國家地區的部分註冊與未註冊商標。