概要
属性
MouseIcon:ContentId |
方法
GamepadSupports(gamepadNum: Enum.UserInputType,gamepadKeyCode: Enum.KeyCode):boolean |
GetGamepadConnected(gamepadNum: Enum.UserInputType):boolean |
GetGamepadState(gamepadNum: Enum.UserInputType):{InputObject} |
GetImageForKeyCode(keyCode: Enum.KeyCode):ContentId |
GetStringForKeyCode(keyCode: Enum.KeyCode,format: Enum.KeyCodeStringFormat):string |
GetSupportedGamepadKeyCodes(gamepadNum: Enum.UserInputType):{any} |
GetUserCFrame(type: Enum.UserCFrame):CFrame |
IsGamepadButtonDown(gamepadNum: Enum.UserInputType,gamepadKeyCode: Enum.KeyCode):boolean |
IsKeyDown(keyCode: Enum.KeyCode):boolean |
IsMouseButtonPressed(mouseButton: Enum.UserInputType):boolean |
IsNavigationGamepad(gamepadEnum: Enum.UserInputType):boolean |
SetNavigationGamepad(gamepadEnum: Enum.UserInputType,enabled: boolean):() |
活动
API 参考
属性
AccelerometerEnabled
代码示例
使用加速度计移动球
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)
endModalEnabled
MouseIcon
UserInputService.MouseIcon:ContentId
代码示例
自定义鼠标图标
local UserInputService = game:GetService("UserInputService")
-- 为了将光标恢复到之前设置的状态,需要将其保存到一个变量
local savedCursor = nil
local function setTemporaryCursor(cursor: string)
-- 仅在当前没有保存的光标时更新保存的光标
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
代码示例
自定义鼠标图标
local UserInputService = game:GetService("UserInputService")
-- 为了将光标恢复到之前设置的状态,需要将其保存到一个变量
local savedCursor = nil
local function setTemporaryCursor(cursor: string)
-- 仅在当前没有保存的光标时更新保存的光标
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()PreferredInput
代码示例
首选输入检测
local UserInputService = game:GetService("UserInputService")
local function preferredInputChanged()
local preferredInput = UserInputService.PreferredInput
if preferredInput == Enum.PreferredInput.Touch then
-- 玩家在启用触控的设备上,且没有其他输入类型可用/已连接
print("Touch")
elseif preferredInput == Enum.PreferredInput.Gamepad then
-- 玩家已连接或最近与游戏手柄交互
print("Gamepad")
elseif preferredInput == Enum.PreferredInput.KeyboardAndMouse then
-- 玩家已连接或最近与键盘或鼠标交互
print("KeyboardAndMouse")
end
end
preferredInputChanged()
UserInputService:GetPropertyChangedSignal("PreferredInput"):Connect(function()
preferredInputChanged()
end)UserHeadCFrame
VREnabled
方法
GamepadSupports
UserInputService:GamepadSupports(
参数
返回
GetDeviceAcceleration
代码示例
输出设备加速度
local UserInputService = game:GetService("UserInputService")
if UserInputService.AccelerometerEnabled then
local acceleration = UserInputService:GetDeviceAcceleration().Position
print(acceleration)
else
warn("无法获取设备加速度,因为设备没有启用的加速度计!")
endGetDeviceGravity
代码示例
使用陀螺仪移动物体
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)
endGetDeviceRotation
GetGamepadConnected
参数
返回
GetGamepadState
参数
返回
GetImageForKeyCode
参数
返回
ContentId
代码示例
用户输入服务 - 获取按键代码的图像
local UserInputService = game:GetService("UserInputService")
local imageLabel = script.Parent
local key = Enum.KeyCode.ButtonA
-- 获取映射图标图像
local mappedIconImage = UserInputService:GetImageForKeyCode(key)
imageLabel.Image = mappedIconImageGetLastInputType
代码示例
检测最后的输入类型
local UserInputService = game:GetService("UserInputService")
if UserInputService:GetLastInputType() == Enum.UserInputType.Keyboard then
print("最近的输入是键盘!")
endGetMouseButtonsPressed
返回
代码示例
检查按下的鼠标按钮
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
返回
代码示例
获取鼠标增量
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("测量鼠标移动", Enum.RenderPriority.Input.Value, onRenderStep)GetStringForKeyCode
参数
| 默认值:"Default" |
返回
GetSupportedGamepadKeyCodes
参数
返回
GetUserCFrame
IsGamepadButtonDown
UserInputService:IsGamepadButtonDown(
参数
返回
IsMouseButtonPressed
参数
返回
参数
返回
RecenterUserHeadCFrame
UserInputService:RecenterUserHeadCFrame():()
返回
()
参数
返回
()
活动
DeviceAccelerationChanged
参数
代码示例
使用加速度计控制玩家
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)
endDeviceGravityChanged
参数
代码示例
使用加速度计移动球
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)
endDeviceRotationChanged
参数
GamepadConnected
参数
GamepadDisconnected
参数
InputBegan
参数
InputChanged
参数
InputEnded
参数
JumpRequest
代码示例
禁用默认跳跃
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
参数
PointerAction
TextBoxFocused
参数
代码示例
聚焦和失去焦点时的文本框修改
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
参数
代码示例
聚焦和失去焦点时的文本框修改
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
UserInputService.TouchDrag(
参数
TouchEnded
参数
TouchLongPress
UserInputService.TouchLongPress(
参数
TouchMoved
参数
TouchPan
UserInputService.TouchPan(
touchPositions:{any}, totalTranslation:Vector2, velocity:Vector2, state:Enum.UserInputState, gameProcessedEvent:boolean
参数
TouchPinch
UserInputService.TouchPinch(
touchPositions:{any}, scale:number, velocity:number, state:Enum.UserInputState, gameProcessedEvent:boolean
参数
TouchRotate
UserInputService.TouchRotate(
touchPositions:{any}, rotation:number, velocity:number, state:Enum.UserInputState, gameProcessedEvent:boolean
参数
TouchStarted
参数
TouchSwipe
UserInputService.TouchSwipe(
参数
TouchTap
TouchTapInWorld
UserCFrameChanged
WindowFocusReleased
代码示例
窗口失去焦点脚本 (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)