UserInputService

显示已弃用

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

无法创建
服务
未复制

UserInputService 是用于检测和捕获用户设备上可用的不同类型输入的服务。

该服务的主要目的是允许体验与多种可用输入形式合作,例如游戏手柄、触摸屏和键盘。它允许 LocalScript 根据设备执行不同的操作,从而为最终用户提供最佳体验。

这服务的一些用途包括在用户与 GUI、工具和其他游戏实例互动时检测用户输入。为了检测用户输入,服务必须查找服务事件。例如,服务可以检测事件,例如当用户使用 UserInputService.TouchStarted 触摸移动设备屏幕,或使用 UserInputService.GamepadConnected 连接游戏手柄,例如 Xbox 控制器到他们的设备。

由于此服务仅限客户端使用,它只能在 LocalScriptModuleScriptLocalScript 要求时才能工作。由于 UserInputService 只是客户端,游戏中的用户只能检测自己的输入 - 而不是他人的输入。

还见 ContextActionService , 一种服务,可以让你将函数绑定到多个用户输入。

概要

属性

方法

活动

属性

AccelerometerEnabled

只读
未复制
读取并联

该属性描述用户的设备是否具有加速器

加速器是一种在大多数移动设备中找到的组件,用于测量加速(速度变化)。

例如,以下代码片段显示了如何检查用户的设备是否具有加速器。


local UserInputService = game:GetService("UserInputService")
local accelerometerEnabled = UserInputService.AccelerometerEnabled
if accelerometerEnabled then
print("Accelerometer enabled!")
else
print("Accelerometer not enabled!")
end

如果设备启用了加速器,你可以使用 UserInputService:GetDeviceAcceleration() 函数或跟踪当设备的加速度发生变化时使用 UserInputService.DeviceAccelerationChanged 事件来获取当前加速度。

由于 UserInputService 仅为客户端,此属性只能在 LocalScript 中使用。

代码示例

This code adds a force on a part so that it falls in the direction of actual gravity relative to the user's device. In order for this example to work as expected, it must be placed in a LocalScript and the user's device must have an accelerometer.

Move a Ball using the Accelerometer

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

只读
未复制
读取并联

该属性描述用户使用的设备是否有可用的游戏手柄。如果游戏手柄可用,你可以使用 UserInputService:GetConnectedGamepads() 来检索连接的游戏手柄列表。

由于 UserInputService 仅为客户端,此属性只能在 LocalScript 中使用。

还见:

GyroscopeEnabled

只读
未复制
读取并联

该属性描述用户的设备是否具有陀螺仪。

陀螺仪是一种在大多数移动设备中找到的组件,可检测方向和旋转速度。

如果用户的设备具有陀螺仪,您可以使用 UserInputService:GetDeviceRotation() 函数和 UserInputService.DeviceRotationChanged 事件将其融入到游戏中。


local UserInputService = game:GetService("UserInputService")
local gyroIsEnabled = UserInputService.GyroscopeEnabled
if gyroIsEnabled then
print("Gyroscope is enabled!")
else
print("Gyroscope is not enabled!")
end

由于 UserInputService 仅为客户端,此属性只能在 LocalScript 中使用。

KeyboardEnabled

只读
未复制
读取并联

该属性描述了用户的设备是否有可用的键盘。当用户的设备具有可用键盘时,此属性为 true ;当它没有时,为 false

它可以用于确定用户是否有可用的键盘 - 这可能很重要,如果你想检查是否可以使用 UserInputService:IsKeyDown()UserInputService:GetKeysPressed() 来检查键盘输入。

由于 UserInputService 仅为客户端,此属性只能在 LocalScript 中使用。

代码示例

这个例子会打印“用户的设备具有可用的键盘!”如果 键盘启用true 和“用户的设备没有可用的键盘!”如果 键盘启用false

检查键盘是否启用

local UserInputService = game:GetService("UserInputService")
if UserInputService.KeyboardEnabled then
print("The user's device has an available keyboard!")
else
print("The user's device does not have an available keyboard!")
end

MouseBehavior

读取并联

这个属性设置了用户的鼠标如何根据 Enum.MouseBehavior 枚列行为。它可以设置为三个值:

该属性的值不会影响事件跟踪鼠标移动的灵敏度。例如, GetMouseDelta 返回无论鼠标是否锁定或能够自由移动到用户屏幕周围的同一 Vector2 屏幕位置,以像素计算。因此,控制相机的默认脚本不受此属性影响。

如果启用了 GuiButtonModalGuiButton.Visible ,该属性将被覆盖,除非玩家的右键按钮向下。

请注意,如果鼠标被锁定,UserInputService.InputChanged 仍会在玩家移动鼠标时发射,并将传递Delta,该鼠标试图移动的。此外,如果玩家被踢出游戏,鼠标将被强行解锁。

由于 UserInputService 仅为客户端,此属性只能在 LocalScript 中使用。

代码示例

这个例子创建了一个望远镜脚本,降低玩家的 FieldOfView()MouseDeltaSensitivity() 当玩家使用具有 MouseEnabled() 的鼠标点击时。该脚本还指向玩家的 向鼠标单击的世界位置。

当玩家再次离开鼠标点击时,玩家的相机返回到与玩家使用脚本时相同的视野和 领域。

当玩家使用望远镜时,脚本通过设置玩家的 MouseBehavior() 到锁定中心来锁定玩家的鼠标到屏幕中心。当玩家移动鼠标时,按照传递给 InputObject.DeltaInputChanged() 属性移动相机,表示玩家在屏幕位置上更改了鼠标的 Vector2

为了这个例子按期望的方式运行,它应该放置在 LocalScript 中。

创建望远镜脚本

local UserInputService = game:GetService("UserInputService")
local Players = game:GetService("Players")
local player = Players.LocalPlayer
local character = player.CharacterAdded:Wait()
local head = character:WaitForChild("Head", false)
local mouse = player:GetMouse()
local zoomed = false
local camera = game.Workspace.CurrentCamera
local target = nil
local originalProperties = {
FieldOfView = nil,
_CFrame = nil,
MouseBehavior = nil,
MouseDeltaSensitivity = nil,
}
local AngleX, TargetAngleX = 0, 0
local AngleY, TargetAngleY = 0, 0
-- 将相机重置为 CFrame 和 FieldOfView,在缩放之前
local function ResetCamera()
target = nil
camera.CameraType = Enum.CameraType.Custom
camera.CFrame = originalProperties._CFrame
camera.FieldOfView = originalProperties.FieldOfView
UserInputService.MouseBehavior = originalProperties.MouseBehavior
UserInputService.MouseDeltaSensitivity = originalProperties.MouseDeltaSensitivity
end
local function ZoomCamera()
-- 允许通过脚本更改相机
camera.CameraType = Enum.CameraType.Scriptable
-- 在缩放之前存储相机属性
originalProperties._CFrame = camera.CFrame
originalProperties.FieldOfView = camera.FieldOfView
originalProperties.MouseBehavior = UserInputService.MouseBehavior
originalProperties.MouseDeltaSensitivity = UserInputService.MouseDeltaSensitivity
-- 缩放相机
target = mouse.Hit.Position
local eyesight = head.Position
camera.CFrame = CFrame.new(eyesight, target)
camera.Focus = CFrame.new(target)
camera.FieldOfView = 10
-- 锁定并减慢鼠标
UserInputService.MouseBehavior = Enum.MouseBehavior.LockCenter
UserInputService.MouseDeltaSensitivity = 1
-- 重置缩放角度
AngleX, TargetAngleX = 0, 0
AngleY, TargetAngleY = 0, 0
end
-- 切换镜头缩放/取消缩放
local function MouseClick()
if zoomed then
-- 缩放相机
ResetCamera()
else
-- 在相机放大
ZoomCamera()
end
zoomed = not zoomed
end
local function MouseMoved(input)
if zoomed then
local sensitivity = 0.6 -- 任何高于此的数值都会使查找上下更加困难;推荐在 0~1 之间的数值
local smoothness = 0.05 -- 推荐在 0~1 之间的任何东西
local delta = Vector2.new(input.Delta.x / sensitivity, input.Delta.y / sensitivity) * smoothness
local X = TargetAngleX - delta.y
local Y = TargetAngleY - delta.x
TargetAngleX = (X >= 80 and 80) or (X <= -80 and -80) or X
TargetAngleY = (Y >= 80 and 80) or (Y <= -80 and -80) or Y
AngleX = AngleX + (TargetAngleX - AngleX) * 0.35
AngleY = AngleY + (TargetAngleY - AngleY) * 0.15
camera.CFrame = CFrame.new(head.Position, target)
* CFrame.Angles(0, math.rad(AngleY), 0)
* CFrame.Angles(math.rad(AngleX), 0, 0)
end
end
local function InputBegan(input, _gameProcessedEvent)
if input.UserInputType == Enum.UserInputType.MouseButton1 then
MouseClick()
end
end
local function InputChanged(input, _gameProcessedEvent)
if input.UserInputType == Enum.UserInputType.MouseMovement then
MouseMoved(input)
end
end
if UserInputService.MouseEnabled then
UserInputService.InputBegan:Connect(InputBegan)
UserInputService.InputChanged:Connect(InputChanged)
end

MouseDeltaSensitivity

未复制
读取并联

该属性决定了用户的 Mouse 的灵敏度。

灵敏度决定了物理鼠标的移动转换到游戏中鼠标的移动程度。这可以用来调整如何敏感事件跟踪鼠标移动,例如 GetMouseDelta , 是否跟随鼠标移动。

该属性不会影响鼠标图标的移动。它也不会影响客户端 设置 菜单的 设置 选项卡中找到的 相机灵敏度 设置,该设置也调整了事件跟踪鼠标移动的灵敏度。

该属性的最大值为 10 并且最小值为 0。较低的值对应较低的灵敏度,较高的值对应更高的灵敏度。

当灵敏度为 0 时,跟踪鼠标移动的事件仍会发射,但所有显示鼠标位置变更的参数和属性都会返回 Vector2.new()Vector3.new()InputObject.Delta 的情况下。例如,GetMouseDelta 总是会返回(0,0)。

代码示例

这个例子创建了一个望远镜脚本,降低玩家的 FieldOfView()MouseDeltaSensitivity() 当玩家使用具有 MouseEnabled() 的鼠标点击时。该脚本还指向玩家的 向鼠标单击的世界位置。

当玩家再次离开鼠标点击时,玩家的相机返回到与玩家使用脚本时相同的视野和 领域。

当玩家使用望远镜时,脚本通过设置玩家的 MouseBehavior() 到锁定中心来锁定玩家的鼠标到屏幕中心。当玩家移动鼠标时,按照传递给 InputObject.DeltaInputChanged() 属性移动相机,表示玩家在屏幕位置上更改了鼠标的 Vector2

