概要
屬性
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:
-- 只有在當前沒有儲存的游標時才更新儲存的游標
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:
-- 只有在當前沒有儲存的游標時才更新儲存的游標
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("觸控")
elseif preferredInput == Enum.PreferredInput.Gamepad then
-- 玩家已連接或最後與 一個遊戲手把互動
print("遊戲手把")
elseif preferredInput == Enum.PreferredInput.KeyboardAndMouse then
-- 玩家已連接或最後與鍵盤或滑鼠互動
print("鍵盤和滑鼠")
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("MeasureMouseMovement", 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)