Engine-klasse
UserInputService
*Deze inhoud is vertaald met behulp van AI (bèta) en kan fouten bevatten. Klik hier om deze pagina in het Engels te bekijken.
Samenvatting
Eigenschappen
MouseIcon:ContentId |
Methoden
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):() |
Evenementen
API-referentie
Eigenschappen
AccelerometerEnabled
Codevoorbeelden
Beweeg een Bal met de Versnellingsmeter
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
Codevoorbeelden
Aangepast Muiskicoon
local UserInputService = game:GetService("UserInputService")
-- Om de cursor terug te zetten naar wat het eerder was, moet deze worden opgeslagen in een variabele
local savedCursor = nil
local function setTemporaryCursor(cursor: string)
-- Update de opgeslagen cursor alleen als deze momenteel niet is opgeslagen
if not savedCursor then
savedCursor = UserInputService.MouseIcon
end
UserInputService.MouseIcon = cursor
end
local function clearTemporaryCursor()
-- Herstel de muiscursor alleen als er een opgeslagen cursor is om te herstellen
if savedCursor then
UserInputService.MouseIcon = savedCursor
-- Herstel dezelfde cursor niet twee keer (kan een ander script overschrijven)
savedCursor = nil
end
end
setTemporaryCursor("http://www.roblox.test1709.icu/asset?id=163023520")
print(UserInputService.MouseIcon)
clearTemporaryCursor()MouseIconContent
Codevoorbeelden
Aangepast Muiskicoon
local UserInputService = game:GetService("UserInputService")
-- Om de cursor terug te zetten naar wat het eerder was, moet deze worden opgeslagen in een variabele
local savedCursor = nil
local function setTemporaryCursor(cursor: string)
-- Update de opgeslagen cursor alleen als deze momenteel niet is opgeslagen
if not savedCursor then
savedCursor = UserInputService.MouseIcon
end
UserInputService.MouseIcon = cursor
end
local function clearTemporaryCursor()
-- Herstel de muiscursor alleen als er een opgeslagen cursor is om te herstellen
if savedCursor then
UserInputService.MouseIcon = savedCursor
-- Herstel dezelfde cursor niet twee keer (kan een ander script overschrijven)
savedCursor = nil
end
end
setTemporaryCursor("http://www.roblox.test1709.icu/asset?id=163023520")
print(UserInputService.MouseIcon)
clearTemporaryCursor()PreferredInput
Codevoorbeelden
Voorkeursinvoerdetectie
local UserInputService = game:GetService("UserInputService")
local function preferredInputChanged()
local preferredInput = UserInputService.PreferredInput
if preferredInput == Enum.PreferredInput.Touch then
-- Speler is op een apparaat met aanraakfunctionaliteit zonder andere invoertypen beschikbaar/aangesloten
print("Aanraak")
elseif preferredInput == Enum.PreferredInput.Gamepad then
-- Speler heeft een gamepad aangesloten of heeft daar het laatst mee interactie gehad
print("Gamepad")
elseif preferredInput == Enum.PreferredInput.KeyboardAndMouse then
-- Speler heeft een toetsenbord of muis aangesloten of heeft daar het laatst mee interactie gehad
print("ToetsenbordEnMuis")
end
end
preferredInputChanged()
UserInputService:GetPropertyChangedSignal("PreferredInput"):Connect(function()
preferredInputChanged()
end)UserHeadCFrame
VREnabled
Methoden
GamepadSupports
UserInputService:GamepadSupports(
Parameters
Teruggaves
GetDeviceAcceleration
Teruggaves
Codevoorbeelden
Versnelling van uitvoerapparaat
local UserInputService = game:GetService("UserInputService")
if UserInputService.AccelerometerEnabled then
local acceleration = UserInputService:GetDeviceAcceleration().Position
print(acceleration)
else
warn("Kan de apparaatversnelling niet ophalen omdat het apparaat geen ingeschakelde versnellingsmeter heeft!")
endGetDeviceGravity
Teruggaves
Codevoorbeelden
Objecten Bewegen met de Gyroscoop
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
-- Koppel evenement aan wanneer de gyroscoop een verandering detecteert
UserInputService.DeviceGravityChanged:Connect(function(accel)
-- Beweeg de bubbel in de wereld op basis van de gyroscoopgegevens
bubble.Position = Vector3.new(-8 * accel.Position.X, 1.8, -8 * accel.Position.Z)
end)
endGetDeviceRotation
Teruggaves
Codevoorbeelden
Rotatie van uitvoerapparaat
local UserInputService = game:GetService("UserInputService")
if UserInputService.GyroscopeEnabled then
local _, cf = UserInputService:GetDeviceRotation()
print(cf)
else
warn("Kan de rotatie van het apparaat niet krijgen omdat het apparaat geen ingeschakelde gyroscoop heeft!")
endGetGamepadConnected
Parameters
Teruggaves
GetGamepadState
Parameters
Teruggaves
GetImageForKeyCode
Parameters
Teruggaves
ContentId
Codevoorbeelden
UserInputService - Verkrijg Afbeelding Voor ToetsCode
local UserInputService = game:GetService("UserInputService")
local imageLabel = script.Parent
local key = Enum.KeyCode.ButtonA
local mappedIconImage = UserInputService:GetImageForKeyCode(key)
imageLabel.Image = mappedIconImageGetLastInputType
Teruggaves
Codevoorbeelden
Detecteer Laatste Invoertype
local UserInputService = game:GetService("UserInputService")
if UserInputService:GetLastInputType() == Enum.UserInputType.Keyboard then
print("Meest recente invoer was toetsenbord!")
endGetMouseButtonsPressed
Teruggaves
Codevoorbeelden
Controleer ingedrukte muisknoppen
local UserInputService = game:GetService("UserInputService")
UserInputService.InputBegan:Connect(function(input, gameProcessedEvent)
-- Geef een array terug van de ingedrukte muisknoppen
local buttonsPressed = UserInputService:GetMouseButtonsPressed()
local m1, m2 = false, false
for _, button in buttonsPressed do
if button.UserInputType == Enum.UserInputType.MouseButton1 then
print("MouseButton1 ingedrukt!")
m1 = true
end
if button.UserInputType == Enum.UserInputType.MouseButton2 then
print("MouseButton2 ingedrukt!")
m2 = true
end
if m1 and m2 then
print("Beide muisknoppen ingedrukt!")
end
end
end)GetMouseDelta
Teruggaves
Codevoorbeelden
Muis Delta Krijgen
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("De muis is verplaatst", delta, "sinds de laatste stap")
end
end
RunService:BindToRenderStep("MeetMuisBeweging", Enum.RenderPriority.Input.Value, onRenderStep)GetStringForKeyCode
Parameters
| Standaardwaarde: Default |
Teruggaves
GetSupportedGamepadKeyCodes
Parameters
Teruggaves
GetUserCFrame
IsGamepadButtonDown
UserInputService:IsGamepadButtonDown(
Parameters
Teruggaves
IsKeyDown
Parameters
Teruggaves
IsMouseButtonPressed
Parameters
Teruggaves
Parameters
Teruggaves
RecenterUserHeadCFrame
UserInputService:RecenterUserHeadCFrame():()
Teruggaves
()
Parameters
Teruggaves
()
Evenementen
DeviceAccelerationChanged
Parameters
Codevoorbeelden
Spelers Besturen met de Versnellingsmeter
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
Parameters
Codevoorbeelden
Beweeg een Bal met de Versnellingsmeter
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
Parameters
GamepadConnected
Parameters
GamepadDisconnected
Parameters
InputBegan
Parameters
InputChanged
Parameters
InputEnded
Parameters
JumpRequest
Codevoorbeelden
Standaard springen uitschakelen
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
-- Verwerk aangepast sprongverzoek
print("Sprong aangevraagd!")
-- Reset debounce-variabele na cooldown
task.wait(COOLDOWN_TIME)
processJumpRequest = false
end
end
UserInputService.JumpRequest:Connect(jumpRequest)LastInputTypeChanged
Parameters
PointerAction
TextBoxFocused
Parameters
Codevoorbeelden
TextBox Wijziging bij Focus en FocusVerlies
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
Parameters
Codevoorbeelden
TextBox Wijziging bij Focus en FocusVerlies
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(
Parameters
TouchEnded
Parameters
TouchLongPress
UserInputService.TouchLongPress(
Parameters
TouchMoved
Parameters
TouchPan
UserInputService.TouchPan(
touchPositions:{any}, totalTranslation:Vector2, velocity:Vector2, state:Enum.UserInputState, gameProcessedEvent:boolean
Parameters
TouchPinch
UserInputService.TouchPinch(
touchPositions:{any}, scale:number, velocity:number, state:Enum.UserInputState, gameProcessedEvent:boolean
Parameters
TouchRotate
UserInputService.TouchRotate(
touchPositions:{any}, rotation:number, velocity:number, state:Enum.UserInputState, gameProcessedEvent:boolean
Parameters
TouchStarted
Parameters
TouchSwipe
UserInputService.TouchSwipe(
Parameters
TouchTap
TouchTapInWorld
UserCFrameChanged
WindowFocusReleased
Codevoorbeelden
Venster Focus Weg Script (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)Script voor Vensterfocus Weg (Script)
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)