Carriage return character is added to textboxes when their focus is captured on FocusLost when enter is pressed

When capturing the focus of a textbox after its focus was lost via pressing enter, a carriage return character (\r) is added to the text.

That is, if you have the following code inside of a TextBox has ClearTextOnFocus set to false, focus it, and press enter, it’ll add a carriage return to the end of the text.

script.Parent.FocusLost:Connect(function(enterPressed)
  if enterPressed then
    script.Parent:CaptureFocus()
  end
end)

If you add even the smallest yield before capturing the focus, this doesn’t happen:

script.Parent.FocusLost:Connect(function(enterPressed)
  if enterPressed then
    game:GetService("RunService").RenderStepped:Wait()
    script.Parent:CaptureFocus()
  end
end)

I’m not entirely sure if this counts as a “bug” per se since it’s a problem with having no frames where the textbox isn’t focused, but it’s definitely unexpected behavior, as this character shouldn’t be showing up in the textbox and it’s not immediately apparent what’s causing it.

5 Likes

Yeah, it shouldn’t be doing this. We’ll look into fixing this.

3 Likes

The same thing happens if you use UserInputService to capture focus when a key is pressed:

inputService.InputBegan:Connect(function(inputObject)
    if inputObject.KeyCode == Enum.KeyCode.Slash then
        textBox:CaptureFocus()
    end
end)

By reading through the code above, you would assume it would behave the same as the default chat (pressing / focuses the textbox), but it also adds a / symbol to the input text field. This can be fixed by yielding as well.

1 Like

I am still getting this bug. Is there any progress on the fix?

1 Like

Hello, can we get an update on this?

Hey @Moduluous - we still have this in our backlog of bugs for textboxes and are making our way through our backlog of DevForum bugs. Hopefully we can get this resolved soon, although we can’t provide an ETA just yet.

1 Like