Skip to content

UserInputService ​

概览 ​

字段值
KindService · 服务 Service
Realmclient

UserInputService 是客户端输入服务,统一提供键盘、鼠标、触摸、摇杆以及蛋仔专属按钮的输入查询与事件派发能力。它既能通过 IsKeyDown、GetMouseLocation、GetMouseDelta、GetJoystickMoveVector 等函数做即时状态查询,也能派发 InputBegan、InputChanged、InputEnded、TouchStarted、JoystickMove 等事件,并支持屏幕坐标与界面坐标互转、鼠标行为与鼠标图标开关、触摸模拟开关等控制项。

适用场景 ​

适合在客户端脚本中处理玩家的按键、点击、触摸和摇杆操作,例如监听 ClickEggyJump 响应蛋仔跳跃按钮,或在 InputBegan 中读取 inputObject.KeyCode 判断具体按键。

使用要点 ​

先用 game:GetService("UserInputService") 取得服务实例,再按需连接 InputBegan、InputChanged、InputEnded 或 ClickEggyJump、JoystickMove 等事件,在回调中通过 inputObject.KeyCode 与 gameProcessedEvent 判断输入内容与是否已被界面消费;需要即时状态时可调用 IsKeyDown、GetMouseLocation、GetJoystickMoveLength,坐标换算使用 ConvertScreenToUIPosition 与 ConvertUIToScreenPosition,鼠标表现调整使用 SetMouseBehavior、SetMouseIconEnabled、SetMouseDeltaSensitivity。

注意事项 ​

UserInputService 的输入事件以当前客户端输入系统实际派发为准。蛋仔专属按钮优先监听 ClickEggyJump/ClickEggyLift/ClickEggyRoll/ClickEggyRush;通用键盘跳跃可通过 InputBegan + Enums.KeyCode.Space 处理。JumpRequest 在当前包中不保证触发,不应作为唯一跳跃入口。

代码示例 ​

过滤已由 UI 处理的键盘输入 ​

lua
-- @runtime client
local UserInputService = game:GetService('UserInputService')
UserInputService.InputBegan:Connect(function(input, gameProcessedEvent)
    if gameProcessedEvent then return end
    if input.KeyCode == Enums.KeyCode.Space then
        print('收到未被 UI 消费的空格输入')
    end
end)

属性 (1) ​

Name类型默认值说明
PreferredInputPreferredInputEnums.PreferredInput.KeyboardAndMouse只读属性,用于表示当前首选输入设备的类型,取值为 Enums.PreferredInput 枚举项,例如 KeyboardAndMouse 或 Touch。

关联类型 ​

事件 (17) ​

InputBegan ​

签名:InputBegan(inputObject: InputObject, gameProcessedEvent: Bool) 触发:local

当用户开始一次输入时触发的事件,例如按下键盘按键、按下鼠标按键或开始触摸屏幕。回调接收两个参数:inputObject 为描述本次输入的 InputObject,gameProcessedEvent 为布尔值,表示该输入是否已被系统或界面处理。

回调参数类型说明
inputObjectInputObject输入对象
gameProcessedEventBool游戏是否已处理该事件

示例代码

监听未被 UI 处理的按键开始

lua
-- @runtime client
local inputService = game:GetService('UserInputService')
inputService.InputBegan:Once(function(input, gameProcessedEvent)
    if gameProcessedEvent then return end
    if input.KeyCode == Enums.KeyCode.Space then print('空格键按下') end
end)

InputChanged ​

签名:InputChanged(inputObject: InputObject, gameProcessedEvent: Bool) 触发:local

输入状态变化时触发;回调依次收到 InputObject 和输入是否已被游戏处理的 Bool。

回调参数类型说明
inputObjectInputObject输入对象
gameProcessedEventBool游戏是否已处理该事件

示例代码

监听鼠标移动

lua
-- @runtime client
local inputService = game:GetService('UserInputService')
inputService.InputChanged:Once(function(input, gameProcessedEvent)
    if gameProcessedEvent then return end
    if input.UserInputType == Enums.UserInputType.MouseMovement then print('鼠标位置:', input.Position) end
end)

InputEnded ​

签名:InputEnded(inputObject: InputObject, gameProcessedEvent: Bool) 触发:local

当一次输入结束时触发的事件,例如松开键盘按键、释放鼠标按键或触摸结束。回调接收 inputObject 与 gameProcessedEvent 两个参数。

回调参数类型说明
inputObjectInputObject输入对象
gameProcessedEventBool游戏是否已处理该事件

示例代码

监听按键结束

lua
-- @runtime client
local inputService = game:GetService('UserInputService')
inputService.InputEnded:Once(function(input, gameProcessedEvent)
    if gameProcessedEvent then return end
    if input.KeyCode == Enums.KeyCode.Space then print('空格键松开') end
end)

