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

InputChanged

This event fires when a user changes how they’re interacting via a Human-Computer Interface device (Mouse button down, touch begin, keyboard button down, etc).

The UserInputService has a similarly named event that is not restricted to a specific UI element: UserInputService/InputChanged.

This event will always fire regardless of game state.

See also

  • GuiObject/InputBegan
  • GuiObject/InputEnded

Parameters

Name Type Default Description

An InputObject, which contains useful data for querying user input such as theEnum/UserInputType|type of input, Enum/UserInputState|state of input, and Enum/Position|screen coordinates of the input


Code Samples


GuiObject InputChanged Demo

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

In order for this to work as expected, it must be placed in a LocalScript and a child of gui.

-- In order to use the InputChanged event, you must specify a GuiObject
local gui = script.Parent

-- 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)
	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

gui.InputChanged:Connect(inputChanged)