为了这个例子按期望的方式运行,它应该放置在 LocalScript 中。

创建望远镜脚本

local UserInputService = game:GetService("UserInputService")
local Players = game:GetService("Players")
local player = Players.LocalPlayer
local character = player.CharacterAdded:Wait()
local head = character:WaitForChild("Head", false)
local mouse = player:GetMouse()
local zoomed = false
local camera = game.Workspace.CurrentCamera
local target = nil
local originalProperties = {
FieldOfView = nil,
_CFrame = nil,
MouseBehavior = nil,
MouseDeltaSensitivity = nil,
}
local AngleX, TargetAngleX = 0, 0
local AngleY, TargetAngleY = 0, 0
-- 将相机重置为 CFrame 和 FieldOfView,在缩放之前
local function ResetCamera()
target = nil
camera.CameraType = Enum.CameraType.Custom
camera.CFrame = originalProperties._CFrame
camera.FieldOfView = originalProperties.FieldOfView
UserInputService.MouseBehavior = originalProperties.MouseBehavior
UserInputService.MouseDeltaSensitivity = originalProperties.MouseDeltaSensitivity
end
local function ZoomCamera()
-- 允许通过脚本更改相机
camera.CameraType = Enum.CameraType.Scriptable
-- 在缩放之前存储相机属性
originalProperties._CFrame = camera.CFrame
originalProperties.FieldOfView = camera.FieldOfView
originalProperties.MouseBehavior = UserInputService.MouseBehavior
originalProperties.MouseDeltaSensitivity = UserInputService.MouseDeltaSensitivity
-- 缩放相机
target = mouse.Hit.Position
local eyesight = head.Position
camera.CFrame = CFrame.new(eyesight, target)
camera.Focus = CFrame.new(target)
camera.FieldOfView = 10
-- 锁定并减慢鼠标
UserInputService.MouseBehavior = Enum.MouseBehavior.LockCenter
UserInputService.MouseDeltaSensitivity = 1
-- 重置缩放角度
AngleX, TargetAngleX = 0, 0
AngleY, TargetAngleY = 0, 0
end
-- 切换镜头缩放/取消缩放
local function MouseClick()
if zoomed then
-- 缩放相机
ResetCamera()
else
-- 在相机放大
ZoomCamera()
end
zoomed = not zoomed
end
local function MouseMoved(input)
if zoomed then
local sensitivity = 0.6 -- 任何高于此的数值都会使查找上下更加困难;推荐在 0~1 之间的数值
local smoothness = 0.05 -- 推荐在 0~1 之间的任何东西
local delta = Vector2.new(input.Delta.x / sensitivity, input.Delta.y / sensitivity) * smoothness
local X = TargetAngleX - delta.y
local Y = TargetAngleY - delta.x
TargetAngleX = (X >= 80 and 80) or (X <= -80 and -80) or X
TargetAngleY = (Y >= 80 and 80) or (Y <= -80 and -80) or Y
AngleX = AngleX + (TargetAngleX - AngleX) * 0.35
AngleY = AngleY + (TargetAngleY - AngleY) * 0.15
camera.CFrame = CFrame.new(head.Position, target)
* CFrame.Angles(0, math.rad(AngleY), 0)
* CFrame.Angles(math.rad(AngleX), 0, 0)
end
end
local function InputBegan(input, _gameProcessedEvent)
if input.UserInputType == Enum.UserInputType.MouseButton1 then
MouseClick()
end
end
local function InputChanged(input, _gameProcessedEvent)
if input.UserInputType == Enum.UserInputType.MouseMovement then
MouseMoved(input)
end
end
if UserInputService.MouseEnabled then
UserInputService.InputBegan:Connect(InputBegan)
UserInputService.InputChanged:Connect(InputChanged)
end

MouseEnabled

只读
未复制
读取并联

该属性描述了用户的设备是否有可用的鼠标。当用户的设备有可用的鼠标时,此属性为 true ;当它没有时,为 false


local UserInputService = game:GetService("UserInputService")
if UserInputService.MouseEnabled then
print("The user's device has an available mouse!")
else
print("The user's device does not have an available mouse!")
end

在使用 UserInputService 鼠标功能之前,例如 UserInputService:GetMouseLocation() ,必须检查此项。

由于 UserInputService 仅为客户端,此属性只能在 LocalScript 中使用。

还见:

代码示例

这个例子创建了一个望远镜脚本,降低玩家的 FieldOfView()MouseDeltaSensitivity() 当玩家使用具有 MouseEnabled() 的鼠标点击时。该脚本还指向玩家的 向鼠标单击的世界位置。

当玩家再次离开鼠标点击时,玩家的相机返回到与玩家使用脚本时相同的视野和 领域。

当玩家使用望远镜时,脚本通过设置玩家的 MouseBehavior() 到锁定中心来锁定玩家的鼠标到屏幕中心。当玩家移动鼠标时,按照传递给 InputObject.DeltaInputChanged() 属性移动相机,表示玩家在屏幕位置上更改了鼠标的 Vector2

为了这个例子按期望的方式运行,它应该放置在 LocalScript 中。

创建望远镜脚本

local UserInputService = game:GetService("UserInputService")
local Players = game:GetService("Players")
local player = Players.LocalPlayer
local character = player.CharacterAdded:Wait()
local head = character:WaitForChild("Head", false)
local mouse = player:GetMouse()
local zoomed = false
local camera = game.Workspace.CurrentCamera
local target = nil
local originalProperties = {
FieldOfView = nil,
_CFrame = nil,
MouseBehavior = nil,
MouseDeltaSensitivity = nil,
}
local AngleX, TargetAngleX = 0, 0
local AngleY, TargetAngleY = 0, 0
-- 将相机重置为 CFrame 和 FieldOfView,在缩放之前
local function ResetCamera()
target = nil
camera.CameraType = Enum.CameraType.Custom
camera.CFrame = originalProperties._CFrame
camera.FieldOfView = originalProperties.FieldOfView
UserInputService.MouseBehavior = originalProperties.MouseBehavior
UserInputService.MouseDeltaSensitivity = originalProperties.MouseDeltaSensitivity
end
local function ZoomCamera()
-- 允许通过脚本更改相机
camera.CameraType = Enum.CameraType.Scriptable
-- 在缩放之前存储相机属性
originalProperties._CFrame = camera.CFrame
originalProperties.FieldOfView = camera.FieldOfView
originalProperties.MouseBehavior = UserInputService.MouseBehavior
originalProperties.MouseDeltaSensitivity = UserInputService.MouseDeltaSensitivity
-- 缩放相机
target = mouse.Hit.Position
local eyesight = head.Position
camera.CFrame = CFrame.new(eyesight, target)
camera.Focus = CFrame.new(target)
camera.FieldOfView = 10
-- 锁定并减慢鼠标
UserInputService.MouseBehavior = Enum.MouseBehavior.LockCenter
UserInputService.MouseDeltaSensitivity = 1
-- 重置缩放角度
AngleX, TargetAngleX = 0, 0
AngleY, TargetAngleY = 0, 0
end
-- 切换镜头缩放/取消缩放
local function MouseClick()
if zoomed then
-- 缩放相机
ResetCamera()
else
-- 在相机放大
ZoomCamera()
end
zoomed = not zoomed
end
local function MouseMoved(input)
if zoomed then
local sensitivity = 0.6 -- 任何高于此的数值都会使查找上下更加困难;推荐在 0~1 之间的数值
local smoothness = 0.05 -- 推荐在 0~1 之间的任何东西
local delta = Vector2.new(input.Delta.x / sensitivity, input.Delta.y / sensitivity) * smoothness
local X = TargetAngleX - delta.y
local Y = TargetAngleY - delta.x
TargetAngleX = (X >= 80 and 80) or (X <= -80 and -80) or X
TargetAngleY = (Y >= 80 and 80) or (Y <= -80 and -80) or Y
AngleX = AngleX + (TargetAngleX - AngleX) * 0.35
AngleY = AngleY + (TargetAngleY - AngleY) * 0.15
camera.CFrame = CFrame.new(head.Position, target)
* CFrame.Angles(0, math.rad(AngleY), 0)
* CFrame.Angles(math.rad(AngleX), 0, 0)
end
end
local function InputBegan(input, _gameProcessedEvent)
if input.UserInputType == Enum.UserInputType.MouseButton1 then
MouseClick()
end
end
local function InputChanged(input, _gameProcessedEvent)
if input.UserInputType == Enum.UserInputType.MouseMovement then
MouseMoved(input)
end
end
if UserInputService.MouseEnabled then
UserInputService.InputBegan:Connect(InputBegan)
UserInputService.InputChanged:Connect(InputChanged)
end

MouseIcon

ContentId
读取并联

MouseIcon 属性决定使用作指针的图像。如果为空,默认使用箭头。当鼠标悬停在某些用户界面对象上,例如 ImageButton , TextButton , TextBoxProximityPrompt 时,此图像将被替换并暂时忽略。

要完全隐藏鼠标,请不要使用透明图像。相反,将 设置为 false。

代码示例

这个例子将用户鼠标图标更改为看起来像龙图像。

用户输入服务.鼠标图标

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()
print(UserInputService.MouseIcon)

MouseIconEnabled

读取并联

该属性决定是否显示 图标,当鼠标的图标可见时,当 它不可见时。

例如,下面的代码片段隐藏了鼠标的图标。


local UserInputService = game:GetService("UserInputService")
UserInputService.MouseIconEnabled = false

由于 UserInputService 仅为客户端,此属性只能在 LocalScript 中使用。

代码示例

This example hides the mouse icon while the player beings using their keyboard, such as to chat or enter text into a TextBox. The mouse icon reappears when the user resumes mouse input.

This uses the LastInputType() event to determine when the user begins keyboard input and mouse input - based on the value of the lastInputType argument.

In order for this example to work as expected, it should be placed in a LocalScript.

Hide Mouse During Keyboard Input

local UserInputService = game:GetService("UserInputService")
local mouseInput = {
Enum.UserInputType.MouseButton1,
Enum.UserInputType.MouseButton2,
Enum.UserInputType.MouseButton3,
Enum.UserInputType.MouseMovement,
Enum.UserInputType.MouseWheel,
}
local keyboard = Enum.UserInputType.Keyboard
local function toggleMouse(lastInputType)
if lastInputType == keyboard then
UserInputService.MouseIconEnabled = false
return
end
for _, mouse in pairs(mouseInput) do
if lastInputType == mouse then
UserInputService.MouseIconEnabled = true
return
end
end
end
UserInputService.LastInputTypeChanged:Connect(toggleMouse)

OnScreenKeyboardPosition

只读
未复制
读取并联

此属性描述屏幕上键盘的位置以像素为单位。键盘的位置为 Vector2.new(0, 0) 当它不可见时。