LastInputTypeChanged ​

签名:LastInputTypeChanged(lastInputType: UserInputType) 触发:local

当最后一次输入类型发生变化时触发,例如从键盘切换到触摸。

回调参数类型说明
lastInputTypeUserInputType最后的输入类型

示例代码

监听最后输入类型变化

lua
-- @runtime client
-- 获取 UserInputService 服务
local userInputService = game:GetService("UserInputService")

-- 监听 LastInputTypeChanged 事件,当最后输入类型改变时触发
userInputService.LastInputTypeChanged:Once(function(lastInputType)
    -- 打印新的最后输入类型
    print("最后输入类型变为:", lastInputType)
    -- 根据输入类型切换 UI 提示
    if lastInputType == Enums.UserInputType.Keyboard then
        print("当前使用键盘输入")
    elseif lastInputType == Enums.UserInputType.Touch then
        print("当前使用触摸输入")
    end
end)

TouchStarted ​

签名:TouchStarted(touch: InputObject, gameProcessedEvent: Bool) 触发:local

当触摸输入开始时触发的事件,touch 参数为描述本次触摸的 InputObject,gameProcessedEvent 为布尔值,表示该输入是否已被系统或界面处理。

回调参数类型说明
touchInputObject触摸输入对象
gameProcessedEventBool游戏是否已处理该事件

示例代码

监听触摸开始

lua
-- @runtime client
local inputService = game:GetService('UserInputService')
inputService.TouchStarted:Once(function(touch, gameProcessedEvent)
    if gameProcessedEvent then return end
    print('触摸开始位置:', touch.Position)
end)

TouchMoved ​

签名:TouchMoved(touch: InputObject, gameProcessedEvent: Bool) 触发:local

当触摸点在屏幕上移动时触发的事件,touch 参数为当前触摸对应的 InputObject,gameProcessedEvent 为布尔值,表示该输入是否已被系统或界面处理。

回调参数类型说明
touchInputObject触摸输入对象
gameProcessedEventBool游戏是否已处理该事件

示例代码

监听触摸移动

lua
-- @runtime client
local inputService = game:GetService('UserInputService')
inputService.TouchMoved:Once(function(touch, gameProcessedEvent)
    if gameProcessedEvent then return end
    print('触摸移动:', touch.Delta, touch.Position)
end)

TouchEnded ​

签名:TouchEnded(touch: InputObject, gameProcessedEvent: Bool) 触发:local

当触摸结束时触发的事件,touch 参数为本次结束触摸对应的 InputObject,gameProcessedEvent 为布尔值,表示该输入是否已被系统或界面处理。

回调参数类型说明
touchInputObject触摸输入对象
gameProcessedEventBool游戏是否已处理该事件

示例代码

监听触摸结束

lua
-- @runtime client
local inputService = game:GetService('UserInputService')
inputService.TouchEnded:Once(function(touch, gameProcessedEvent)
    if gameProcessedEvent then return end
    print('触摸结束位置:', touch.Position)
end)

TouchTap ​

签名:TouchTap(touchPositions: Table<Vector2>, gameProcessedEvent: Bool) 触发:local

UserInputService 在客户端触发的触摸点击事件,当触屏设备上识别到轻触(点按)手势时触发。回调提供参与手势的触点位置表 touchPositions,以及标记该输入是否已被引擎 UI 消费的 gameProcessedEvent。

回调参数类型说明
touchPositionsTable<Vector2>参与手势的所有手指屏幕坐标(Tap 永远是 1 个)
gameProcessedEventBool游戏是否已处理该事件

示例代码

监听触摸轻点事件并处理触点数据

