PcoWSkbVqDnWTu_dm2ix
We use cookies on this site to enhance your user experience
Collapse Sidebar

UserInputType

InputObject

UserInputType

Contains a Enum/UserInputType enum that describes the type of user input, such as whether the input is via keyboard, mouse, touch, gamepad, to name a few.

Enums

Name Value Description

MouseButton1

0

The left mouse button.

MouseButton2

1

The right mouse button.

MouseButton3

2

The middle mouse button.

MouseWheel

3

The mouse wheel.

MouseMovement

4

Movement of the mouse.

Touch

7

A tap on the screen from a mobile device.

Keyboard

8

Key press on a keyboard.

Focus

9

The client regaining focus of the Roblox window.

Accelerometer

10

The accelerometer of a mobile device.

Gyro

11

The Gyroscope of a mobile device.

Gamepad1

12

Input from the 1st plugged in Gamepad.

Gamepad2

13

Input from the 2nd plugged in Gamepad.

Gamepad3

14

Input from the 3rd plugged in Gamepad.

Gamepad4

15

Input from the 4th plugged in Gamepad.

Gamepad5

16

Input from the 5th plugged in Gamepad

Gamepad6

17

Input from the 6th plugged in Gamepad.

Gamepad7

18

Input from the 7th plugged in Gamepad.

Gamepad8

19

Input from the 8th plugged in Gamepad.

TextInput

20

Input of Text into a text-based GuiObject. Normally this is only a TextBox.

None

21

Unknown UserInputType.

See also

  • InputObject/Delta
  • InputObject/KeyCode
  • InputObject/Position
  • InputObject/UserInputState


Code Samples


Handling InputChanged

The following example demonstrates one of many usage examples of handling user input from InputChanged depending on its type.

-- In order to use the InputChanged event, the UserInputService service must be used
local userInputService = game:GetService("UserInputService")

-- Prints the current input position and the change (delta) in position
local function printMovement(input)
    print("\tPosition:",input.Position)
    print("\tMovement Delta:",input.Delta)
end
 
-- A sample function providing multiple usage cases for various types of user input
local function InputChanged(input, gameProcessed)
    if input.UserInputType == Enum.UserInputType.MouseMovement then
   	 print("The mouse has been moved!")
   	 printMovement(input)
    elseif input.UserInputType == Enum.UserInputType.MouseWheel then
   	 print("The mouse wheel has been scrolled!")
   	 print("\tWheel Movement:",input.Position.Z)
    elseif input.UserInputType == Enum.UserInputType.Gamepad1 then
   	 if input.KeyCode == Enum.KeyCode.Thumbstick1 then
   		 print("The left thumbstick has been moved!")
   		 printMovement(input)
   	 elseif input.KeyCode == Enum.KeyCode.Thumbstick2 then
   		 print("The right thumbstick has been moved!")
   		 printMovement(input)
   	 elseif input.KeyCode == Enum.KeyCode.ButtonL2 then
   		 print("The pressure being applied to the left trigger has changed!")
   		 print("\tPressure:",input.Position.Z)
   	 elseif input.KeyCode == Enum.KeyCode.ButtonR2 then
   		 print("The pressure being applied to the right trigger has changed!")
   		 print("\tPressure:",input.Position.Z)
   	 end
    elseif input.UserInputType == Enum.UserInputType.Touch then
   	 print("The user's finger is moving on the screen!")
   	 printMovement(input)
    elseif input.UserInputType == Enum.UserInputType.Gyro then
   	 local rotInput,rotCFrame = UserInputService:GetDeviceRotation()
   	 local rotX,rotY,rotZ = rotCFrame:toEulerAnglesXYZ()
   	 local rot = Vector3.new(math.deg(rotX),math.deg(rotY),math.deg(rotZ))
   	 print("The rotation of the user's mobile device has been changed!")
   	 print("\tPosition",rotCFrame.p)
   	 print("\tRotation:",rot)
    elseif input.UserInputType == Enum.UserInputType.Accelerometer then
   	 print("The acceleration of the user's mobile device has been changed!")
   	 printMovement(input)
    end    
end

userInputService.InputChanged:Connect(InputChanged)

Handling InputBegan

The following example demonstrates one of many usage examples of handling user input from InputBegan depending on its type.

-- In order to use the InputBegan event, the UserInputService service must be used
local UserInputService = game:GetService("UserInputService")