由于 UserInputService 仅为客户端,此属性只能在 LocalScriptScript 中使用,其中 RunContext 设置为 Enum.RunContext.Client

还见 OnScreenKeyboardVisibleOnScreenKeyboardSize

OnScreenKeyboardSize

只读
未复制
读取并联

此属性描述屏幕上键盘的像素大小。当键盘不可见时,键盘的大小为 Vector2.new(0, 0)

由于 UserInputService 仅为客户端,此属性只能在 LocalScriptScript 中使用,其中 RunContext 设置为 Enum.RunContext.Client

还见 OnScreenKeyboardVisibleOnScreenKeyboardPosition

OnScreenKeyboardVisible

只读
未复制
读取并联

此属性描述了屏幕上的键盘是否目前在用户的屏幕上可见。

由于 UserInputService 仅为客户端,此属性只能在 LocalScriptScript 中使用,其中 RunContext 设置为 Enum.RunContext.Client

还见 OnScreenKeyboardSizeOnScreenKeyboardPosition

PreferredInput

只读
未复制
读取并联

TouchEnabled

只读
未复制
读取并联

该属性描述了用户当前设备是否有可用的触摸屏。

属性用于确定用户的设备是否具有触摸屏,因此触摸事件是否会发生。如果触摸启用是真的,你可以使用用户输入服务事件,例如 UserInputService.TouchStartedUserInputService.TouchEnded 来跟踪用户何时开始和停止触摸他们设备的屏幕。

下面的代码片段打印用户的设备是否有触摸屏。


local UserInputService = game:GetService("UserInputService")
if UserInputService.TouchEnabled then
print("The user's device has a touchscreen!")
else
print("The user's device does not have a touchscreen!")
end

还见:

VREnabled

只读
未复制
读取并联

该属性描述用户是否使用虚拟现实(VR)设备。

如果启用了 VR 设备,您可以通过函数如 UserInputService:GetUserCFrame() 与其位置和移动进行互动。您还可以使用 UserInputService.UserCFrameChanged 事件反应虚拟现实设备的移动。


local UserInputService = game:GetService("UserInputService")
local isUsingVR = UserInputService.VREnabled
if isUsingVR then
print("User is using a VR headset!")
else
print("User is not using a VR headset!")
end

由于 UserInputService 仅为客户端,此属性只能在 LocalScript 中使用。

还见:

代码示例

