Textbox releases when pressing enter!

I am making a custom chat system, and I want to know how I can stop the textbox from releasing the focus when I press enter. I use userInputService and check if the textbox is in focus, but since it releases when I press Enter, I can never get it to focus correctly.

Maybe add wait before releasing it’s focused?

local UserInputService = game:GetService("UserInputService")

local TextBox = path.to.textbox

UserInputService.InputBegan:Connect(function(input)
   if input.KeyCode ~= Enum.KeyCode.Return then return end
   TextBox:CaptureFocus()
end)

When I press enter, the textbox get’s rid of focus, and then the TextBox:IsFocused() is false. I do not want to capture focus though.

I do not understand the thing you are trying to say. Adding a wait will not change anything.

What do you mean? I just dont understand what do you want.

Why not enable MultiLine? Pressing enter would create a new line instead of losing focus.

Here is the script:

local UserInputService = game:GetService("UserInputService")
local Module = require(game.ReplicatedStorage.CustomChatModule)

UserInputService.InputBegan:Connect(function(Input)
	if Input.KeyCode == Enum.KeyCode.Return then
		if script.Parent.TextBox:IsFocused() then
			if #script.Parent.TextBox.Text < 1 then
				Module.Add(game.Players.LocalPlayer,false,script.Parent.TextBox.Text)
			end
			script.Parent.TextBox:ReleaseFocus()
		end
	end
end)

But the code doesn’t work. When I press Enter, the code doesn’t seem to work. Here is a gyazo gif.
https://i.gyazo.com/9580d1dee7959913914880e35a721434.mp4

local UserInputService = game:GetService("UserInputService")
local Module = require(game.ReplicatedStorage.CustomChatModule)

UserInputService.InputBegan:Connect(function(Input)
	if Input.KeyCode == Enum.KeyCode.Return then
		if script.Parent.TextBox:IsFocused() then
			if #script.Parent.TextBox.Text > 0 then
				Module.Add(game.Players.LocalPlayer,false,script.Parent.TextBox.Text)
			end
			script.Parent.TextBox:ReleaseFocus()
		end
	end
end)

You were checking if text length is less that 1. I think you meant to check if its more than 0?

It still isn’t working for some reason.

Also why are you trying to use the Enter key anyway? This wouldn’t be supported by any other device. You should use the FocusLost event instead.

Doesn’t that also get affected by clicking off the box? I thought that pressing enter on the mobile keyboard works too.

For the mobile I think its better to add a send button, instead of listening to this event, because sometimes you may accidentally click somewhere else and your message will be sent.

Yes

FocusLost returns an InputObject that will tell you how the focus was lost.

2 Likes

If the script below doesn’t work, try the method from @MightyDantheman because it’s the the best thing you can do.

local UserInputService = game:GetService("UserInputService")
local Module = require(game.ReplicatedStorage.CustomChatModule)

UserInputService.InputBegan:Connect(function(Input)
	if Input.KeyCode == Enum.KeyCode.Return then
		if script.Parent.TextBox:IsFocused() then
			if #script.Parent.TextBox.Text < 1 then
				Module.Add(game.Players.LocalPlayer,false,script.Parent.TextBox.Text)
				print("<1")
			elseif #script.Parent.TextBox.Text > 1 then
				print(">1")
			end
			script.Parent.TextBox:ReleaseFocus()
		end
		elseif not script.Parent.TextBox:IsFocused() then
		print("ended")
	end
end)

Ah okay. But how would I know if the user pressed enter/return on their keyboard? And does it work for pc and mobile? (Maybe even xbox too?)

Offtopic, I somehow deleted the menu button…

The very first parameter returned, as shown in my previous reply, is a boolean of whether or not the “Enter” key was used to lose focus or not.

Did it end up working? (must be 30

1 Like