Klasa silnika
UserInputService
*Ta zawartość została przetłumaczona przy użyciu narzędzi AI (w wersji beta) i może zawierać błędy. Aby wyświetlić tę stronę w języku angielskim, kliknij tutaj.
Podsumowanie
Właściwości
MouseIcon:ContentId |
Metody
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):() |
Zdarzenia
Materiały referencyjne dotyczące interfejsów API
Właściwości
AccelerometerEnabled
Przykłady kodu
Przenieś piłkę używając akcelerometru
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
Przykłady kodu
Niestandardowa ikona myszy
local UserInputService = game:GetService("UserInputService")
-- Aby przywrócić kursor do tego, co było wcześniej ustawione, należy zapisać to w zmiennej
local savedCursor = nil
local function setTemporaryCursor(cursor: string)
-- Zaktualizuj zapisany kursor tylko wtedy, gdy nie jest aktualnie zapisany
if not savedCursor then
savedCursor = UserInputService.MouseIcon
end
UserInputService.MouseIcon = cursor
end
local function clearTemporaryCursor()
-- Przywróć kursor myszy tylko wtedy, gdy jest zapisany kursor do przywrócenia
if savedCursor then
UserInputService.MouseIcon = savedCursor
-- Nie przywracaj tego samego kursora dwa razy (może nadpisać inny skrypt)
savedCursor = nil
end
end
setTemporaryCursor("http://www.roblox.com/asset?id=163023520")
print(UserInputService.MouseIcon)
clearTemporaryCursor()MouseIconContent
Przykłady kodu
Niestandardowa ikona myszy
local UserInputService = game:GetService("UserInputService")
-- Aby przywrócić kursor do tego, co było wcześniej ustawione, należy zapisać to w zmiennej
local savedCursor = nil
local function setTemporaryCursor(cursor: string)
-- Zaktualizuj zapisany kursor tylko wtedy, gdy nie jest aktualnie zapisany
if not savedCursor then
savedCursor = UserInputService.MouseIcon
end
UserInputService.MouseIcon = cursor
end
local function clearTemporaryCursor()
-- Przywróć kursor myszy tylko wtedy, gdy jest zapisany kursor do przywrócenia
if savedCursor then
UserInputService.MouseIcon = savedCursor
-- Nie przywracaj tego samego kursora dwa razy (może nadpisać inny skrypt)
savedCursor = nil
end
end
setTemporaryCursor("http://www.roblox.com/asset?id=163023520")
print(UserInputService.MouseIcon)
clearTemporaryCursor()PreferredInput
Przykłady kodu
Preferowana detekcja wejścia
local UserInputService = game:GetService("UserInputService")
local function preferredInputChanged()
local preferredInput = UserInputService.PreferredInput
if preferredInput == Enum.PreferredInput.Touch then
-- Gracz korzysta z urządzenia obsługującego dotyk, bez innych dostępnych/połączonych typów wejścia
print("Touch")
elseif preferredInput == Enum.PreferredInput.Gamepad then
-- Gracz podłączył lub ostatnio korzystał z kontrolera
print("Gamepad")
elseif preferredInput == Enum.PreferredInput.KeyboardAndMouse then
-- Gracz podłączył lub ostatnio korzystał z klawiatury lub myszki
print("KeyboardAndMouse")
end
end
preferredInputChanged()
UserInputService:GetPropertyChangedSignal("PreferredInput"):Connect(function()
preferredInputChanged()
end)UserHeadCFrame
VREnabled
Metody
GamepadSupports
UserInputService:GamepadSupports(
Parametry
Zwroty
GetDeviceAcceleration
Zwroty
Przykłady kodu
Przyspieszenie Urządzenia Wyjściowego
local UserInputService = game:GetService("UserInputService")
if UserInputService.AccelerometerEnabled then
local acceleration = UserInputService:GetDeviceAcceleration().Position
print(acceleration)
else
warn("Nie można uzyskać przyspieszenia urządzenia, ponieważ urządzenie nie ma włączonego akcelerometru!")
endGetDeviceGravity
Zwroty
Przykłady kodu
Przemieszczanie obiektów za pomocą żyroskopu
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
-- Powiąż zdarzenie, gdy żyroskop wykrywa zmianę
UserInputService.DeviceGravityChanged:Connect(function(accel)
-- Przesuń bańkę w świecie na podstawie danych z żyroskopu
bubble.Position = Vector3.new(-8 * accel.Position.X, 1.8, -8 * accel.Position.Z)
end)
endGetDeviceRotation
Zwroty
Przykłady kodu
Rotacja urządzenia wyjściowego
local UserInputService = game:GetService("UserInputService")
if UserInputService.GyroscopeEnabled then
local _, cf = UserInputService:GetDeviceRotation()
print(cf)
else
warn("Nie można uzyskać rotacji urządzenia, ponieważ urządzenie nie ma włączonego żyroskopu!")
endGetGamepadConnected
Parametry
Zwroty
GetGamepadState
Parametry
Zwroty
GetImageForKeyCode
Parametry
Zwroty
ContentId
Przykłady kodu
UserInputService - Pobierz obraz dla KeyCode
local UserInputService = game:GetService("UserInputService")
local imageLabel = script.Parent
local key = Enum.KeyCode.ButtonA
local mappedIconImage = UserInputService:GetImageForKeyCode(key)
imageLabel.Image = mappedIconImageGetLastInputType
Zwroty
Przykłady kodu
Wykryj Ostatni Typ Wejścia
local UserInputService = game:GetService("UserInputService")
if UserInputService:GetLastInputType() == Enum.UserInputType.Keyboard then
print("Ostatnie wejście było z klawiatury!")
endGetMouseButtonsPressed
Zwroty
Przykłady kodu
Sprawdź wciśnięte przyciski myszy
local UserInputService = game:GetService("UserInputService")
UserInputService.InputBegan:Connect(function(input, gameProcessedEvent)
-- Zwróć tablicę wciśniętych przycisków myszy
local buttonsPressed = UserInputService:GetMouseButtonsPressed()
local m1, m2 = false, false
for _, button in buttonsPressed do
if button.UserInputType == Enum.UserInputType.MouseButton1 then
print("Wciśnięto MouseButton1!")
m1 = true
end
if button.UserInputType == Enum.UserInputType.MouseButton2 then
print("Wciśnięto MouseButton2!")
m2 = true
end
if m1 and m2 then
print("Wciśnięto oba przyciski myszy!")
end
end
end)GetMouseDelta
Zwroty
Przykłady kodu
Uzyskiwanie Deltu Myszki
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("Myszka się poruszyła", delta, "od ostatniego kroku")
end
end
RunService:BindToRenderStep("MeasureMouseMovement", Enum.RenderPriority.Input.Value, onRenderStep)GetStringForKeyCode
Parametry
| Wartość domyślna: "Default" |
Zwroty
GetSupportedGamepadKeyCodes
Parametry
Zwroty
GetUserCFrame
IsGamepadButtonDown
UserInputService:IsGamepadButtonDown(
Parametry
Zwroty
IsKeyDown
IsMouseButtonPressed
Parametry
Zwroty
Parametry
Zwroty
RecenterUserHeadCFrame
UserInputService:RecenterUserHeadCFrame():()
Zwroty
()
Parametry
Zwroty
()
Zdarzenia
DeviceAccelerationChanged
Parametry
Przykłady kodu
Kontroluj graczy za pomocą akcelerometru
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
Parametry
Przykłady kodu
Przenieś piłkę używając akcelerometru
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
Parametry
GamepadConnected
Parametry
GamepadDisconnected
Parametry
InputBegan
Parametry
InputChanged
Parametry
InputEnded
Parametry
JumpRequest
Przykłady kodu
Wyłącz domyślne skakanie
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
-- Przetwórz niestandardowe żądanie skoku
print("Żądanie skoku!")
-- Zresetuj zmienną debounce po okresie oczekiwania
task.wait(COOLDOWN_TIME)
processJumpRequest = false
end
end
UserInputService.JumpRequest:Connect(jumpRequest)LastInputTypeChanged
Parametry
PointerAction
TextBoxFocused
Parametry
Przykłady kodu
Modyfikacja TextBox na Skupionym i Utraconym Fokusie
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
Parametry
Przykłady kodu
Modyfikacja TextBox na Skupionym i Utraconym Fokusie
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(
Parametry
TouchEnded
Parametry
TouchLongPress
UserInputService.TouchLongPress(
Parametry
TouchMoved
Parametry
TouchPan
UserInputService.TouchPan(
touchPositions:{any}, totalTranslation:Vector2, velocity:Vector2, state:Enum.UserInputState, gameProcessedEvent:boolean
Parametry
TouchPinch
UserInputService.TouchPinch(
touchPositions:{any}, scale:number, velocity:number, state:Enum.UserInputState, gameProcessedEvent:boolean
Parametry
TouchRotate
UserInputService.TouchRotate(
touchPositions:{any}, rotation:number, velocity:number, state:Enum.UserInputState, gameProcessedEvent:boolean
Parametry
TouchStarted
Parametry
TouchSwipe
UserInputService.TouchSwipe(
Parametry
TouchTap
TouchTapInWorld
UserCFrameChanged
WindowFocusReleased
Przykłady kodu
Skrypt Zmiany Fokusu Okna (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)Skrypt Zdalnego Skupienia Okna (Skrypt)
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)