This example demonstrates how to implement a head tracking script that mirrors the movement of a virtual reality (VR) headset (the user's actual head) to their in-game character's head.

The example first check if the user is using a VR device by checking the value of the VREnabled() property. This example only works if the user is using a VR headset.

To determine the initial CFrame of the character's head, the code sample calls GetUserCFrame() and passes Enum.UserCFrame.Head as the argument.

To update the head's CFrame whenever the user's VR headset moves, the example connects the VRService.UserCFrameChanged event to the TrackHead() function. When the event fires to indicate that a VR device moved, TrackHead() checks if the headset moved. If the headset moved, the function updates the CFrame of the character's head to the CFrame value provided as an argument.

As the UserCFrame enum also tracks VR left and right hand devices, the concept of VR device tracking can be expanded to other character bodyparts.

In order for the example to work as expected, it must be placed in a LocalScript.

VR Head Tracking

local VRService = game:GetService("VRService")
local Players = game:GetService("Players")
local player = Players.LocalPlayer
local character = player.CharacterAdded:Wait()
local head = character:WaitForChild("Head")
local function TrackHead(inputType, value)
if inputType == Enum.UserCFrame.Head then
head.CFrame = value
end
end
if VRService.VREnabled then
-- Set the initial CFrame
head.CFrame = VRService:GetUserCFrame(Enum.UserCFrame.Head)
-- Track VR headset movement and mirror for character's head
VRService.UserCFrameChanged:Connect(TrackHead)
end

方法

GamepadSupports

此函数返回给定的 Enum.UserInputType 游戏手柄是否支持与给定的 Enum.KeyCode 相对应的按钮。该函数用于确定有效的游戏手柄输入。

要确定哪些 Enum.UserInputType 游戏手柄连接,请使用 UserInputService:GetConnectedGamepads()

由于 UserInputService 仅为客户端,此函数只能在 LocalScript 中使用。

还见:

参数

gamepadNum: Enum.UserInputType

游戏手柄的 Enum.UserInputType

默认值:""
gamepadKeyCode: Enum.KeyCode

问题中的按钮的 Enum.KeyCode

默认值:""

返回

是否给定的游戏手柄支持与给定的Enum.KeyCode相对应的按钮。

GetConnectedGamepads

此函数返回当前连接的 Enum.UserInputType 游戏手柄阵列。如果没有连接游戏手柄,这个阵列将为空。此外,它只返回游戏手柄的 UserInputType 对象。例如,这个事件将返回连接的 Gamepad1 对象,但不是键盘对象。

例如,以下代码片段检索连接的游戏手柄,并将它们存储在名为 connectedGamepads 的变量中。


local UserInputService = game:GetService("UserInputService")
local connectedGamepads = UserInputService:GetConnectedGamepads()

要检查特定游戏手柄是否连接,请使用 UserInputService:GetGamepadConnected()

由于 UserInputService 仅为客户端,此函数只能在 LocalScript 中使用。

还见:


返回

一个与连接到用户设备的游戏手柄相对应的 UserInputTypes 阵列。

GetDeviceAcceleration

获取设备加速函数确定用户设备的当前加速。它返回一个 InputObject 描述设备当前加速的文本。

为了使此功能正常运行,用户的设备必须配备加速器。要检查用户的设备是否具有启用加速器,您可以检查 UserInputService.AccelerometerEnabled 属性。

如果你想在用户的设备加速更改时跟踪,你可以使用 UserInputService.DeviceAccelerationChanged 事件。

由于它只能在本地发射,它只能在 LocalScript 中使用。


返回

代码示例

This example checks if a user's device has an enabled accelerometer and, if true, it outputs the current acceleration.

Output Device Acceleration

local UserInputService = game:GetService("UserInputService")
if UserInputService.AccelerometerEnabled then
local acceleration = UserInputService:GetDeviceAcceleration().Position
print(acceleration)
else
warn("Cannot get device acceleration because device does not have an enabled accelerometer!")
end

GetDeviceGravity

该函数返回一个 InputObject 描述设备当前重力向量的函数。

重力向量由设备对重力世界力的相对位置决定。例如,如果设备完全垂直(肖像),重力向量是 Vector3.new(0, 0, -9.18) 。如果设备的左侧指向下,向量是 Vector3.new(9.81, 0, 0)。最后,如果设备的后面指向下,向量是 Vector3.new(0, -9.81, 0)。

该函数可能用于启用用户的设备在游戏中影响或控制重力或移动游戏对象,例如球。

重力仅用于启用陀螺仪的设备的玩家 - 例如移动设备。

要检查用户的设备是否具有启用陀螺仪,请检查 UserInputService.GyroscopeEnabled 的值。如果设备具有启用陀螺仪,您还可以使用 UserInputService.DeviceGravityChanged 事件来跟踪用户设备上重力力量的变化。

由于 UserInputService 仅为客户端,此函数只能在 LocalScript 中使用。


返回

代码示例

This example implements a level where the bubble will move along the X and Z axes depending on the device's current gryoscope position.

Moving Objects with the Gyroscope

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
-- Bind event to when gyroscope detects change
UserInputService.DeviceGravityChanged:Connect(function(accel)
-- Move the bubble in the world based on the gyroscope data
bubble.Position = Vector3.new(-8 * accel.Position.X, 1.8, -8 * accel.Position.Z)
end)
end

GetDeviceRotation

该函数返回一个 和一个 描述设备当前旋转向量的 。

这被发射到一个输入对象。输入对象的 位置 属性是一个 Enum.InputType.Gyroscope ,用于跟踪每个本地设备轴的总旋转。

设备旋转只能在具有 gyroscope 的设备上跟踪。

由于此函数是在本地发射的,它只能在 LocalScript 中使用。


返回

包含两个属性的 tuple:

  1. delta 属性描述最后发生的旋转量
  2. CFrame 是设备当前对其默认参考框的旋转。

代码示例

This example checks if a user's device has an enabled gyroscope and, if true, it outputs the device's current CFrame.

Output Device Rotation

local UserInputService = game:GetService("UserInputService")
if UserInputService.GyroscopeEnabled then
local _, cf = UserInputService:GetDeviceRotation()
print(cf)
else
warn("Cannot get device rotation because device does not have an enabled gyroscope!")
end

GetFocusedTextBox

此函数返回客户端目前关注的 TextBox。A TextBox 可以由用户手动选择,或使用 TextBox:CaptureFocus() 函数强制选择。如果未选择TextBox,该函数将返回nil

由于 UserInputService 仅为客户端,此函数只能在 LocalScript 中使用。

还看到也看到

返回

GetGamepadConnected

此函数返回是否连接了带有给定 Enum.UserInputType 的游戏手柄到客户端。

这可用于检查是否有特定游戏手柄,例如 'Gamepad1' 连接到客户端设备。

要检索所有连接的游戏手柄的列表,请使用 UserInputService:GetConnectedGamepads()

由于 UserInputService 仅为客户端,此函数只能在 LocalScript 中使用。

还见:

参数

gamepadNum: Enum.UserInputType

游戏手柄中的 Enum.UserInputType

默认值:""

返回

是否连接了与 Enum.UserInputType 相关的游戏手柄。

GetGamepadState

此函数返回给定 游戏手柄上所有可用输入的阵列,代表每个输入的最后输入状态。

要找到连接的游戏手柄的 UserInputTypes,请使用 UserInputService:GetConnectedGamepads()

由于此函数仅在本地发射,它只能在 LocalScript 中使用。

还见:

参数

gamepadNum: Enum.UserInputType

与问题的游戏手柄相对应的 Enum.UserInputType

默认值:""

返回

一个InputObjects阵列,代表给定游戏手柄的所有可用输入的当前状态。

GetImageForKeyCode

ContentId

该方法接受请求的 Enum.KeyCode 并返回与连接到的游戏手柄设备相关的图像(仅限于 Xbox、PlayStation 和 Windows)。这意味着如果连接的控制器是 Xbox 一控制器,用户看到的是 Xbox 资产。同样,如果连接的设备是 PlayStation 控制器,用户看到 PlayStation 资产。如果您想使用自定义资产,请参阅GetStringForKeyCode()

参数

keyCode: Enum.KeyCode

用于获取相关图像的 Enum.KeyCode

默认值:""

返回

ContentId

返回的图像资产 ID。

代码示例

此 API 返回给定的 Enum.KeyCode 的请求图像。

用户输入服务 - 获取关键码的图像

local UserInputService = game:GetService("UserInputService")
local imageLabel = script.Parent
local key = Enum.KeyCode.ButtonA
local mappedIconImage = UserInputService:GetImageForKeyCode(key)
imageLabel.Image = mappedIconImage

GetKeysPressed

该函数返回与当前按下的键相关的 InputObjects 阵列。

这个阵列可以循环以确定当前正在按下哪些键,使用 InputObject.KeyCode 值。

要检查特定键是否被按下,请使用 UserInputService:IsKeyDown()

由于 UserInputService 仅为客户端,此函数只能在 LocalScript 中使用。


返回

与当前按下的键相关的 InputObjects 阵列。

GetLastInputType

该函数返回与用户最新输入相关的 'Enum.UserInputType`。

例如,如果用户的以前输入按下了空格键,那么返回的 Enum.UserInputType 将是 '键盘'

UserInputService.LastInputTypeChanged事件可用于跟踪用户最后使用的Enum.UserInputType更改时间。

由于 UserInputService 仅为客户端,此函数只能在 LocalScript 中使用。


返回

与用户最新输入相关的 Enum.UserInputType

代码示例

This example gets the last Enum.UserInputType and outputs if it was keyboard input.

Detect Last Input Type

local UserInputService = game:GetService("UserInputService")
if UserInputService:GetLastInputType() == Enum.UserInputType.Keyboard then
print("Most recent input was keyboard!")
end

GetMouseButtonsPressed

此函数返回一个阵列 InputObjects 与当前按下的鼠标按钮相对应的阵列。

由此函数跟踪的鼠标按钮包括:


<td>描述</td>
</tr>
</thead>
<tr>
<td>鼠标按钮1</td>
<td>左鼠标按钮。</td>
</tr>
<tr>
<td>鼠标按钮2</td>
<td>右键按钮。</td>
</tr>
<tr>
<td>鼠标按钮3</td>
<td>中间的鼠标按钮。</td>
</tr>
名称

如果用户在函数调用时没有按下任何鼠标按钮,它将返回一个空阵列。

由于 UserInputService 仅为客户端,此函数只能在 LocalScript 中使用。


返回

一组与当前持有的鼠标按钮相对应的 InputObjects 阵列。

代码示例

This example checks if the user pressed MouseButton1 (left), MouseButton2 (right), or both mouse buttons. This can be extended to behave differently depending on which mouse buttons are pressed, including MouseButton3 (middle).

Check Pressed Mouse Buttons

local UserInputService = game:GetService("UserInputService")
UserInputService.InputBegan:Connect(function(input, gameProcessedEvent)
-- Return an array of the pressed mouse buttons
local buttonsPressed = UserInputService:GetMouseButtonsPressed()
local m1, m2 = false, false
for _, button in buttonsPressed do
if button.UserInputType == Enum.UserInputType.MouseButton1 then
print("MouseButton1 pressed!")
m1 = true
end
if button.UserInputType == Enum.UserInputType.MouseButton2 then
print("MouseButton2 pressed!")
m2 = true
end
if m1 and m2 then
print("Both mouse buttons pressed!")
end
end
end)

GetMouseDelta

这个函数返回玩家位置的 Mouse 在最后渲染帧中的变化,以像素为单位,作为 Vector2 。此函数仅在鼠标使用 UserInputService.MouseBehavior 属性被锁定时才能工作。如果鼠标未被锁定,返回的 Vector2 值将为零。

在客户端设置和 UserInputService.MouseDeltaSensitivity 中确定的鼠标灵敏度将影响结果。

由于 UserInputService 仅为客户端,此函数只能在 LocalScript 中使用。


返回

更改鼠标的移动。

代码示例

The following example measures any mouse movement in pixels from the last render step to the current render step. If the user has set their camera sensitivity to be higher or lower than 1 in the experience's menu, it will affect the returned value.

Getting Mouse Delta

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("The mouse has moved", delta, "since the last step")
end
end
RunService:BindToRenderStep("MeasureMouseMovement", Enum.RenderPriority.Input.Value, onRenderStep)

GetMouseLocation

此函数返回一个 Vector2 代表玩家当前屏幕位置的 Mouse 以像素表示左上角。这不会计算到 Enum.ScreenInsets ;要获得顶部左侧和底部右侧的插入,请调用 GuiService:GetGuiInset()

如果鼠标指针的位置在屏幕外或玩家的设备没有鼠标,返回的值将未确定。

由于 UserInputService 仅为客户端,此函数只能在 LocalScript 中使用。


返回

一个 Vector2 代表鼠标当前屏幕位置,以像素表示。

GetNavigationGamepads

该函数返回连接并启用 GUI 导航的游戏手柄 UserInputTypes 阵列。这个列表是按优先级排序的,这意味着它可以被循环以确定哪个游戏手柄应具有导航控件。

连接到游戏手柄是导航游戏手柄还是仅决定哪个游戏手柄控制导航图形用户界面。这不会影响导航控件。

由于 UserInputService 仅为客户端,此函数只能在 LocalScript 中使用。

还见:


返回

一个由 UserInputTypes 组成的阵列,可用于在优先级顺序下的图形用户界面导航。

GetStringForKeyCode

获取键码字符串 返回用户应按下的键来输入给定的 Enum.KeyCode ,考虑到他们的键盘布局。对于需要按住某些修改器的键代码,该函数返回键以及修改器一起按下的键。请参阅以下示例进行进一步解释。

当使用非QWERTY键盘布局与 Roblox 交互时,键代码映射到相等的 QWERTY 位置。例如,按下 A 在 AZERTY 键盘上会导致 Enum.KeyCode.Q。这种映射可能会导致经验用户界面元素的信息不匹配。例如,“按下 M 打开地图”在 AZERTY 键盘上不准确;需要“按下 ? 打开地图”,这与 QWERTY 上的 M 位置相同。这个函数通过提供非QWERTY键盘布局使用时按下的实际键来解决这个问题。


local UserInputService = game:GetService("UserInputService")
local textLabel = script.Parent
local mapKey = Enum.KeyCode.M
textLabel.Text = "Press " .. UserInputService:GetStringForKeyCode(mapKey) .. " to open the map"
关于 QWERTY 键盘的例子

<th>返回值</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>Enum.KeyCode.Q</code></td>
<td><code>Q</code></td>
</tr>
<tr>
<td><code>Enum.KeyCode.W</code></td>
<td><code>W</code></td>
</tr>
<tr>
<td><code>Enum.KeyCode.Equals</code></td>
<td><code>=</code></td>
</tr>
<tr>
<td><code>Enum.KeyCode.At</code></td>
<td><code>2</code> 因为 <code>@</code> 是用 <kbd>Shift</kbd><kbd>2</kbd> 编写的</td>
</tr>
</tbody>
密码键码
关于 AZERTY 键盘的例子

<th>返回值</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>Enum.KeyCode.Q</code></td>
<td><code>A</code></td>
</tr>
<tr>
<td><code>Enum.KeyCode.W</code></td>
<td><code>Z</code></td>
</tr>
<tr>
<td><code>Enum.KeyCode.Equals</code></td>
<td><code>=</code></td>
</tr>
<tr>
<td><code>Enum.KeyCode.At</code></td>
<td><code>É</code></td>
</tr>
</tbody>
密码键码
游戏手柄使用

返回最近连接的游戏手柄的字符串映射。如果连接的控制器不支持,函数返回请求的键代码的默认字符串转换。

以下示例显示了您如何映射自定义资产到 ButtonA :


local UserInputService = game:GetService("UserInputService")
local imageLabel = script.Parent
local key = Enum.KeyCode.ButtonA
local mappings = {
ButtonA = "rbxasset://BUTTON_A_ASSET", -- Replace with the desired ButtonA asset
ButtonCross = "rbxasset://BUTTON_CROSS_ASSET" -- Replace with the desired ButtonCross asset
}
local mappedKey = UserInputService:GetStringForKeyCode(key)
local image = mappings[mappedKey]
imageLabel.Image = image
游戏手柄映射

方向键代码无论基于设备都没有任何差异。Enum.KeyCode.ButtonSelect在某些情况下有稍微不同的行为。使用两个 PlayStation 映射来确保用户看到正确的按钮。


<th>游戏机返回值</th>
<th>Xbox 返回值</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>Enum.KeyCode.ButtonA</code></td>
<td><code>按钮交叉</code></td>
<td><code>按钮A</code></td>
</tr>
<tr>
<td><code>Enum.KeyCode.ButtonB</code></td>
<td><code>按钮圆圈</code></td>
<td><code>按钮B</code></td>
</tr>
<tr>
<td><code>Enum.KeyCode.ButtonX</code></td>
<td><code>按钮方块</code></td>
<td><code>按钮X</code></td>
</tr>
<tr>
<td><code>Enum.KeyCode.ButtonY</code></td>
<td><code>按钮三角形</code></td>
<td><code>按钮Y</code></td>
</tr>
<tr>
<td><code>Enum.KeyCode.ButtonL1</code></td>
<td><code>按钮L1</code></td>
<td><code>按钮LB</code></td>
</tr>
<tr>
<td><code>Enum.KeyCode.ButtonL2</code></td>
<td><code>按钮L2</code></td>
<td><code>按钮LT</code></td>
</tr>
<tr>
<td><code>Enum.KeyCode.ButtonL3</code></td>
<td><code>按钮L3</code></td>
<td><code>按钮LS</code></td>
</tr>
<tr>
<td><code>Enum.KeyCode.ButtonR1</code></td>
<td><code>按钮R1</code></td>
<td><code>按钮RB</code></td>
</tr>
<tr>
<td><code>Enum.KeyCode.ButtonR2</code></td>
<td><code>按钮R2</code></td>
<td><code>按钮RT</code></td>
</tr>
<tr>
<td><code>Enum.KeyCode.ButtonR3</code></td>
<td><code>按钮R3</code></td>
<td><code>按钮RS</code></td>
</tr>
<tr>
<td><code>Enum.KeyCode.按钮开始</code></td>
<td><code>按钮选项</code></td>
<td><code>按钮开始</code></td>
</tr>
<tr>
<td><code>Enum.KeyCode.按钮选择</code></td>
<td><code>按钮触摸板</code> 和 <code>按钮共享</code></td>
<td><code>按钮选择</code></td>
</tr>
</tbody>
密码键码
关于键盘的系统图像

当使用 Enum.KeyCode 可能更好地用作图像时,例如用于用户界面中的 ImageLabel,您可以使用以下遗产图标。然而,建议您使用 GetImageForKeyCode() 作为更现代、跨平台方法来检索 Xbox 和 PlayStation 控制器图标。


<th>图像</th>
<th>资产 ID</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>Enum.KeyCode.ButtonX</code></td>
<td>
<img src="../../../assets/scripting/controls/xboxX.png" width="24">
</img>
</td>
<td><code>rbxasset://textures/ui/Controls/xboxX.png</code></td>
</tr>
<tr>
<td><code>Enum.KeyCode.ButtonY</code></td>
<td>
<img src="../../../assets/scripting/controls/xboxY.png" width="24">
</img>
</td>
<td><code>rbxasset://textures/ui/Controls/xboxY.png</code></td>
</tr>
<tr>
<td><code>Enum.KeyCode.ButtonA</code></td>
<td>
<img src="../../../assets/scripting/controls/xboxA.png" width="24">
</img>
</td>
<td><code>rbxasset://textures/ui/Controls/xboxA.png</code></td>
</tr>
<tr>
<td><code>Enum.KeyCode.ButtonB</code></td>
<td>
<img src="../../../assets/scripting/controls/xboxB.png" width="24">
</img>
</td>
<td><code>rbxasset://textures/ui/Controls/xboxB.png</code></td>
</tr>
<tr>
<td><code>Enum.KeyCode.DPadLeft</code></td>
<td>
<img src="../../../assets/scripting/controls/dpadLeft.png" width="24">
</img>
</td>
<td><code>rbxasset://textures/ui/Controls/dpadLeft.png</code></td>
</tr>
<tr>
<td><code>Enum.KeyCode.DPadRight</code></td>
<td>
<img src="../../../assets/scripting/controls/dpadRight.png" width="24">
</img>
</td>
<td><code>rbxasset://textures/ui/Controls/dpadRight.png</code></td>
</tr>
<tr>
<td><code>Enum.KeyCode.DPadUp</code></td>
<td>
<img src="../../../assets/scripting/controls/dpadUp.png" width="24">
</img>
</td>
<td><code>rbxasset://textures/ui/Controls/dpadUp.png</code></td>
</tr>
<tr>
<td><code>Enum.KeyCode.DPadDown</code></td>
<td>
<img src="../../../assets/scripting/controls/dpadDown.png" width="24">
</img>
</td>
<td><code>rbxasset://textures/ui/Controls/dpadDown.png</code></td>
</tr>
<tr>
<td><code>Enum.KeyCode.ButtonSelect</code></td>
<td>
<img src="../../../assets/scripting/controls/xboxView.png" width="24">
</img>
</td>
<td><code>rbxasset://textures/ui/Controls/xboxView.png</code></td>
</tr>
<tr>
<td><code>Enum.KeyCode.ButtonStart</code></td>
<td>
<img src="../../../assets/scripting/controls/xboxmenu.png" width="24">
</img>
</td>
<td><code>rbxasset://textures/ui/Controls/xboxmenu.png</code></td>
</tr>
<tr>
<td><code>Enum.KeyCode.ButtonL1</code></td>
<td>
<img src="../../../assets/scripting/controls/xboxLB.png" width="24">
</img>
</td>
<td><code>rbxasset://textures/ui/Controls/xboxLB.png</code></td>
</tr>
<tr>
<td><code>Enum.KeyCode.ButtonR1</code></td>
<td>
<img src="../../../assets/scripting/controls/xboxRB.png" width="24">
</img>
</td>
<td><code>rbxasset://textures/ui/Controls/xboxRB.png</code></td>
</tr>
<tr>
<td><code>Enum.KeyCode.ButtonL2</code></td>
<td>
<img src="../../../assets/scripting/controls/xboxLT.png" width="24">
</img>
</td>
<td><code>rbxasset://textures/ui/Controls/xboxLT.png</code></td>
</tr>
<tr>
<td><code>Enum.KeyCode.ButtonR2</code></td>
<td>
<img src="../../../assets/scripting/controls/xboxRT.png" width="24">
</img>
</td>
<td><code>rbxasset://textures/ui/Controls/xboxRT.png</code></td>
</tr>
<tr>
<td><code>Enum.KeyCode.ButtonL3</code></td>
<td>
<img src="../../../assets/scripting/controls/xboxLS.png" width="24">
</img>
</td>
<td><code>rbxasset://textures/ui/Controls/xboxLS.png</code></td>
</tr>
<tr>
<td><code>Enum.KeyCode.ButtonR3</code></td>
<td>
<img src="../../../assets/scripting/controls/xboxRS.png" width="24">
</img>
</td>
<td><code>rbxasset://textures/ui/Controls/xboxRS.png</code></td>
</tr>
<tr>
<td><code>Enum.KeyCode.Thumbstick1</code></td>
<td>
<img src="../../../assets/scripting/controls/xboxLSDirectional.png" width="24">
</img>
</td>
<td><code>rbxasset://textures/ui/Controls/xboxLSDirectional.png</code></td>
</tr>
<tr>
<td><code>Enum.KeyCode.Thumbstick2</code></td>
<td>
<img src="../../../assets/scripting/controls/xboxRSDirectional.png" width="24">
</img>
</td>
<td><code>rbxasset://textures/ui/Controls/xboxRSDirectional.png</code></td>
</tr>
<tr>
<td><code>Enum.KeyCode.Backspace</code></td>
<td>
<img src="../../../assets/scripting/controls/backspace.png" width="24">
</img>
</td>
<td><code>rbxasset://textures/ui/Controls/backspace.png</code></td>
</tr>
<tr>
<td><code>Enum.KeyCode.Return</code></td>
<td>
<img src="../../../assets/scripting/controls/return.png" width="24">
</img>
</td>
<td><code>rbxasset://textures/ui/Controls/return.png</code></td>
</tr>
<tr>
<td><code>Enum.KeyCode.左Shift</code></td>
<td>
<img src="../../../assets/scripting/controls/shift.png" width="24">
</img>
</td>
<td><code>rbxasset://textures/ui/Controls/shift.png</code></td>
</tr>
<tr>
<td><code>Enum.KeyCode.RightShift</code></td>
<td>
<img src="../../../assets/scripting/controls/shift.png" width="24">
</img>
</td>
<td><code>rbxasset://textures/ui/Controls/shift.png</code></td>
</tr>
<tr>
<td><code>Enum.KeyCode.Tab</code></td>
<td>
<img src="../../../assets/scripting/controls/tab.png" width="24">
</img>
</td>
<td><code>rbxasset://textures/ui/Controls/tab.png</code></td>
</tr>
<tr>
<td><code>Enum.KeyCode.Quote</code></td>
<td>
<img src="../../../assets/scripting/controls/apostrophe.png" width="24">
</img>
</td>
<td><code>rbxasset://textures/ui/Controls/apostrophe.png</code></td>
</tr>
<tr>
<td><code>Enum.KeyCode.Comma</code></td>
<td>
<img src="../../../assets/scripting/controls/comma.png" width="24">
</img>
</td>
<td><code>rbxasset://textures/ui/Controls/comma.png</code></td>
</tr>
<tr>
<td><code>Enum.KeyCode.Backquote</code></td>
<td>
<img src="../../../assets/scripting/controls/graveaccent.png" width="24">
</img>
</td>
<td><code>rbxasset://textures/ui/Controls/graveaccent.png</code></td>
</tr>
<tr>
<td><code>Enum.KeyCode.Period</code></td>
<td>
<img src="../../../assets/scripting/controls/period.png" width="24">
</img>
</td>
<td><code>rbxasset://textures/ui/Controls/period.png</code></td>
</tr>
<tr>
<td><code>Enum.KeyCode.Space</code></td>
<td>
<img src="../../../assets/scripting/controls/spacebar.png" width="24">
</img>
</td>
<td><code>rbxasset://textures/ui/Controls/spacebar.png</code></td>
</tr>
</tbody>
密码键码

参数

keyCode: Enum.KeyCode
默认值:""

返回

GetSupportedGamepadKeyCodes

此函数返回与给定 Enum.UserInputType 相关的游戏手柄支持的 KeyCodes 阵列。

这个函数可以用来确定哪些键盘被连接的游戏手柄支持,而不被支持。要确定是否支持特定的 KeyCode,请使用 UserInputService:GamepadSupports()

如果在不存在或未连接的游戏手柄上调用此函数,该函数将返回一个空阵列。

由于 UserInputService 仅为客户端,此函数只能在 LocalScript 中使用。

还见:

参数

gamepadNum: Enum.UserInputType

游戏手柄的 Enum.UserInputType

默认值:""

返回

由给定游戏手柄支持的 KeyCodes 阵列。

代码示例

这个例子获取一个列表的导航游戏板和一个列表支持的Enum.KeyCodes。然后,它循环通过支持的 KeyCode 列表并将 ButtonXX 键绑定到函数,如果使用游戏手柄 ContextActionService

绑定支持游戏手柄键盘命令

local UserInputService = game:GetService("UserInputService")
local ContextActionService = game:GetService("ContextActionService")
local function actionHandler(actionName, inputState, inputObject)
if inputState == Enum.UserInputState.Begin then
print("Action Handler: " .. actionName)
print(inputObject)
end
-- 由于此函数不返回任何内容,这个处理器将
-- “洗手台”输入,其他任何操作处理器不会在之后调用
-- 这个。
end
local navGamepads = UserInputService:GetNavigationGamepads()
for _, gamepad in pairs(navGamepads) do
local supportedKeyCodes = UserInputService:GetSupportedGamepadKeyCodes(gamepad)
for _, keycode in pairs(supportedKeyCodes) do
if keycode == Enum.KeyCode.ButtonX then
ContextActionService:BindAction("SampleAction", actionHandler, false, Enum.KeyCode.ButtonX)
end
if keycode == Enum.KeyCode.X then
ContextActionService:BindAction("SampleAction", actionHandler, false, Enum.KeyCode.X)
end
end
end

IsGamepadButtonDown

这个函数检查特定按钮是否在特定游戏手柄上按下。如果 gamepad 拥有指定的 button 按下,它将返回 true;否则将返回 false。

有效的用户输入类型

指定的游戏手柄应该是以下用户输入类型枚举值之一:


<tr>
<td>Enum.UserInputType.游戏手柄1-8</td>
</tr>
名称
有效的键盘代码

指定的按钮应该是以下键盘枚值之一:


<tr>
<td>Enum.KeyCode.ButtonX</td>
</tr>
<tr>
<td>Enum.KeyCode.ButtonY</td>
</tr>
<tr>
<td>Enum.KeyCode.ButtonA</td>
</tr>
<tr>
<td>Enum.KeyCode.ButtonB</td>
</tr>
<tr>
<td>Enum.KeyCode.ButtonR1</td>
</tr>
<tr>
<td>Enum.KeyCode.ButtonL1</td>
</tr>
<tr>
<td>Enum.KeyCode.ButtonR2</td>
</tr>
<tr>
<td>Enum.KeyCode.ButtonL2</td>
</tr>
<tr>
<td>Enum.KeyCode.ButtonR3</td>
</tr>
<tr>
<td>Enum.KeyCode.ButtonL3</td>
</tr>
<tr>
<td>Enum.KeyCode.按钮开始</td>
</tr>
<tr>
<td>Enum.KeyCode.按钮选择</td>
</tr>
<tr>
<td>Enum.KeyCode.DPad左</td>
</tr>
<tr>
<td>Enum.KeyCode.DPadRight</td>
</tr>
<tr>
<td>Enum.KeyCode.DPadUp</td>
</tr>
<tr>
<td>Enum.KeyCode.DPadDown</td>
</tr>
名称

这可以用来检查是否按住了特定按钮,例如 A。例如:


local UserInputService = game:GetService("UserInputService")
local button = Enum.KeyCode.ButtonA
local gamepad = Enum.UserInputType.Gamepad1
local isButtonHeld = UserInputService:IsGamepadButtonDown(gamepad, button)

由于 UserInputService 仅为客户端,此函数只能在 LocalScript 中使用。

还见:

参数

gamepadNum: Enum.UserInputType

给定游戏手柄的 Enum.UserInputType

默认值:""
gamepadKeyCode: Enum.KeyCode

指定按钮的 Enum.KeyCode

默认值:""

返回

指定的游戏手柄按钮在指定的游戏手柄上是否按下。

IsKeyDown

此函数返回用户是否按住与给定 Enum.KeyCode 相关的键。如果指定的键按下了,它将返回 true ;如果未按下,它将返回 false

这可用于检查是否按下了特定的键,例如空格键。例如:


local UserInputService = game:GetService("UserInputService")
local spaceHeld = UserInputService:IsKeyDown(Enum.KeyCode.Space)

要检索用户按下的所有键列表,请使用 UserInputService:GetKeysPressed() 函数。

由于 UserInputService 仅为客户端,此函数只能在 LocalScript 中使用。

还见:

参数

keyCode: Enum.KeyCode

钥匙的 Enum.KeyCode

默认值:""

返回

是否需要按住指定的键。

IsMouseButtonPressed

这个函数接受一个鼠标按钮 Enum.UserInputType 并返回一个 bool 指示它是否当前按下。

检查鼠标按钮的值取决于传给函数的 Enum.UserInputType 值作为参数。例如:


local UserInputService = game:GetService("UserInputService")
local pressed = UserInputService:IsMouseButtonPressed(Enum.UserInputType.MouseButton1)

由于 UserInputService 仅为客户端,此函数只能在 LocalScript 中使用。

参数

mouseButton: Enum.UserInputType

鼠标按钮的 Enum.UserInputType

默认值:""

返回

给定的鼠标按钮是否当前已按住。

IsNavigationGamepad

该函数返回 true 如果指定的 Enum.UserInputType 游戏手柄允许控制导航和选择 GuiObjects

如果你想设置导航游戏手柄,你可以使用 UserInputService:SetNavigationGamepad() 。你也可以使用 UserInputService:GetNavigationGamepads() 来获取所有导航游戏手柄的列表。

例如,下面的代码检查游戏手柄1是否作为导航游戏手柄:


local UserInputService = game:GetService("UserInputService")
if UserInputService:IsNavigationGamepad(UserInputType.Gamepad1) then
print("Gamepad is a navigation gamepad!")
else
print("Gamepad is not a navigation gamepad!")
end

无论导航,所有连接的游戏手柄的列表都可以使用 `UserInput/GetConnectedGamepads 来检索。

由于 UserInputService 仅为客户端,此函数只能在 LocalScript 中使用。

还见:

参数

gamepadEnum: Enum.UserInputType

指定游戏手柄的 Enum.UserInputType

默认值:""

返回

是否指定的游戏手柄是导航游戏手柄。

RecenterUserHeadCFrame

()

此函数将 VR 头戴设备的 CFrame 重新定位到用户穿戴的头戴设备的当前方向。这意味着头戴式设备的当前方向已设置为 CFrame.new()

使用此函数将耳机 CFrame 移至播放区中央,如果它似乎处于奇怪的偏移。

这与 VRService 函数相同,VRService:RecenterUserHeadCFrame()

由于 UserInputService 仅为客户端,此函数只能在 LocalScript 中使用。


返回

()

SetNavigationGamepad

()

SetNavigationGamepad 函数设置是否需要指定的 Enum.UserInputType 游戏手柄移动 GUI 导航器。允许移动 GUI 导航器的游戏手柄被视为 导航游戏手柄

如果 启用 参数被传递为 true,游戏手柄可以移动 GUI 导航器。如果参数是 false,游戏手柄无法移动 GUI 导航器。

如果您想检查指定的游戏手柄是否设置为导航游戏手柄,您可以使用 UserInputService:IsNavigationGamepad() 函数。您还可以使用 UserInputService:GetNavigationGamepads() 来检索所有导航游戏板的列表。

由于 UserInputService 仅为客户端,此函数只能在 LocalScript 中使用。

还见:

参数

gamepadEnum: Enum.UserInputType

指定游戏手柄的 Enum.UserInputType

默认值:""
enabled: boolean

是否指定的游戏手柄可以移动 GUI 导航器。

默认值:""

返回

()

代码示例

这个例子将 Gamepad1 设置为导航游戏手柄,传递 Enum.UserInputType.Gamepad1true 作为参数。

用户输入服务:设置导航游戏板

local UserInputService = game:GetService("UserInputService")
UserInputService:SetNavigationGamepad(Enum.UserInputType.Gamepad1, true)

活动

DeviceAccelerationChanged

装置加速更改事件会在用户移动具有加速器的设备时发生。

加速器是一种在大多数移动设备中找到的组件,用于测量加速(速度变化)。

要确定用户的设备是否启用了加速器,请参阅UserInputService.AccelerometerEnabled

这个事件可以用来跟踪带有加速器的设备的移动。示例使用包括在移动设备加速时移动玩家角色。

此外,此事件可以与 UserInputService:GetDeviceAcceleration() 一起使用,以确定设备上的用户设备当前的移动情况,如果设备具有加速器。

该事件仅在本地发射 - 这意味着只有装置移动的玩家才能使用该事件,并且只能在 LocalScript 中工作。

参数

acceleration: InputObject

一个 InputObject ,包括 UserInputType ,以及 ** 显示每个本地设备轴上的重力力的 Position


代码示例

This example uses the accelerometer to move the player character when a mobile device is accelerated. The character will move along the axis that the device was moved.

Control Players Using the Accelerometer

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

当设备的重力 UserInputService.DeviceGravityChanged 在具有加速器的设备上发生变化时,事件触发Vector3

设备的重力向量代表每个设备的 X、Y 和 Z 轴上的重力力。虽然重力永远不会改变,但当设备旋转并更改方向时,其对每个轴的力会发生变化。对每个轴施加的力值是从 -1 到 1 的单位向量。

加速器是一种在大多数移动设备中找到的组件,用于测量加速(速度变化)。

该事件可用于确定用户设备上的重力力的实世方向。这甚至可以用于模拟游戏中用户设备上的重力力,例如游戏内对象(见下面的示例)。

要检查用户的设备是否启用了加速器,请参阅 UserInputService.AccelerometerEnabled 。如果设备启用了加速器,你可以使用 UserInputService:GetDeviceGravity() 函数来获取用户设备上的当前重力力。

参数

gravity: InputObject

一个 InputObject ,具有 InputObject.Position 属性,显示每个本地设备轴上的重力力。这个位置可以用作确定设备相对于重力方向的方向。


代码示例

This code adds a force on a part so that it falls in the direction of actual gravity relative to the user's device. In order for this example to work as expected, it must be placed in a LocalScript and the user's device must have an accelerometer.

Move a Ball using the Accelerometer

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

当用户旋转带有陀螺仪的设备时,设备旋转更改事件发生。

陀螺仪是一种在大多数移动设备中找到的组件,可检测方向和旋转速度。

事件对于跟踪设备的方向和用户旋转设备后的更改有用。要确定当前设备的旋转,您可以使用 UserInputService:GetDeviceRotation() 函数。

要检查用户的设备是否启用了陀螺仪,并且这个事件将发射,请参阅 UserInputService.GyroscopeEnabled

此事件仅在 Roblox 客户端窗口处于焦点时发生。例如,窗口被最小化时,输入不会被捕获。

参数

rotation: InputObject

一个 InputObject 提供关于设备旋转的信息。 代表新的旋转值, 代表在某个位置值中的旋转变化, 代表在某个位置值中的旋转变化。

cframe: CFrame

一个 CFrame 代表设备当前的方向。


代码示例

This code adds a force on a part so that it falls in the direction of actual gravity relative to the user's device. In order for this example to work as expected, it must be placed in a LocalScript and the user's device must have an accelerometer.

Move a Ball using the Accelerometer

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

GamepadConnected

游戏手柄连接事件会在游戏手柄连接到客户端时触发。

由于 Roblox 游戏支持多个控制器,这个事件在与 UserInputService.GamepadDisconnected 事件匹配时跟踪哪些控制器/游戏板是激活的时有用。您还可以使用 UserInputService:GetConnectedGamepads() 来找到正确的游戏手柄进行使用。

以下示例显示了游戏手柄连接到客户端时的跟踪使用示例。


local UserInputService = game:GetService("UserInputService")
local function GamepadConnected(gamepad)
print("Player has plugged controller: " .. tostring(gamepad))
end)
UserInputService.GamepadConnected:Connect(GamepadConnected)

如果您想查看哪些设备已连接,您可以使用 UserInputService:GetConnectedGamepads() 函数。

由于此事件是在本地发射的,它只能在 LocalScript 中使用。

还见:

参数

gamepadNum: Enum.UserInputType

连接的游戏手柄的 Enum.UserInputType


GamepadDisconnected

游戏手柄断开事件会在游戏手柄断开时发生。

由于 Roblox 游戏支持多个控制器,这个事件在与 UserInputService.GamepadConnected 事件匹配时跟踪哪些控制器/游戏板是激活的时有用。您还可以使用 UserInputService:GetConnectedGamepads() 来找到正确的游戏手柄进行使用。

以下示例显示了游戏手柄从客户端断开时的跟踪使用示例。


local UserInputService = game:GetService("UserInputService")
local function GamepadDisconnected(gamepad)
print("Player has unplugged controller: " .. tostring(gamepad))
end)
UserInputService.GamepadDisconnected:Connect(GamepadDisconnected)

由于此事件是在本地发射的,它只能在 LocalScript 中使用。

还见:

参数

gamepadNum: Enum.UserInputType

断开游戏手柄的 Enum.UserInputType


InputBegan

输入开始事件在用户开始通过人机界面设备(鼠标向下、触摸开始、键盘按钮向下等)进行交互时触发。

它可以用于跟踪用户互动的开始,例如当用户第一次与图形用户界面元素、游戏手柄等互动时。它不捕获鼠标轮移动。

这个事件可以与 UserInputService.InputChangedUserInputService.InputEnded 一起使用,来跟踪用户输入、更改和结束的时间。

此事件仅在 Roblox 客户端窗口处于焦点时发生。例如,窗口被最小化时,输入不会被捕获。

由于此事件仅在本地发射,它只能在 LocalScript 中使用。

参数

包含用户输入信息的 InputObject 实例。

gameProcessedEvent: boolean

指示游戏引擎是否内部观察到此输入并采取行动。一般来说,这指的是 UI 处理,因此如果从这个输入触摸或单击了按钮,那么 gameProcessedEvent 将是 true。这也适用于通过 ContextActionService 连接的输入事件。


InputChanged

当用户通过人机界面设备(鼠标按钮向下、触摸开始、键盘按钮向下等)更改他们的互动方式时,输入更改事件会触发(例如)。

要忽略 Roblox 自动处理的事件,例如滚动在 ScrollingFrame 中,检查 gameProcessedEvent 参数是否为 false。这个事件可以与 UserInputService.InputBeganUserInputService.InputEnded 一起使用,来跟踪用户输入、更改和结束的时间。

此事件仅在 Roblox 客户端窗口处于焦点时发生。例如,窗口被最小化时,输入不会被捕获。

由于此事件仅在本地发射,它只能在 LocalScript 中使用。

参数

包含用户输入信息的 InputObject 实例。

gameProcessedEvent: boolean

指示游戏引擎是否内部观察到此输入并采取行动。一般来说,这指的是 UI 处理,因此如果从这个输入触摸或单击了按钮,那么 gameProcessedEvent 将是 true。这也适用于通过 ContextActionService 连接的输入事件。


InputEnded

输入已结束事件会在用户停止通过人机界面设备(鼠标向下、触摸开始、键盘按钮向下等)进行交互时触发。当跟踪用户释放键盘键、鼠标按钮、触摸屏输入等时,这很有用

这个事件可以与 UserInputService.InputBeganUserInputService.InputChanged 一起使用,来跟踪用户输入、更改和结束的时间。

此事件仅在 Roblox 客户端窗口处于焦点时发生。例如,窗口被最小化时,输入不会被捕获。

由于此事件仅在本地发射,它只能在 LocalScript 中使用。

参数

一个 InputObject 实例,包含关于用户输入的信息。

gameProcessedEvent: boolean

指示游戏引擎是否内部观察到此输入并采取行动。一般来说,这指的是 UI 处理,因此如果从这个输入触摸或单击了按钮,那么 gameProcessedEvent 将是 true。这也适用于通过 ContextActionService 连接的输入事件。


JumpRequest

当客户端发出跳转请求时,UserInputService 跳转请求事件会发生,例如当客户端按下空格键或跳跃按钮时,在移动设备上。

该事件会在用户尝试将其 Player.Character 跳跃时触发。默认行为会回应跳跃请求,设置玩家的 Humanoid.Jump 属性为真值,使玩家的角色跳跃。

事件可用于跟踪玩家每次想跳跃的时间。而不是使用它来让玩家跳跃,这应该用于更改默认跳跃行为 - 例如禁用跳跃。

例如,下面的代码每次玩家发送跳跃请求时都会打印“跳跃”。


local UserInputService = game:GetService("UserInputService")
function onJumpRequest()
print("Jump!")
end
UserInputService.JumpRequest:Connect(onJumpRequest)

由于此事件为单次跳跃请求触发多次,建议使用 缓冲

如果您想将键或按钮连接到其他操作,请考虑使用事件,例如 UserInputService:GetKeysPressed()UserInputService.InputBeganContextActionService

由于此事件仅在本地发射,它只能在 LocalScript 中使用。


代码示例

这段代码示例禁用了跳转到 LocalPlayer 通过设置 Enum.HumanoidStateType.Jumping 状态为 false。将此状态设置为 false,一旦用户尝试跳过就会取消跳跃。

为了这个例子按期望的方式运行,它应该放置在 LocalScript 中。

禁用跳跃

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")
-- 当用户尝试跳跃时发生火灾
local function jump()
humanoid:SetStateEnabled(Enum.HumanoidStateType.Jumping, false)
end
UserInputService.JumpRequest:Connect(jump)

LastInputTypeChanged

客户端通过人机界面设备改变互动方式时,UserInputService.LastInputTypeChanged将随时发生。(i.e.从 MouseMovement 到 MouseWheel 或从 Thumbstick1 到 Thumbstick2)。

无论输入类型最后是否发生了变化,您都可以使用 UserInputService:GetLastInputType() 函数来获取最后一个输入类型的值。

由于此事件仅在本地发射,它只能在 LocalScript 中使用。

参数

lastInputType: Enum.UserInputType

一个 Enum.UserInputType 指示最后一个输入类型。


代码示例

This example hides the mouse icon while the player beings using their keyboard, such as to chat or enter text into a TextBox. The mouse icon reappears when the user resumes mouse input.

This uses the LastInputType() event to determine when the user begins keyboard input and mouse input - based on the value of the lastInputType argument.

In order for this example to work as expected, it should be placed in a LocalScript.

Hide Mouse During Keyboard Input

local UserInputService = game:GetService("UserInputService")
local mouseInput = {
Enum.UserInputType.MouseButton1,
Enum.UserInputType.MouseButton2,
Enum.UserInputType.MouseButton3,
Enum.UserInputType.MouseMovement,
Enum.UserInputType.MouseWheel,
}
local keyboard = Enum.UserInputType.Keyboard
local function toggleMouse(lastInputType)
if lastInputType == keyboard then
UserInputService.MouseIconEnabled = false
return
end
for _, mouse in pairs(mouseInput) do
if lastInputType == mouse then
UserInputService.MouseIconEnabled = true
return
end
end
end
UserInputService.LastInputTypeChanged:Connect(toggleMouse)

PointerAction

指针操作 在用户执行特定的指针操作时发生。例如滚动鼠标轮。

参数

wheel: number
pan: Vector2
pinch: number
gameProcessedEvent: boolean

TextBoxFocusReleased

当客户端失去对 的焦点时,事件会触发,通常是当客户端按下返回键或单击/触摸屏幕上的其他地方停止输入文本时。

例如,下面的代码打印了事件触发时丢失焦点的名称 TextBox


local UserInputService = game:GetService("UserInputService")
function TextBoxFocusReleased(textbox)
print(textbox.Name)
end
UserInputService.TextBoxFocusReleased:Connect(TextBoxFocusReleased)

它可以与 UserInputService.TextBoxFocused 一起使用,以跟踪当一个 TextBox 获得和失去焦点时。

由于此事件仅在本地发射,它只能在 LocalScript 中使用。

还看到也看到

参数

textboxReleased: TextBox

那个 TextBox 失去了焦点。


TextBoxFocused

当一个获得焦点在 TextBox 上时,此事件会触发,通常是当客户单击/点击文本框以开始输入文本时。这也会在使用 TextBox:CaptureFocus() 将文本框焦点聚焦时发射。

例如,下面的代码打印了事件触发时聚焦的 TextBox 名称。


local UserInputService = game:GetService("UserInputService")
function TextBoxFocused(textbox)
print(textbox.Name)
end)
UserInputService.TextBoxFocused:Connect(TextBoxFocused)

它可以与 UserInputService.FocusReleased 一起使用,追踪文本框是否获得或失去焦点。

由于此事件仅在本地发射,它只能在 LocalScript 中使用。

还看到也看到

参数

textboxFocused: TextBox

获得焦点的 TextBox


TouchDrag

参数

dragDirection: Enum.SwipeDirection
numberOfTouches: number
gameProcessedEvent: boolean

TouchEnded

当用户从触摸启用的设备的屏幕上释放手指时,触摸结束事件发生,结束对设备的触摸输入。

该事件可用于确定用户何时停止触摸其设备的屏幕。它可以与 UserInputService.TouchStarted 匹配,以确定用户何时开始和停止触摸屏幕。

例如,下面的代码打印了用户停止触摸屏幕的屏幕位置。


local UserInputService = game:GetService("UserInputService")
function TouchEnded(touch, gameProcessedEvent)
print("Touch ended at " .. tostring(touch.Position))
end
UserInputService.TouchEnded:Connect(TouchEnded)

触摸输入对象在触摸期间始终是相同的输入对象。因此,当触摸对象时比较 InputObjects 是有效的,以确定它是否是同一只手指。

要检查用户的设备是否启用触摸,并且触摸事件会发射,请参阅 UserInputService.TouchEnabled

此事件仅在 Roblox 客户端窗口处于焦点时发生。例如,窗口被最小化时,输入不会被捕获。

由于此事件仅在本地发射,它只能在 LocalScript 中使用。

还见:

参数

包含用户输入信息的 InputObject 实例。

gameProcessedEvent: boolean

指示游戏引擎是否内部观察到此输入并采取行动。一般来说,这指的是 UI 处理,因此如果从这个输入触摸或单击了按钮,那么 gameProcessedEvent 将是 true。这也适用于通过 ContextActionService 连接的输入事件。


TouchLongPress

当用户在触摸启用的设备的相同屏幕位置上按住至少一个手指一小段时间时,发射。

这个事件可以用来确定用户何时按住手指在游戏中GuiObject或元素上。

下面的例子打印用户在同一屏幕位置按住至少一个手指的短时间后的长按state。可能的状态包括:开始更改结束取消


local UserInputService = game:GetService("UserInputService")
function TouchLongPress(TouchPositions, state, gameProcessedEvent)
print("Long press event fired. State of press: " .. tostring(state))
end
UserInputService.TouchLongPress:Connect(TouchLongPress)

要检查用户的设备是否启用触摸,以及触摸事件是否会触发,请参阅UserInputService.TouchEnabled

它可以与 UserInputService.TouchStartedUserInputService.TouchEnded 匹配,以确定用户何时开始和停止触摸屏幕。

此事件仅在 Roblox 客户端窗口处于焦点时发生。例如,窗口被最小化时,输入不会被捕获。

由于此事件仅在本地发射,它只能在 LocalScript 中使用。

还见:

参数

touchPositions: Array

一个由 Vector2 个对象组成的阵列,表示参与手势的手指位置。

gameProcessedEvent: boolean

指示游戏引擎是否内部观察到此输入并采取行动。一般来说,这指的是 UI 处理,因此如果从这个输入触摸或单击了按钮,那么 gameProcessedEvent 将是 true。这也适用于通过 ContextActionService 连接的输入事件。


TouchMoved

当用户在 TouchEnabled 设备上移动手指时发生火焰,例如平板电脑或智能手机。

这个事件对于跟踪用户是否在屏幕上移动手指以及用户移动手指的位置有用。

下面的代码显示了触摸从其前一个位置移动到 TouchEnabled 设备上的新位置。请注意,传递的 InputObject.Position 参数上的 touch 是一个 Vector3 ,但仅包含 X 和 Y 坐标;Z 总是 0。


local UserInputService = game:GetService("UserInputService")
function onTouchMoved(touch, gameProcessedEvent)
local oldPosition = touch.Position - touch.Delta
print("Touch moved from " .. tostring(oldPosition) .. " to " .. tostring(touch.Position))
end
UserInputService.TouchMoved:Connect(onTouchMoved)

将此事件与 UserInputService.TouchStartedUserInputService.TouchEnded 匹配,以确定用户何时开始触摸屏幕、移动手指时触摸屏幕以及何时停止触摸屏幕。

要检查用户的设备是否支持触摸以及触摸事件是否会触发,请参阅 UserInputService.TouchEnabled

此事件仅在 Roblox 客户端窗口处于焦点时发生。例如,窗口被最小化时,输入不会被捕获。

由于此事件仅在本地发射,它只能在 LocalScript 中使用。

还见:

参数

包含用户输入信息的 InputObject 实例。

gameProcessedEvent: boolean

指示游戏引擎是否内部观察到此输入并采取行动。一般来说,这指的是 UI 处理,因此如果从这个输入触摸或单击了按钮,那么 gameProcessedEvent 将是 true。这也适用于通过 ContextActionService 连接的输入事件。


TouchPan

触摸板事件在用户拖动至少一个手指到 TouchEnabled 设备时触发。

此事件可用于确定用户何时在触摸启用的设备的屏幕上移动手指 - 例如,在自定义相机脚本中旋转 Camera

下面的代码打印“触摸拖动速度”,随后是用户拖动手指时屏幕上的速度。


local UserInputService = game:GetService("UserInputService")
UserInputService.TouchPan:Connect(function(touchPositions, totalTranslation, velocity, state, gameProcessedEvent)
print("Speed of touch drag: " .. tostring(velocity))
end)

看看另一个有用的 UserInputService 函数在这里 UserInputService.TouchRotate .

此事件仅在 Roblox 客户端窗口处于焦点时发生。例如,窗口被最小化时,输入不会被捕获。

由于此事件仅在本地发射,它只能在 LocalScript 中使用。

还见:

参数

touchPositions: Array

一个由 Vector2 个对象组成的阵列,表示参与手势的触摸位置(例如手指)。

totalTranslation: Vector2

从开始到结束的平底锅动作的尺寸(以像素计)。

velocity: Vector2

平底锅的手势速度(以像素计)每秒钟。

gameProcessedEvent: boolean

指示游戏引擎是否内部观察到此输入并采取行动。一般来说,这指的是 UI 处理,因此如果从这个输入触摸或单击了按钮,那么 gameProcessedEvent 将是 true。这也适用于通过 ContextActionService 连接的输入事件。


TouchPinch

当用户放置并移动两个手指到 TouchEnabled 设备的屏幕时,发射。

例如,下面的代码打印了自从触摸捏动开始以来相机缩放比例的变化量。


local UserInputService = game:GetService("UserInputService")
UserInputService.TouchPinch:Connect(function(touchPositions, scale, velocity, state, gameProcessedEvent)
print("Scale difference since beginning of pinch: " .. tostring(scale))
end)

要检查用户的设备是否启用触摸,并且触摸事件会发射,请参阅 UserInputService.TouchEnabled

此事件仅在 Roblox 客户端窗口处于焦点时发生。例如,窗口最小化时,输入不会被捕获。由于此事件仅在本地发射,它只能在 LocalScript 中使用。

还见:

参数

touchPositions: Array

一个 Vector2s 阵列,表示涉及握住手势的手指的屏幕位置,以像素为单位。

scale: number

从开始到完成的握住力(以像素计)除以开始握住位置。

velocity: number

按钮手势的速度(以像素计)每秒钟。

gameProcessedEvent: boolean

指示游戏引擎是否内部观察到此输入并采取行动。一般来说,这指的是 UI 处理,因此如果从这个输入触摸或单击了按钮,那么 gameProcessedEvent 将是 true。这也适用于通过 ContextActionService 连接的输入事件。


TouchRotate

触摸旋转事件会在用户在 TouchEnabled 设备上旋转两个手指时触发。

例如,以下代码打印了相机自触摸旋转开始以来旋转了多少。


local UserInputService = game:GetService("UserInputService")
UserInputService.TouchRotate:Connect(function(touchPositions, rotation, velocity, state, gameProcessedEvent)
print("Camera has rotated " .. tostring(rotation) .. " degrees!")
end)

要检查用户的设备是否启用触摸,并且触摸事件会发射,请参阅 UserInputService.TouchEnabled

此事件仅在 Roblox 客户端窗口处于焦点时发生。例如,窗口被最小化时,输入不会被捕获。

由于此事件仅在本地发射,它只能在 LocalScript 中使用。

在移动设备上控制用户的相机的核心脚本使用与此事件相似的功能代码。对于此事件,最佳实践是在创建移动相机系统时使用它来覆盖默认核心脚本。

还见:

参数

touchPositions: Array

一个 Vector2s 阵列,表示参与手势的手指位置。

rotation: number

该手势旋转以来的度数。

velocity: number

旋转变化(以度计)除以更改持续时间(以秒计)。

gameProcessedEvent: boolean

指示游戏引擎是否内部观察到此输入并采取行动。一般来说,这指的是 UI 处理,因此如果从这个输入触摸或单击了按钮,那么 gameProcessedEvent 将是 true。这也适用于通过 ContextActionService 连接的输入事件。


TouchStarted

当用户将手指放在 TouchEnabled 设备上时,触摸开始事件发生,开始使用设备进行触摸输入

这个事件可以用来确定用户何时开始触摸他们设备的屏幕。它可以与 UserInputService.TouchEnded 匹配,以确定用户何时开始和停止触摸屏幕。

触摸输入对象在触摸期间始终是相同的输入对象。因此,当触摸对象时比较 InputObjects 是有效的,以确定它是否是同一只手指。

要检查用户的设备是否启用触摸,并且触摸事件会发射,请参阅 UserInputService.TouchEnabled

此事件仅在 Roblox 客户端窗口处于焦点时发生。例如,窗口被最小化时,输入不会被捕获。

由于此事件仅在本地发射,它只能在 LocalScript 中使用。

还见:

参数

包含用户输入信息的 InputObject 实例。

gameProcessedEvent: boolean

指示游戏引擎是否内部观察到此输入并采取行动。一般来说,这指的是 UI 处理,因此如果从这个输入触摸或单击了按钮,那么 gameProcessedEvent 将是 true。这也适用于通过 ContextActionService 连接的输入事件。


TouchSwipe

当用户在设备上滑动手指时,TouchSwipe会发生,当用户滑动手指时,TouchEnabled会发生。

该事件可用于确定用户在设备屏幕上滑动手指的时间和方向,以及用户滑动的方向。

要更精确地跟踪触摸输入移动,请使用 UserInputService.TouchMoved

要检查用户的设备是否启用触摸,并且触摸事件会发射,请参阅 UserInputService.TouchEnabled

此事件仅在 Roblox 客户端窗口处于焦点时发生。例如,窗口被最小化时,输入不会被捕获。

由于此事件仅在本地发射,它只能在 LocalScript 中使用。

还见:

参数

swipeDirection: Enum.SwipeDirection

一个 Enum.SwipeDirection , 指示用户滑动的方向。

numberOfTouches: number

参与手势的触摸数(例如手指)。

gameProcessedEvent: boolean

指示游戏引擎是否内部观察到此输入并采取行动。一般来说,这指的是 UI 处理,因此如果从这个输入触摸或单击了按钮,那么 gameProcessedEvent 将是 true。这也适用于通过 ContextActionService 连接的输入事件。


TouchTap

触摸事件在 TouchEnabled 设备上的屏幕上触摸/点击用户的手指时触发。

无论用户是否触摸/点击游戏世界或 GuiObject 元素,此事件都会发射。如果您正在寻找仅在用户触摸/点击游戏世界时发射的事件,请使用 UserInputService.TouchTapInWorld

要检查用户的设备是否启用触摸,并且触摸事件会发射,请参阅 UserInputService.TouchEnabled

此事件仅在 Roblox 客户端窗口处于焦点时发生。例如,窗口被最小化时,输入不会被捕获。

由于此事件仅在本地发射,它只能在 LocalScript 中使用。

参数

touchPositions: Array

一个由 Vector2 个对象组成的阵列,表示敲击手势中涉及的手指位置。

gameProcessedEvent: boolean

指示游戏引擎是否内部观察到此输入并采取行动。一般来说,这指的是 UI 处理,因此如果从这个输入触摸或单击了按钮,那么 gameProcessedEvent 将是 true。这也适用于通过 ContextActionService 连接的输入事件。


TouchTapInWorld

触摸世界事件在用户在 TouchEnabled 设备上触摸/点击屏幕时触发。当用户点击游戏世界时,它会被发射。

这个事件可以用来确定用户点击屏幕时是否没有点击GuiObject元素。如果用户点击 GUI 元素,UserInputService.TouchTap 会在 TouchTapInWorld 之前发射。

要检查用户的设备是否启用触摸,并且触摸事件会发射,请参阅 UserInputService.TouchEnabled

此事件仅在 Roblox 客户端窗口处于焦点时发生。例如,窗口被最小化时,输入不会被捕获。

由于它只能在本地发射,它只能在 LocalScript 中使用。

还见:

参数

position: Vector2

一个 Vector2 指示触摸位置的。

processedByUI: boolean

用户是否点击了图形用户界面元素。


WindowFocusReleased

当 Roblox 客户端窗口失去焦点时,UserInputService 窗口焦点释放事件发生 - 通常是当 Roblox 客户端被用户最小化时。

例如,下面的代码每当 Roblox 客户端失去焦点时打印 “窗口焦点释放”


local UserInputService = game:GetService("UserInputService")
UserInputService.WindowFocusReleased:Connect(function()
print("Window focus released")
end)

此事件可以与 UserInputService.WindowFocused 一起使用,以跟踪 Roblox 客户端是否积极关注用户的屏幕。

由于它只能在本地发射,它只能在 LocalScript 中使用。


WindowFocused

当 Roblox 客户端窗口获得焦点时,UserInputService会发生,通常是当 Roblox 客户端最大化/在用户屏幕上打开时。

例如,下面的代码每当 Roblox 客户端获得焦点时打印 “窗口聚焦”


local UserInputService = game:GetService("UserInputService")
UserInputService.WindowFocused:Connect(function()
print("Window focused")
end)

此事件可以与 UserInputService.WindowFocusReleased 一起使用,以跟踪 Roblox 客户端是否积极关注用户的屏幕。

由于此事件仅在本地发射,它只能在 LocalScript 中使用。