lua
-- @runtime client
-- 获取输入服务实例(客户端本地服务)
local userInputService = game:GetService("UserInputService")
-- 先读取当前设备是否支持触摸输入,用于判断 TouchTap 是否会派发
local touchEnabled = userInputService:TouchEnabled()
print("当前设备支持触摸输入:", touchEnabled)
-- 监听触摸轻点事件,touchPositions 保存本次轻点涉及的触点坐标
userInputService.TouchTap:Once(function(touchPositions, gameProcessedEvent)
    -- gameProcessedEvent 为 true 表示该输入已被 UI 消费,直接跳过避免重复响应
    if gameProcessedEvent then
        return
    end
    -- 消费回调参数:输出本次轻点包含的触点数量
    print("本次轻点触点数量:", #touchPositions)
end)

TouchPinch ​

签名:TouchPinch(touchPositions: Table&lt;Vector2&gt;, scale: Float, velocity: Float, state: UserInputState, gameProcessedEvent: Bool) 触发:local

UserInputService 在客户端触发的触摸手势事件,当触屏设备上识别到双指缩放(捏合)操作时触发。事件回调依次提供参与手势的触点位置表 touchPositions、本次手势的缩放系数 scale、缩放速度 velocity、当前输入状态 state,以及标记该输入是否已被引擎 UI 消费的 gameProcessedEvent。

回调参数类型说明
touchPositionsTable<Vector2>两根手指当前屏幕坐标
scaleFloat当前距离/初始距离,1.0=未变化
velocityFloatscale 的瞬时变化率(per 秒)
stateUserInputState手势状态:Begin/Change/End/Cancel
gameProcessedEventBool游戏是否已处理该事件

示例代码

监听双指捏合手势 TouchPinch

lua
-- @runtime client
-- 获取输入服务实例(必须通过服务注册名获取)
local userInputService = game:GetService("UserInputService")
-- 注册双指捏合手势的单次监听,常用于缩放地图或界面
userInputService.TouchPinch:Once(function(touchPositions, scale, velocity, state, gameProcessedEvent)
    -- 回调参数在回调体内直接消费,避免作用域外引用
    print("两指位置列表:", touchPositions)
    print("捏合缩放比例:", scale)
    print("捏合速度:", velocity)
    print("输入状态:", state, "是否已被引擎消费:", gameProcessedEvent)
end)
print("TouchPinch 监听已注册,请用双指捏合屏幕触发")

TouchRotate ​

签名:TouchRotate(touchPositions: Table&lt;Vector2&gt;, rotation: Float, velocity: Float, state: UserInputState, gameProcessedEvent: Bool) 触发:local

UserInputService 在客户端触发的触摸手势事件,当触屏设备上识别到双指旋转操作时触发。回调依次提供参与手势的触点位置表 touchPositions、旋转量 rotation、旋转速度 velocity、当前输入状态 state,以及标记该输入是否已被引擎 UI 消费的 gameProcessedEvent。

回调参数类型说明
touchPositionsTable<Vector2>两根手指当前屏幕坐标
rotationFloat相对初始角度的累计弧度(带符号,逆时针正)
velocityFloatrotation 的瞬时变化率(rad/秒)
stateUserInputState手势状态:Begin/Change/End/Cancel
gameProcessedEventBool游戏是否已处理该事件

示例代码

监听双指旋转手势并读取旋转参数

lua
-- @runtime client
-- 获取输入服务实例
local userInputService = game:GetService("UserInputService")
-- 监听双指旋转手势,回调参数依次为触点位置表、旋转角度、旋转速度、输入状态、是否已被 UI 消费
userInputService.TouchRotate:Once(function(touchPositions, rotation, velocity, state, gameProcessedEvent)
    -- gameProcessedEvent 为 true 说明输入已被界面控件消费,游戏逻辑应跳过
    if gameProcessedEvent then
        print("旋转手势已被 UI 消费,忽略本次输入")
        return
    end
    -- 在当前回调作用域内直接消费回调参数:旋转角度可用于相机环绕,速度可用于惯性衰减
    print("双指旋转角度:", rotation)
    print("双指旋转速度:", velocity)
    print("当前输入状态:", state)
    print("参与旋转的触点数:", #touchPositions)
end)

JoystickStart ​

签名:JoystickStart() 触发:local

当虚拟摇杆操作开始时触发。

示例代码

监听摇杆开始事件

lua
-- @runtime client
-- 获取 UserInputService 服务实例
local userInputService = game:GetService("UserInputService")

-- 监听摇杆开始事件
userInputService.JoystickStart:Once(function()
    print("摇杆操作开始")
end)

JoystickMove ​

签名:JoystickMove(x: Float, y: Float, length: Float) 触发:local

当虚拟摇杆移动时触发,提供摇杆的 X、Y 偏移和长度。

回调参数类型说明
xFloatx轴值
yFloaty轴值
lengthFloat摇杆偏移长度

示例代码

监听摇杆移动事件

lua
-- @runtime client
-- 获取 UserInputService 服务
local userInputService = game:GetService("UserInputService")

-- 监听 JoystickMove 事件,当摇杆移动时触发
userInputService.JoystickMove:Once(function(x, y, length)
    -- 打印摇杆移动数据
    print("摇杆移动 - X:", x, "Y:", y, "长度:", length)
    -- 根据摇杆方向控制角色移动(示例)
    if length > 0.1 then
        print("摇杆方向:", x, y)
    end
end)

JoystickEnd ​

签名:JoystickEnd() 触发:local

当虚拟摇杆操作结束时触发。

示例代码

监听摇杆结束事件

lua
-- @runtime client
-- 获取 UserInputService 服务实例
local userInputService = game:GetService("UserInputService")

-- 监听摇杆结束事件
userInputService.JoystickEnd:Once(function()
    print("摇杆操作结束")
end)

ClickEggyJump ​

签名:ClickEggyJump() 触发:local

当玩家点击蛋仔跳跃按钮时触发。

示例代码

监听蛋仔跳跃按钮点击

lua
-- @runtime client
-- 获取 UserInputService 服务实例
local userInputService = game:GetService("UserInputService")

-- 监听蛋仔跳跃按钮点击事件
userInputService.ClickEggyJump:Once(function()
    print("蛋仔跳跃按钮被点击")
end)

ClickEggyRush ​

签名:ClickEggyRush() 触发:local

当玩家点击蛋仔冲刺按钮时触发。

示例代码

监听蛋仔冲刺按钮点击

lua
-- @runtime client
-- 获取 UserInputService 服务实例
local userInputService = game:GetService("UserInputService")

-- 监听蛋仔冲刺按钮点击事件
userInputService.ClickEggyRush:Once(function()
    print("蛋仔冲刺按钮被点击")
end)

ClickEggyRoll ​

签名:ClickEggyRoll() 触发:local

当玩家点击蛋仔滚动按钮时触发。

示例代码

监听蛋仔滚动按钮点击

lua
-- @runtime client
-- 获取 UserInputService 服务实例
local userInputService = game:GetService("UserInputService")

-- 监听蛋仔滚动按钮点击事件
userInputService.ClickEggyRoll:Once(function()
    print("蛋仔滚动按钮被点击")
end)

ClickEggyLift ​

签名:ClickEggyLift() 触发:local

当玩家点击蛋仔举起按钮时触发。

示例代码

监听蛋仔举起按钮点击

lua
-- @runtime client
-- 获取 UserInputService 服务实例
local userInputService = game:GetService("UserInputService")

-- 监听蛋仔举起按钮点击事件
userInputService.ClickEggyLift:Once(function()
    print("蛋仔举起按钮被点击")
end)

函数 (25) ​

IsKeyDown ​

签名:IsKeyDown(keyCode: KeyCode) -> Bool (是否正在被按下)

检查指定按键当前是否处于按下状态,参数为 Enums.KeyCode 中的按键项,返回 Bool。

参数类型说明
keyCodeKeyCode键盘按键代码

返回值 Bool (是否正在被按下)

示例代码

查询键盘按键是否正在按下

lua
-- @runtime client
-- 获取当前客户端的输入服务实例
local userInputService = game:GetService("UserInputService")
-- 查询空格键此刻是否处于按下状态
local isSpaceDown = userInputService:IsKeyDown(Enums.KeyCode.Space)
print("空格键是否按下:", isSpaceDown)
-- 同时查询前进键,用于判断移动意图
local isForwardDown = userInputService:IsKeyDown(Enums.KeyCode.W)
print("W 键是否按下:", isForwardDown)
-- 根据查询结果组合出行为判断
if isSpaceDown and isForwardDown then
    print("玩家正在边前进边按住跳跃键")
end

IsMouseButtonPressed ​

签名:IsMouseButtonPressed(userInputType: UserInputType) -> Bool (是否正在被按下)

检查某个鼠标按钮是否正在被按下。

参数类型说明
userInputTypeUserInputType用户输入类型

返回值 Bool (是否正在被按下)

示例代码

检查鼠标按键是否按下

lua
-- @runtime client
-- 获取 UserInputService 服务
local userInputService = game:GetService("UserInputService")

-- 检查鼠标左键是否正在被按下
local isLeftPressed = userInputService:IsMouseButtonPressed(Enums.UserInputType.MouseButton1)
print("鼠标左键是否按下:", isLeftPressed)
-- 检查鼠标右键是否正在被按下
local isRightPressed = userInputService:IsMouseButtonPressed(Enums.UserInputType.MouseButton2)
print("鼠标右键是否按下:", isRightPressed)

GetKeysPressed ​

签名:GetKeysPressed() -> Array&lt;InputObject&gt; (按下的键对应的 InputObject 数组(顺序无保证))

返回当前正在被按下的所有键盘按键对应的 InputObject 数组,数组顺序无保证。

返回值 Array<InputObject> (按下的键对应的 InputObject 数组(顺序无保证))

示例代码

获取当前正在按下的所有键盘按键

lua
-- @runtime client
local userInputService = game:GetService("UserInputService")
-- 获取当前所有按下按键对应的 InputObject 数组,顺序无保证
local keysPressed = userInputService:GetKeysPressed()
print("当前按下的按键数量:", #keysPressed)
for _, inputObject in ipairs(keysPressed) do
    -- 遍历结果即可拿到每个按键对应的 InputObject
    if inputObject.KeyCode == Enums.KeyCode.Space then
        print("空格键当前处于按下状态")
    end
end

GetMouseButtonsPressed ​

签名:GetMouseButtonsPressed() -> Array&lt;InputObject&gt; (按下的鼠标按键对应的 InputObject 数组(顺序无保证))

返回当前正在被按下的所有鼠标按键对应的 InputObject 数组,数组顺序无保证。

返回值 Array<InputObject> (按下的鼠标按键对应的 InputObject 数组(顺序无保证))

示例代码

获取当前正在按下的所有鼠标按键

lua
-- @runtime client
local userInputService = game:GetService("UserInputService")
-- 获取当前所有被按下的鼠标按键对应的 InputObject 数组
local buttonsPressed = userInputService:GetMouseButtonsPressed()
print("当前按下的鼠标按键数量:", #buttonsPressed)
for _, inputObject in ipairs(buttonsPressed) do
    -- 用输入类型判断具体是哪个鼠标键
    if inputObject.UserInputType == Enums.UserInputType.MouseButton1 then
        print("鼠标左键处于按下状态")
    end
end

GetMouseLocation ​

签名:GetMouseLocation() -> Vector2 (鼠标位置)

获取鼠标位置,返回 Vector2 类型的结果。返回坐标与 ConvertUIToScreenPosition 的输出属于同一套屏幕(设备)坐标,即左上原点、真实像素。该函数不需要传入任何参数。

返回值 Vector2 (鼠标位置)

示例代码

获取鼠标当前位置

lua
-- @runtime client
-- 获取用户输入服务(全局单例)
local userInputService = game:GetService("UserInputService")

-- 鼠标位置为屏幕(设备)坐标:左上原点、真实像素
local mousePos = userInputService:GetMouseLocation()
print("鼠标屏幕坐标:", mousePos)
print("位置分量 x =", mousePos.x, ", y =", mousePos.y)
-- 需要与界面元素对齐时,可继续换算到 UI 坐标系
local uiPos = userInputService:ConvertScreenToUIPosition(mousePos)
print("同一位置的 UI 坐标:", uiPos)

GetMouseDelta ​

签名:GetMouseDelta() -> Vector2 (当前帧鼠标位移;未锁定时为 (0, 0))

获取当前帧累计的鼠标位移,返回 Vector2 类型的结果。在鼠标未被锁定的情况下,接口说明其返回值为 (0, 0)。该函数不需要传入任何参数。

返回值 Vector2 (当前帧鼠标位移;未锁定时为 (0, 0))

示例代码

读取本帧鼠标位移

lua
-- @runtime client
-- 获取用户输入服务(全局单例)
local userInputService = game:GetService("UserInputService")

-- 未锁定鼠标时返回 (0, 0)
local delta = userInputService:GetMouseDelta()
print("本帧累计鼠标位移:", delta)
-- 分量可分别用于水平/垂直视角转动
print("位移分量 x =", delta.x, ", y =", delta.y)
print("位移长度:", delta.Magnitude)

ConvertScreenToUIPosition ​

签名:ConvertScreenToUIPosition(screenPos: Vector2) -> Vector2 (UI(cocos)坐标:左下原点、设计分辨率,与 EUINode:GetWorldPosition 一致)

将屏幕(设备)坐标转换为 UI(cocos) 坐标。参数 screenPos 为屏幕(设备)坐标的 Vector2,返回值同样是 Vector2,但属于 UI(cocos) 坐标系,即左下原点、基于设计分辨率。

参数类型说明
screenPosVector2屏幕(设备)坐标:左上原点、真实像素,与 GetMouseLocation 一致

返回值 Vector2 (UI(cocos)坐标:左下原点、设计分辨率,与 EUINode:GetWorldPosition 一致)

示例代码

将鼠标屏幕坐标转换为 UI 坐标

lua
-- @runtime client
local inputService = game:GetService('UserInputService')
local screenPos = inputService:GetMouseLocation()
local uiPos = inputService:ConvertScreenToUIPosition(screenPos)
local restoredScreenPos = inputService:ConvertUIToScreenPosition(uiPos)
print('屏幕坐标:', screenPos.x, screenPos.y)
print('UI 坐标:', uiPos.x, uiPos.y)
print('回转后的屏幕坐标:', restoredScreenPos.x, restoredScreenPos.y)

ConvertUIToScreenPosition ​

签名:ConvertUIToScreenPosition(uiPos: Vector2) -> Vector2 (屏幕(设备)坐标:左上原点、真实像素,与 GetMouseLocation 一致)

将 UI(cocos) 坐标转换为屏幕(设备)坐标。参数 uiPos 为 UI(cocos) 坐标的 Vector2(左下原点、设计分辨率),返回值为屏幕(设备)坐标的 Vector2(左上原点、真实像素)。

参数类型说明
uiPosVector2UI(cocos)坐标:左下原点、设计分辨率,与 EUINode:GetWorldPosition 一致

返回值 Vector2 (屏幕(设备)坐标:左上原点、真实像素,与 GetMouseLocation 一致)

示例代码

将 UI 坐标转换为屏幕坐标

lua
-- @runtime client
local inputService = game:GetService('UserInputService')
local uiPos = Vector2.New(100, 200)
local screenPos = inputService:ConvertUIToScreenPosition(uiPos)
local restoredUiPos = inputService:ConvertScreenToUIPosition(screenPos)
print('UI 坐标:', uiPos.x, uiPos.y)
print('屏幕坐标:', screenPos.x, screenPos.y)
print('回转后的 UI 坐标:', restoredUiPos.x, restoredUiPos.y)

TouchEnabled ​

签名:TouchEnabled() -> Bool (是否支持触摸)

检查当前设备是否支持触摸输入。

返回值 Bool (是否支持触摸)

示例代码

检查是否支持触摸输入

lua
-- @runtime client
-- 获取 UserInputService 服务
local userInputService = game:GetService("UserInputService")

-- 检查当前设备是否支持触摸输入
local touchEnabled = userInputService:TouchEnabled()
print("是否支持触摸输入:", touchEnabled)
-- 根据触摸支持情况调整 UI(示例)
if touchEnabled then
    print("当前设备支持触摸,显示虚拟摇杆")
else
    print("当前设备不支持触摸,显示键盘提示")
end

GetTouchSimulationEnabled ​

签名:GetTouchSimulationEnabled() -> Bool (是否处于触摸模拟态)

查询当前是否处于触摸模拟态,返回 Bool,主要用于编辑器调试场景。

返回值 Bool (是否处于触摸模拟态)

示例代码

查询当前是否处于触摸模拟态

lua
-- @runtime client
-- 获取输入服务实例
local userInputService = game:GetService("UserInputService")
-- 查询是否处于触摸模拟态(编辑器调试场景下使用)
local touchSimulation = userInputService:GetTouchSimulationEnabled()
print("触摸模拟是否开启:", touchSimulation)
-- 按查询结果输出调试提示,便于确认当前输入链路
if touchSimulation then
    print("当前按触摸语义处理输入,可调试手势逻辑")
else
    print("当前未开启触摸模拟,输入来自真实设备")
end

SetTouchSimulationEnabled ​

签名:SetTouchSimulationEnabled(enabled: Bool) -> Bool (切换前的状态(被门控拒绝时返回当前值,未发生切换))

开启或关闭触摸模拟,参数为 Bool 类型的 enabled,返回切换前的状态(Bool),主要用于编辑器调试场景。

参数类型说明
enabledBool是否开启触摸模拟

返回值 Bool (切换前的状态(被门控拒绝时返回当前值,未发生切换))

示例代码

切换触摸模拟并读取切换前状态

lua
-- @runtime client
-- 获取输入服务实例
local userInputService = game:GetService("UserInputService")
-- 开启触摸模拟,返回值是切换前的状态
local previous = userInputService:SetTouchSimulationEnabled(true)
print("切换前触摸模拟状态:", previous)
-- 查询切换后的最新状态
print("切换后触摸模拟状态:", userInputService:GetTouchSimulationEnabled())
-- 恢复切换前的状态,避免影响后续调试
userInputService:SetTouchSimulationEnabled(previous)
print("恢复后触摸模拟状态:", userInputService:GetTouchSimulationEnabled())

SetGameProcessedEventEnabled ​

签名:SetGameProcessedEventEnabled(enabled: Bool) -> void

开启或关闭 gameProcessedEvent 机制,参数为 Bool 类型的 enabled,无返回值。

参数类型说明
enabledBool是否启用 gameProcessedEvent 判定

返回值 void

示例代码

开关输入事件处理标记机制

lua
-- @runtime client
-- 获取输入服务实例
local userInputService = game:GetService("UserInputService")
-- 记录开关的初始状态,便于后续对比
local beforeEnabled = userInputService:IsGameProcessedEventEnabled()
print("初始状态:", beforeEnabled)
-- 打开该机制,使输入事件携带界面命中标记
userInputService:SetGameProcessedEventEnabled(true)
print("开启后状态:", userInputService:IsGameProcessedEventEnabled())
-- 关闭该机制,避免每次输入都进行界面命中查询
userInputService:SetGameProcessedEventEnabled(false)
print("关闭后状态:", userInputService:IsGameProcessedEventEnabled())

IsGameProcessedEventEnabled ​

签名:IsGameProcessedEventEnabled() -> Bool (是否启用)

查询 gameProcessedEvent 机制是否已启用,返回 Bool。

返回值 Bool (是否启用)

示例代码

查询输入事件处理标记机制的开关状态

lua
-- @runtime client
-- 获取输入服务实例
local userInputService = game:GetService("UserInputService")
-- 查询输入事件处理标记机制的总开关状态
local gpeEnabled = userInputService:IsGameProcessedEventEnabled()
print("输入事件处理标记机制是否启用:", gpeEnabled)
-- 依据开关状态提示输入事件是否附带界面命中信息
if gpeEnabled then
    print("输入事件会附带界面命中判定,读取该标记更准确")
else
    print("输入事件不做界面命中查询,标记恒为未处理")
end

GetStringForKeyCode ​

签名:GetStringForKeyCode(keyCode: KeyCode) -> String (可输入字符或空字符串,永不返回 nil)

返回指定按键在 QWERTY 布局下对应的可输入字符,返回类型为 String,永不返回 nil。

参数类型说明
keyCodeKeyCodeKeyCode 整数值(直接传 KeyCode.W 等枚举即可)

返回值 String (可输入字符或空字符串,永不返回 nil)

示例代码

获取按键对应的可输入字符

lua
-- @runtime client
-- 获取输入服务实例
local userInputService = game:GetService("UserInputService")
-- 把按键枚举转换为 QWERTY 布局下可输入的字符
local letter = userInputService:GetStringForKeyCode(Enums.KeyCode.Q)
print("按键 Q 对应字符:", letter)
-- 数字键同样可以转换,返回值永不为空值以外的类型
local digit = userInputService:GetStringForKeyCode(Enums.KeyCode.One)
print("按键 One 对应字符:", digit)
-- 用转换结果拼出按键提示文案
local hint = "按下 " .. letter .. " 键释放技能,按下 " .. digit .. " 键切换武器"
print(tostring(hint))

GetMouseDeltaSensitivity ​

签名:GetMouseDeltaSensitivity() -> Float (鼠标灵敏度)

获取当前鼠标灵敏度设置。

返回值 Float (鼠标灵敏度)

示例代码

获取鼠标灵敏度

lua
-- @runtime client
-- 获取 UserInputService 服务
local userInputService = game:GetService("UserInputService")

-- 获取当前鼠标灵敏度
local sensitivity = userInputService:GetMouseDeltaSensitivity()
-- 打印鼠标灵敏度
print("当前鼠标灵敏度:", sensitivity)

SetMouseDeltaSensitivity ​

签名:SetMouseDeltaSensitivity(sensitivity: Float) -> void

设置鼠标灵敏度。

参数类型说明
sensitivityFloat鼠标灵敏度

返回值 void

示例代码

设置鼠标灵敏度

lua
-- @runtime client
-- 获取 UserInputService 服务
local userInputService = game:GetService("UserInputService")

-- 设置鼠标灵敏度为 0.5
userInputService:SetMouseDeltaSensitivity(0.5)
-- 验证设置后的灵敏度
local sensitivity = userInputService:GetMouseDeltaSensitivity()
print("设置后的鼠标灵敏度:", sensitivity)

SetTouchMovementMode ​

签名:SetTouchMovementMode(mode: DevTouchMovementMode) -> void

设置触屏移动模式。

⚙ 使用 Enums.DevTouchMovementMode.UserChoice/Thumbstick/FixedThumbstick/DynamicThumbstick/Scriptable,禁止直接传数字。

参数类型说明
modeDevTouchMovementMode触屏移动模式,使用 Enums.DevTouchMovementMode 的具体成员。

返回值 void

示例代码

使用枚举设置触屏移动模式

lua
-- @runtime client
local UserInputService = game:GetService('UserInputService')
local mode = Enums.DevTouchMovementMode.Thumbstick
UserInputService:SetTouchMovementMode(mode)
print('触屏移动模式已更新:', tostring(mode))

GetJoystickMoveVector ​

签名:GetJoystickMoveVector() -> Vector2 (当前虚拟摇杆的移动方向向量)

获取当前摇杆移动方向向量,返回 Vector2 类型的结果,用于表示摇杆当前的移动方向。该函数不需要传入任何参数。

返回值 Vector2 (当前虚拟摇杆的移动方向向量)

示例代码

读取摇杆移动方向向量

lua
-- @runtime client
-- 获取用户输入服务(全局单例)
local userInputService = game:GetService("UserInputService")

-- 读取当前摇杆方向向量,无摇杆输入时为 (0, 0)
local moveVector = userInputService:GetJoystickMoveVector()
print("当前摇杆方向向量:", moveVector)
-- 分量可分别作为水平/垂直方向输入使用
print("方向分量 x =", moveVector.x, ", y =", moveVector.y)
-- 通过长度判断当前是否存在实际摇杆输入
print("方向向量长度:", moveVector.Magnitude)

GetJoystickMoveLength ​

签名:GetJoystickMoveLength() -> Float (当前虚拟摇杆的偏移长度)

获取当前虚拟摇杆的偏移长度。

返回值 Float (当前虚拟摇杆的偏移长度)

示例代码

获取摇杆偏移长度

lua
-- @runtime client
-- 获取 UserInputService 服务
local userInputService = game:GetService("UserInputService")

-- 获取当前摇杆偏移长度
local joystickLength = userInputService:GetJoystickMoveLength()
print("摇杆偏移长度:", joystickLength)
-- 根据偏移长度判断摇杆是否被推动
if joystickLength > 0 then
    print("摇杆正在被推动")
end

GetLastInputType ​

签名:GetLastInputType() -> UserInputType (最后输入类型)

获取最后一次输入的类型。

返回值 UserInputType (最后输入类型)

示例代码

获取最后输入类型

lua
-- @runtime client
-- 获取 UserInputService 服务
local userInputService = game:GetService("UserInputService")

-- 获取最后输入类型
local lastInputType = userInputService:GetLastInputType()
print("最后输入类型:", lastInputType)
-- 根据最后输入类型显示不同提示
if lastInputType == Enums.UserInputType.Keyboard then
    print("最后使用键盘输入")
elseif lastInputType == Enums.UserInputType.Touch then
    print("最后使用触摸输入")
end

GetStringForInputType ​

签名:GetStringForInputType(inputType: UserInputType) -> String (名字描述)

获取输入类型的字符串描述。

参数类型说明
inputTypeUserInputType输入类型

返回值 String (名字描述)

示例代码

获取输入类型的描述字符串

lua
-- @runtime client
-- 获取 UserInputService 服务
local userInputService = game:GetService("UserInputService")

-- 获取键盘输入类型的描述
local keyboardDesc = userInputService:GetStringForInputType(Enums.UserInputType.Keyboard)
print("键盘输入类型描述:", keyboardDesc)
-- 获取触摸输入类型的描述
local touchDesc = userInputService:GetStringForInputType(Enums.UserInputType.Touch)
print("触摸输入类型描述:", touchDesc)

GetMouseBehavior ​

签名:GetMouseBehavior() -> MouseBehavior (鼠标行为)

获取当前鼠标行为模式。

返回值 MouseBehavior (鼠标行为)

示例代码

读取鼠标行为模式

lua
-- @runtime client
local userInputService = game:GetService('UserInputService')
-- 读取当前鼠标行为模式
local currentBehavior = userInputService:GetMouseBehavior()
if currentBehavior == Enums.MouseBehavior.LockCenter then
    print('当前鼠标锁定在屏幕中心,模式为:', currentBehavior)
elseif currentBehavior == Enums.MouseBehavior.LockCurrentPosition then
    print('当前鼠标锁定在当前位置,模式为:', currentBehavior)
else
    print('当前鼠标为默认自由移动模式,模式为:', currentBehavior)
end

SetMouseBehavior ​

签名:SetMouseBehavior(behavior: MouseBehavior) -> void

设置鼠标行为模式。

参数类型说明
behaviorMouseBehavior鼠标行为 (Default=0, LockCenter=1, LockCurrentPosition=2)

返回值 void

示例代码

将鼠标行为切换为锁定屏幕中心

lua
-- @runtime client
local userInputService = game:GetService('UserInputService')
-- 锁定后鼠标指针将停留在屏幕中心
userInputService:SetMouseBehavior(Enums.MouseBehavior.LockCenter)
local newBehavior = userInputService:GetMouseBehavior()
print('设置后的鼠标行为:', newBehavior)
if newBehavior == Enums.MouseBehavior.LockCenter then
    print('确认:鼠标已锁定到屏幕中心')
end

GetMouseIconEnabled ​

签名:GetMouseIconEnabled() -> Bool (是否启用鼠标图标)

获取鼠标图标是否启用的状态。

返回值 Bool (是否启用鼠标图标)

示例代码

查询鼠标图标显示状态

lua
-- @runtime client
local userInputService = game:GetService('UserInputService')
local isIconEnabled = userInputService:GetMouseIconEnabled()
if isIconEnabled then
    print('鼠标图标当前已显示')
else
    print('鼠标图标当前已隐藏')
end

SetMouseIconEnabled ​

签名:SetMouseIconEnabled(enabled: Bool) -> void

设置鼠标图标是否启用。

参数类型说明
enabledBool是否启用鼠标图标

返回值 void

示例代码

临时隐藏鼠标图标并恢复

lua
-- @runtime client
local userInputService = game:GetService('UserInputService')
-- 先记录当前鼠标图标状态,便于演示后恢复
local originalState = userInputService:GetMouseIconEnabled()
print('隐藏前鼠标图标状态:', originalState)
userInputService:SetMouseIconEnabled(false)
local hiddenState = userInputService:GetMouseIconEnabled()
print('隐藏后鼠标图标状态:', hiddenState)
-- 将鼠标图标恢复为进入示例前的状态
userInputService:SetMouseIconEnabled(originalState)