Detect key tap on mobile keyboard

Is there any way at all to detect when a user is typing on a mobile device?

I’m working on a system where every time a player types a character in the chat bar, it plays a typewriter sound. This system is working flawlessly on desktop however I’m struggling to get it to work on mobile. I looked through documentations and found no way of detecting when a player is using their mobile keyboard.

I noticed during my testing that the event UserInputService.TouchStarted does not fire when the player is using the keyboard. This is preventing me to gauge if the player is touching the screen while ChatInputBarConfiguration.IsFocused is true .

This is what the intended result should be:

-- Desktop detection
UserInputService.InputBegan:Connect(function(input, gameProcessed)
	local keyCode, inputType = input.KeyCode, input.UserInputType

	if inputType == Enum.UserInputType.Keyboard then
		if gameProcessed and chatConfig.IsFocused then
			if not typing then
				-- Ensure the key has a character input to avoid misleading key presses
				if table.find(blacklistedKeys, keyCode) == nil then
					typing = true
					RemoteEvent:FireServer(true) -- Communicate input has started to the server
				end
			end

			-- Play type writer sound
			local newSound = Replicators.KeystrokeSound(false)
			newSound.Parent = SoundService
			newSound.Volume *= 0.25
			newSound:Play()

			if table.find(blacklistedKeys, keyCode) == nil then
				keystroke:FireServer(keyCode == Enum.KeyCode.Space) -- Communicate key input to the server
				lastKeyPressTime = tick()
			end
		end
	end
end)
1 Like

I fear that there might not be any solution other than making your own Chat UI and connecting it to TextChatService, or completely making a new chat system and tracking the TextBox’s text changing.

1 Like

I feared this would be the case. I may just give a general “Player is typing” signal which I was able to achieve like this:

-- Detect when a player is typing on a mobile device
if UserInputService.TouchEnabled then
	chatConfig:GetPropertyChangedSignal('IsFocused'):Connect(function()
		RemoteEvent:FireServer(chatConfig.IsFocused)
	end)
end

It’s not exactly what I was going for but it’s an acceptable compromise for mobile clients.

1 Like

Yeah that sounds fine, maybe you could also make it so it just plays a typing sound while the chatConfig is focused. Of course it would probably be weird when it’s just playing even if the player is not typing while that chatbar is focused.

1 Like

Another way to achieve this would be to make your own custom Chat. You’d be able to see what is inputted in the input box