-- A sample function providing multiple usage cases for various types of user input
UserInputService.InputBegan:Connect(function(input, gameProcessed)
	if input.UserInputType == Enum.UserInputType.Keyboard then
		local keyPressed = input.KeyCode
		print("A key is being pushed down! Key:",input.KeyCode)
	elseif input.UserInputType == Enum.UserInputType.MouseButton1 then
		print("The left mouse button has been pressed down at",input.Position)
	elseif input.UserInputType == Enum.UserInputType.MouseButton2 then
		print("The right mouse button has been pressed down at",input.Position)
	elseif input.UserInputType == Enum.UserInputType.Touch then
		print("A touchscreen input has started at",input.Position)
	elseif input.UserInputType == Enum.UserInputType.Gamepad1 then
		print("A button is being pressed on a gamepad! Button:",input.KeyCode)
	end

	if gameProcessed then
		print("\tThe game engine internally observed this input!")
	else
		print("\tThe game engine did not internally observe this input!")
	end
end)

Create a Custom CameraScript

By default, Roblox relies on a LocalScript, described [here][1], to control the user’s camera. However, this script can be overridden with a custom CameraScript. The example below demonstrates how to create a custom script to control the user’s camera using many of the UserInputService events.

The script is broken into two parts:

  1. Mobile camera events, which rely on touch events
  2. Non-mobile camera events, which rely on keyboard input and tracking the user’s movement

First, the camera script needs utility functions to setup the camera and set its Camera/CameraType|CameraType to Scriptable so that the script can control the camera. It also needs a function to update the camera when it moves, rotates, and zooms.

Using touch events allows us to track user input as they interact with the touchscreen on their mobile device. These events allow us to handle camera movement, rotation, and zoom.

The second half of the code sample adds camera support for players on desktop devices. When input begans, the function Input() checks that the state of the input is Enum/UserInputState|Enum.UserInputState.Begin to ignore all keypress inputs other than when the user first presses a key down. When the user presses I and O the camera zooms in and out. When the presses down and moves their left mouse button, the script Enum/MouseBehavior|locks the player’s mouse by changing the UserInputService/MouseBehavior property. The camera rotates according to the mouse’s UserInputService/GetMouseDelta|change in screen position. When the player moves their character, the camera moves with them.

All of the parts discussed above are combined and shown in the code sample below.

-- ========================================
-- GLOBAL VARIABLES
-- ========================================
local UserInputService = game:GetService("UserInputService")
local RunService = game:GetService("RunService")

-- The camera used by the LocalPlayer
local camera = game.Workspace.CurrentCamera

local players = game:GetService("Players")
local player = players.LocalPlayer
local character = player.CharacterAdded:Wait()
local torso = character:WaitForChild("Torso")
local playerPosition = torso.Position

local default_CameraPosition = torso.Position
local default_CameraRotation = Vector2.new(0,math.rad(-60))
local default_CameraZoom = 15

local cameraPosition = default_CameraPosition
local cameraRotation = default_CameraRotation
local cameraZoom = default_CameraZoom

local cameraRotationBounds = {math.rad(-81),math.rad(20)}
local cameraZoomBounds = nil --{10,200}
local touchDragSpeed = 0.15
local cameraSpeed = 0.1
local cameraRotateSpeed = 10
local cameraMouseRotateSpeed = 0.25
local cameraTouchRotateSpeed = 10
-- ========================================
-- ========================================





-- ========================================
-- UTILITY FUNCTIONS
-- ========================================
local function SetCameraMode()
camera.CameraType = "Scriptable"
camera.FieldOfView = 80
camera.CameraSubject = nil
end

local function UpdateCamera()
SetCameraMode()
local cameraRotationCFrame = CFrame.Angles(0, cameraRotation.X, 0)*CFrame.Angles(cameraRotation.Y, 0, 0)
camera.CFrame = cameraRotationCFrame + cameraPosition + cameraRotationCFrame*Vector3.new(0, 0, cameraZoom)
camera.Focus = camera.CFrame - Vector3.new(0, camera.CFrame.p.Y, 0)
end
-- ========================================
-- ========================================




-- ========================================
-- MOBILE CAMERA EVENTS
-- ========================================
-- Events used to control the camera for players using a mobile device

