Determining Preferred Input Method: Modernized

Many roblox games today determine the method of input for UI applications using UserInputService.TouchEnabled. On the surface, it seems correct: it is true that most devices for which this value is true are mobile, but it is not complete. Consider an edge case: a player with a hyrbid device, for instance, a laptop with touchscreen, will be incorrectly labelled as mobile if TouchEnabled alone is used.

I write this today because my primary device is a laptop with touchscreen, and though it is true that I can (and often do) temporarily disable my touchscreen to circumvent the issues with TouchEnabled loading mobile UI, it is impractical and frankly tedious. Our job as developers is to consider the quality of life of all players, and though the hyrbid device community is small, we exist, and we play your games.

Roblox offers a modern, easy to implement method for reliably determining preferred input. It is UserInputService.PreferredInput, and should be used in place of outdated TouchEnabled checks.

Let’s take a look at some code samples:

What NOT to do:

local UserInputService = game:GetService("UserInputService")
local function isMobile()
 if UserInputService.TouchEnabled then
  return true
 end
 return false
end
isMobile()

What TO do:

local UserInputService = game:GetService("UserInputService")
local preferredInput: Enum.PreferredInput = UserInputService.PreferredInput
warn("INPUT TYPE:", preferredInput)

Here are the results from in-studio emulations that demonstrate that this works for correctly determining the preferred input method of hybrid devices. My touchscreen was enabled during this test:

Using my regular laptop with no device emulation. Touchscreen was enabled.
image

Using in-studio mobile device emulator
image

Using in-studio gamepad device emulator

As you can see, this approach is very simple, and would extend the reach and quality of your game. I apologize if the tone of this forum post comes off as hostile, but it becomes frustrating when something like this is ignored by developers for this long.

Have a great day, and if you have questions, comments, or concerns, please leave them below!

See replies for additional notes

7 Likes

I don’t really deal with cross platform programming but I just also checked the post for PreferredInput just now and its actually very useful, good thing to keep in mind. Thanks!

2 Likes

No worries! I’m glad you found it. Roblox’s docs says the exact thing I’m saying here as well.

1 Like

Hello readers,

After writing this, I realized that some games may want hybrid devices to be able to switch between input modes seamlessly during runtime. Roblox’s UserInputService:GetLastInputType() method and UserInputService.LastInputTypeChanged event both did not work as intended on hyrbid devices, so I have spent some time writing this manual method to switch between modes:

local UserInputService = game:GetService("UserInputService")

-- We will use this variable to store changes
local lastInputType: Enum.UserInputType = nil

-- Many programs already listen for InputBegan, so we will simply add to an existing set of instructions
-- for when the input begins.
UserInputService.InputBegan:Connect(function(input: InputObject, gameProcessedEvent: boolean)
	if gameProcessedEvent then return end
    -- If this is the first time this function is running, we should set a value.
	if not lastInputType then lastInputType = input.UserInputType end
	
	-- First ask if this check is necessary, we do not want to run this for no reason.
	-- The condition for a hybrid device is keyboard & mouse + touch.
	if UserInputService.PreferredInput == Enum.PreferredInput.KeyboardAndMouse and UserInputService.TouchEnabled then
		-- If the type of input (input.UserInputType) we're receiving is different than what we have stored most recently,
		-- then we are detecting a change.
		if input.UserInputType ~= lastInputType then
			-- Touch
			if input.UserInputType == Enum.UserInputType.Touch then
				-- Debug:
				warn("INPUT CHANGED TO:", input.UserInputType, " | SWITCHING TO MOBILE MODE")
				-- Swap to mobile UI here.
			-- Keyboard or Mouse
			elseif input.UserInputType == Enum.UserInputType.Keyboard or input.UserInputType == Enum.UserInputType.MouseMovement or input.UserInputType == Enum.UserInputType.MouseButton1 or input.UserInputType == Enum.UserInputType.MouseButton2 or input.UserInputType == Enum.UserInputType.MouseButton3 or input.UserInputType == Enum.UserInputType.MouseWheel then
				-- It would be redundant if we let this block run every time a keyboard / mouse input was made.
				-- Instead, we ask if the last input was touch or not, so that the PC mode switch is guaranteed
				-- to only occur once.
				if lastInputType == Enum.UserInputType.Touch then
					-- Debug:
					warn("INPUT CHANGED TO:", input.UserInputType, " | SWITCHING TO PC MODE")
					-- Swap to PC UI here.
				end
			end
			-- Set the new value for the next time we check for a change.
			lastInputType = input.UserInputType
		end
	end
end)

I’m sure there are more elegant ways to approach this problem, but this is what I have come up with. It is very important to note that these switches cannot be observed on Roblox Studio due to a small bug I found. You should test this on the actual Roblox client. I will be writing the bug report shortly after posting this to try and get it resolved. Edit: You can now find my bug report here.

It is similarly important to note that this solution is focusing on hybrid devices, but the general idea of tracking input changes should work for a swap between any set of input types.

Thank you for reading, here’s a video demonstration I have taken:

Have a lovely day! :grin:

3 Likes