LastInputTypeChanged not working on mobile device

I’m using UIS.LastInputTypeChanged event to detect if a user is on mobile or laptop for some reason it works when I use the mobile screen on my roblox studio however when I tried playing the game on my phone it didn’t work at all.

InputController.Type = nil
InputController.TouchTable = {
	Enum.UserInputType.Touch,
	Enum.UserInputType.Accelerometer,
	Enum.UserInputType.Gyro
}
InputController.KeyboardTable = {
	Enum.UserInputType.MouseButton1,
	Enum.UserInputType.MouseButton2,
	Enum.UserInputType.MouseButton3,
	Enum.UserInputType.Keyboard,
	Enum.UserInputType.MouseMovement,
	Enum.UserInputType.MouseWheel
}
--Main
function InputController:CreateInput(Func, Key)
	local UtilityController = Knit.GetController("UtilityController")
	
	UtilityController:Wrap(function()
		repeat
			task.wait()
		until self.Type ~= nil--Pause until InputType is determined
		
		if self.Type == Enum.UserInputType.Touch then
			print("Mobile")
			UIS.TouchTap:Connect(function(TapPos, GameProcessedEvent)
				if GameProcessedEvent then return end
				Func()
			end)
		else
			print("Laptop")
			UIS.InputBegan:Connect(function(Input, GameProcessedEvent)
				if GameProcessedEvent then return end
				
				if Input.KeyCode == Key or Input.UserInputType == Key then
					Func()
				end
			end)
		end
	end)
end

function InputController:KnitInit()
	print("InputController Initialized")
end

function InputController:KnitStart()
	UIS.LastInputTypeChanged:Connect(function(LastInputType)
		if table.find(self.TouchTable, LastInputType) and table.find(self.TouchTable, self.Type) == nil then
			self.Type = Enum.UserInputType.Touch
		elseif table.find(self.KeyboardTable, LastInputType) and table.find(self.KeyboardTable, self.Type) == nil then
			self.Type = Enum.UserInputType.Keyboard
		end
	end)
end
2 Likes

I know this is a bit old, but I just ran into this problem and found the solution. Might help still or might not.

My alt approach:

userInputService.InputBegan:Connect(function(input)
	print("InputType Test: "..inputTypeThePlayerIsUsing)
	
	if input.UserInputType == Enum.UserInputType.Keyboard then
		print("Player using Keyboard!")
	end

    if input.UserInputType == Enum.UserInputType.Touch then
		print("Player using Touchscreen/Mobile device!")
	end

end)

It’s working for my game, so that’s great.

1 Like

I’m also late but I keep seeing people use “LastInputTypeChanged” for detecting devices when it doesn’t make sense as to why.

Using the regular input began event works aswell, I used to think I was doing something wrong by just using input began because other people were using the other event to detect devices. I’m glad someone else does the same like I do.

They both basically do the same thing, I would say using input began is better because it doesn’t just return the input type. So you can do more with it.

1 Like