-- ====================
-- CAMERA MOVE
-- ====================
-- Fired by UserInputService.TouchPan
local lastTouchTranslation = nil
local function TouchMove(touchPositions, totalTranslation, velocity, state)
	if state == Enum.UserInputState.Change or state == Enum.UserInputState.End then
		local difference = totalTranslation - lastTouchTranslation
	cameraPosition = cameraPosition + Vector3.new(difference.X, 0, difference.Y)
		UpdateCamera()
	end
	lastTouchTranslation = totalTranslation
end

-- ====================
-- CAMERA ROTATE
-- ====================
-- Fired by UserInputService.TouchRotate
local lastTouchRotation = nil
local function TouchRotate(touchPositions, rotation, velocity, state)
if state == Enum.UserInputState.Change or state == Enum.UserInputState.End then
local difference = rotation - lastTouchRotation
cameraRotation = cameraRotation + Vector2.new(-difference,0)*math.rad(cameraTouchRotateSpeed*cameraRotateSpeed)
		UpdateCamera()
	end
lastTouchRotation = rotation
end

-- ====================
-- CAMERA ZOOM
-- ====================
-- Fired by UserInputService.TouchPinch
local lastTouchScale = nil
local function TouchZoom(touchPositions, scale, velocity, state)
	if state == Enum.UserInputState.Change or state == Enum.UserInputState.End then
		local difference = scale - lastTouchScale
		cameraZoom = cameraZoom * (1 + difference)
		if cameraZoomBounds ~= nil then
			cameraZoom = math.min(math.max(cameraZoom, cameraZoomBounds[1]), cameraZoomBounds[2])
		else
			cameraZoom = math.max(cameraZoom, 0)
end
		UpdateCamera()
	end
	lastTouchScale = scale
end

local function Input()
	UpdateCamera()
end
-- ========================================





-- ========================================
-- NON-MOBILE CAMERA EVENTS
-- ========================================

local function Input(inputObject)
	if inputObject.UserInputType == Enum.UserInputType.Keyboard then
		if inputObject.UserInputState == Enum.UserInputState.Begin then						
			-- ====================
			-- CAMERA ZOOM
			-- ====================
			-- (I) Zoom In
			if inputObject.KeyCode == Enum.KeyCode.I then
			 	cameraZoom = cameraZoom - 15
			elseif inputObject.KeyCode == Enum.KeyCode.O then
				cameraZoom = cameraZoom + 15
			end
		
			-- (O) Zoom Out
			if cameraZoomBounds ~= nil then
				cameraZoom = math.min(math.max(cameraZoom, cameraZoomBounds[1]), cameraZoomBounds[2])
			else
				cameraZoom = math.max(cameraZoom, 0)
			end
			
			UpdateCamera()
		end
	end
	
	-- ====================
	-- CAMERA ROTATE
	-- ====================
	local pressed = UserInputService:IsMouseButtonPressed(Enum.UserInputType.MouseButton1)
	if pressed then
		UserInputService.MouseBehavior = Enum.MouseBehavior.LockCurrentPosition
		local rotation = userInputService:GetMouseDelta()
		cameraRotation = cameraRotation + rotation*math.rad(cameraMouseRotateSpeed)
	else
		UserInputService.MouseBehavior = Enum.MouseBehavior.Default
	end
end

-- ====================
-- CAMERA MOVE
-- ====================
local function PlayerChanged()
	local movement = torso.Position - playerPosition
	cameraPosition = cameraPosition + movement
	playerPosition = torso.Position
		
	UpdateCamera()
end
-- ========================================
-- ========================================






-- ========================================
-- DEVICE CHECK
-- ========================================
-- Determine whether the user is on a mobile device

if UserInputService.TouchEnabled then
	-- The user is on a mobile device, use Touch events
UserInputService.TouchPan:Connect(TouchMove)
UserInputService.TouchRotate:Connect(TouchRotate)
UserInputService.TouchPinch:Connect(TouchZoom)
else
	
	-- The user is not on a mobile device use Input events
UserInputService.InputBegan:Connect(Input)
UserInputService.InputChanged:Connect(Input)
UserInputService.InputEnded:Connect(Input)

	-- Camera controlled by player movement
	wait(2)
	RunService:BindToRenderStep("PlayerChanged", Enum.RenderPriority.Camera.Value-1, PlayerChanged)
end	
-- ========================================
-- ========================================