How to detect when player is typing?

So in some games when a player is typing a little bubble chat appears at the top of the head.
And im searching how to replicate this.
The best i tried is searching on YT, API reference, and it says nothing at all.

So how do i detect when a player is typing?

6 Likes

You need to use the InputBegan Event of UserInputService.

  • Check whether the input is a key , if it is you check the second argument returned by the event , GameProcessedEvent which is a boolean.

If it’s true then the player is typing.

7 Likes

Could you provide a code example?, Oh and no im not copying the code example.

-- Client
local UIS = game:GetService("UserInputService")
UIS.InputBegan:Connect(function(_, typing)
   if typing then
      print("Typing")
   end
end)
3 Likes

Anyone know why the first letter that’s typed in the textbox isn’t detected with this code ^

This should work it detects the moment the player selected the text field. Use this in a local script of course.

local userInputService = game:GetService("UserInputService")
local plr = game.Players.LocalPlayer
local Chat = plr.PlayerGui:WaitForChild("Chat")
local ChatBox = Chat.Frame.ChatBarParentFrame.Frame.BoxFrame.Frame:WaitForChild("ChatBar")

userInputService.InputBegan:Connect(function()
	local focus = ChatBox:IsFocused()
	if focus == true then
		--The code you want to run!
	end
end)
2 Likes
local Players = game:GetService("Players")
local UserInputService = game:GetService("UserInputService")

local function onInputBegan(input, gameProcessedEvent)
    if gameProcessedEvent then
        return
    end

    if input.UserInputType == Enum.UserInputType.Keyboard and input.KeyCode == Enum.KeyCode.Unknown and input.UserInputState == Enum.UserInputState.Begin then
        local player = Players:GetPlayerFromCharacter(input.UserInputType)
        if player then
            print(player.Name .. " is typing")
        end
    end
end

UserInputService.InputBegan:Connect(onInputBegan)

there are also simple ways, I wouldn’t call this complex. :smiley:

1 Like

Doesn’t that also trigger if you are just walking around on pc?

yea, I’ve made it but didn’t checked abt that lol-

I will get at the pc in js a second

Don’t worry, the script I sent a few minutes ago probably works unless I forgot something it checks both if the player is pressing keys and if the chatbox is selected on the client side.

yup I have made another solution, but if yours is working then I don’t mind.:slight_smile:

1 Like

Is there a way to pull this same similar method with the new TextChatService?

Edit: I ask this because, for any unaware, the new TextChatService stores the new chat UI in CoreGui, which is permission locked and unable to be accessed by scripts. LegacyChatService stores the chat UI in PlayerGui, which isn’t permission locked.

1 Like

Hi Jeebus, Yeah you can do that two by looking if the player is focused on a chatbox. Here is a sample script I’ve just made that checks if the player is focused on a chatbox and if they pressed F of course you could just leave that out depending on what you want to do with it, Hope it helps! :smile:

local userInputService = game:GetService("UserInputService")

userInputService.InputBegan:Connect(function(InputKey)
	local TextBox = userInputService:GetFocusedTextBox() -- gets the focused chatbox
	if not TextBox then -- if no chat box is focused it'll check if the player pressed f and then it prints "F pressed"
		if InputKey.KeyCode == Enum.KeyCode.F then -- you can leave this out depending on what you want to do
			print("F pressed")
		end
	else
		print("FocusedOnChatBox")
	end
end)
3 Likes

That’s awesome! I wasn’t aware of that, thank you so much!