User Input Service
*เนื้อหานี้แปลโดยใช้ AI (เวอร์ชัน Beta) และอาจมีข้อผิดพลาด หากต้องการดูหน้านี้เป็นภาษาอังกฤษ ให้คลิกที่นี่
ข้อมูลสรุป
คุณสมบัติ
MouseIcon:ContentId |
วิธีการ
CreateVirtualInput(target: Instance?):Object |
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
คุณสมบัติ
Accelerometer Enabled
ตัวอย่างโค้ด
เคลื่อนที่ลูกบอลโดยใช้ 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)
endModal Enabled
Mouse Icon
User Input Service. Mouse Icon:ContentId
ตัวอย่างโค้ด
ไอคอนเมาส์ที่กำหนดเอง
local UserInputService = game:GetService("UserInputService")
-- เพื่อคืนค่าเคอร์เซอร์กลับไปเป็นสิ่งที่ตั้งไว้ก่อนหน้านี้ จะต้องบันทึกไว้ในตัวแปร
local savedCursor = nil
local function setTemporaryCursor(cursor: string)
-- อัปเดตเคอร์เซอร์ที่บันทึกไว้เฉพาะเมื่อยังไม่ได้บันทึก
if not savedCursor then
savedCursor = UserInputService.MouseIcon
end
UserInputService.MouseIcon = cursor
end
local function clearTemporaryCursor()
-- คืนค่าเคอร์เซอร์เมาส์เฉพาะเมื่อมีเคอร์เซอร์ที่บันทึกไว้เพื่อคืนค่า
if savedCursor then
UserInputService.MouseIcon = savedCursor
-- ไม่คืนค่าเคอร์เซอร์เดียวกันสองครั้ง (อาจเขียนทับสคริปต์อื่น)
savedCursor = nil
end
end
setTemporaryCursor("http://www.roblox.com/asset?id=163023520")
print(UserInputService.MouseIcon)
clearTemporaryCursor()Mouse Icon Content
ตัวอย่างโค้ด
ไอคอนเมาส์ที่กำหนดเอง
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()Preferred Input
ตัวอย่างโค้ด
การตรวจจับการป้อนข้อมูลที่ต้องการ
local UserInputService = game:GetService("UserInputService")
local function preferredInputChanged()
local preferredInput = UserInputService.PreferredInput
if preferredInput == Enum.PreferredInput.Touch then
-- ผู้เล่นอยู่บนอุปกรณ์ที่รองรับการสัมผัสโดยไม่มีประเภทการป้อนข้อมูลอื่นที่สามารถใช้ได้/เชื่อมต่อ
print("Touch")
elseif preferredInput == Enum.PreferredInput.Gamepad then
-- ผู้เล่นได้เชื่อมต่อหรือมีปฏิสัมพันธ์ล่าสุดกับเกมแพด
print("Gamepad")
elseif preferredInput == Enum.PreferredInput.KeyboardAndMouse then
-- ผู้เล่นได้เชื่อมต่อหรือมีปฏิสัมพันธ์ล่าสุดกับคีย์บอร์ดหรือเมาส์
print("KeyboardAndMouse")
end
end
preferredInputChanged()
UserInputService:GetPropertyChangedSignal("PreferredInput"):Connect(function()
preferredInputChanged()
end)User Head C Frame
VR Enabled
วิธีการ
Create Virtual Input
Gamepad Supports
User Input Service: Gamepad Supports(
พารามิเตอร์
ส่งค่ากลับ
Get Device Acceleration
ส่งค่ากลับ
ตัวอย่างโค้ด
การเร่งความเร็วของอุปกรณ์เอาต์พุต
local UserInputService = game:GetService("UserInputService")
if UserInputService.AccelerometerEnabled then
local acceleration = UserInputService:GetDeviceAcceleration().Position
print(acceleration)
else
warn("ไม่สามารถรับความเร่งของอุปกรณ์ได้เนื่องจากอุปกรณ์ไม่มีเซ็นเซอร์วัดความเร่งที่เปิดใช้งาน!")
endGet Device Gravity
ส่งค่ากลับ
ตัวอย่างโค้ด
การเคลื่อนที่ของวัตถุด้วยไจโรสโคป
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)
endGet Device Rotation
ส่งค่ากลับ
ตัวอย่างโค้ด
การหมุนอุปกรณ์เอาต์พุต
local UserInputService = game:GetService("UserInputService")
if UserInputService.GyroscopeEnabled then
local _, cf = UserInputService:GetDeviceRotation()
print(cf)
else
warn("ไม่สามารถรับการหมุนอุปกรณ์ได้เนื่องจากอุปกรณ์ไม่มีไจโรสโคปที่เปิดใช้งาน!")
endGet Gamepad Connected
พารามิเตอร์
ส่งค่ากลับ
Get Gamepad State
พารามิเตอร์
ส่งค่ากลับ
Get Image For Key Code
พารามิเตอร์
ส่งค่ากลับ
ContentId
ตัวอย่างโค้ด
UserInputService - รับภาพสำหรับ KeyCode
local UserInputService = game:GetService("UserInputService")
local imageLabel = script.Parent
local key = Enum.KeyCode.ButtonA
-- รับภาพที่แมพไว้สำหรับ KeyCode
local mappedIconImage = UserInputService:GetImageForKeyCode(key)
imageLabel.Image = mappedIconImageGet Last Input Type
ส่งค่ากลับ
ตัวอย่างโค้ด
ตรวจจับประเภทการป้อนข้อมูลล่าสุด
local UserInputService = game:GetService("UserInputService")
if UserInputService:GetLastInputType() == Enum.UserInputType.Keyboard then
print("การป้อนข้อมูลล่าสุดคือคีย์บอร์ด!")
endGet Mouse Buttons Pressed
ส่งค่ากลับ
ตัวอย่างโค้ด
ตรวจสอบปุ่มเมาส์ที่ถูกกด
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)Get 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("เมาส์ได้เคลื่อนที่", delta, "ตั้งแต่ขั้นตอนล่าสุด")
end
end
RunService:BindToRenderStep("MeasureMouseMovement", Enum.RenderPriority.Input.Value, onRenderStep)Get String For Key Code
พารามิเตอร์
| ค่าเริ่มต้น: Default |
ส่งค่ากลับ
Get Supported Gamepad Key Codes
พารามิเตอร์
ส่งค่ากลับ
Get User C Frame
Is Gamepad Button Down
User Input Service: Is Gamepad Button Down(
พารามิเตอร์
ส่งค่ากลับ
Is Key Down
พารามิเตอร์
ส่งค่ากลับ
Is Mouse Button Pressed
พารามิเตอร์
ส่งค่ากลับ
พารามิเตอร์
ส่งค่ากลับ
Recenter User Head C Frame
User Input Service: Recenter User Head C Frame():()
ส่งค่ากลับ
()
พารามิเตอร์
ส่งค่ากลับ
()
เหตุการณ์
Device Acceleration Changed
พารามิเตอร์
ตัวอย่างโค้ด
ควบคุมผู้เล่นด้วย 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)
endDevice Gravity Changed
พารามิเตอร์
ตัวอย่างโค้ด
เคลื่อนที่ลูกบอลโดยใช้ 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)
endDevice Rotation Changed
พารามิเตอร์
Gamepad Connected
พารามิเตอร์
Gamepad Disconnected
พารามิเตอร์
Input Began
พารามิเตอร์
Input Changed
พารามิเตอร์
Input Ended
พารามิเตอร์
Jump Request
ตัวอย่างโค้ด
ปิดการกระโดดเริ่มต้น
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("คำขอกระโดด!")
-- รีเซ็ตตัวแปร debounce หลังจาก cooldown
task.wait(COOLDOWN_TIME)
processJumpRequest = false
end
end
UserInputService.JumpRequest:Connect(jumpRequest)Last Input Type Changed
พารามิเตอร์
Pointer Action
Text Box Focused
พารามิเตอร์
ตัวอย่างโค้ด
การปรับเปลี่ยน TextBox เมื่อมีการโฟกัสและยกเลิกการโฟกัส
local UserInputService = game:GetService("UserInputService")
local function textBoxFocused(textBox)
textBox.BackgroundTransparency = 0
end
local function textBoxFocusReleased(textBox)
textBox.BackgroundTransparency = 0.5
end
UserInputService.TextBoxFocused:Connect(textBoxFocused)
UserInputService.TextBoxFocusReleased:Connect(textBoxFocusReleased)Text Box Focus Released
พารามิเตอร์
ตัวอย่างโค้ด
การปรับเปลี่ยน TextBox เมื่อมีการโฟกัสและยกเลิกการโฟกัส
local UserInputService = game:GetService("UserInputService")
local function textBoxFocused(textBox)
textBox.BackgroundTransparency = 0
end
local function textBoxFocusReleased(textBox)
textBox.BackgroundTransparency = 0.5
end
UserInputService.TextBoxFocused:Connect(textBoxFocused)
UserInputService.TextBoxFocusReleased:Connect(textBoxFocusReleased)Touch Drag
User Input Service. Touch Drag(
พารามิเตอร์
Touch Ended
พารามิเตอร์
Touch Long Press
User Input Service. Touch Long Press(
พารามิเตอร์
Touch Moved
พารามิเตอร์
Touch Pan
User Input Service. Touch Pan(
touch Positions:{any}, total Translation:Vector2, velocity:Vector2, state:Enum.UserInputState, game Processed Event:boolean
พารามิเตอร์
Touch Pinch
User Input Service. Touch Pinch(
touch Positions:{any}, scale:number, velocity:number, state:Enum.UserInputState, game Processed Event:boolean
พารามิเตอร์
Touch Rotate
User Input Service. Touch Rotate(
touch Positions:{any}, rotation:number, velocity:number, state:Enum.UserInputState, game Processed Event:boolean
พารามิเตอร์
Touch Started
พารามิเตอร์
Touch Swipe
User Input Service. Touch Swipe(
พารามิเตอร์
Touch Tap
Touch Tap In World
User C Frame Changed
Window Focus Released
ตัวอย่างโค้ด
สคริปต์การให้ความสนใจหน้าต่าง (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)