Detect when player presses enter in a textbox (plugin)

Hello! So I am making a In-Studio Script executor plugin and am trying to implement Auto indentation but am unsure how. I have tried stuff like this:

CodeBox.InputBegan:Connect(function(input)
	
	if input.UserInputType == Enum.UserInputType.Keyboard then
		
		if input.KeyCode == Enum.KeyCode.Return then
			
			print("Testing")
			
		end
		
	end
	
end)

But it doesn’t seem to work. I’ve looked this up many times and wasn’t able to find anything.

Thanks for the help and sorry for the trouble!

1 Like

Possible duplicated of Detect when enter key pressed while textbox focused

But anyways, you’ll most likely want to use the TextBox FocusLost event.

More information here: TextBox.FocusLost

2 Likes

Just use the textbox’s ‘FocusLost’ event/signal and check its ‘enterPressed’ parameter.

textBox.FocusLost:Connect(function(enter, inputObject)
	--Do code.
end)
4 Likes

I thought of FocusLost except I don’t want it to exit the textbox, just add an indent like it would in the script editor. Thanks for the suggestion though!

The ‘Return’ key automatically releases the textbox’s focus, here’s a script to achieve what you’re trying to achieve though.

local script = script
local textBox = script.Parent
textBox.ClearTextOnFocus = false --This is necessary (can be done in the properties window).

local function onTextBoxFocusLost(enterPressed, inputObject)
	if enterPressed then
		textBox.Text..="\n"
		textBox:CaptureFocus()
	end
end

textBox.FocusLost:Connect(onTextBoxFocusLost)
1 Like

That partially worked. Except for some reason it only did it when I pressed backspace and when I would press backspace again it would go back one line with one less space. But I think this method could work. Thanks!

That’s weird, are you using a non-US standard keyboard?

https://developer.roblox.com/en-us/api-reference/property/TextBox/MultiLine
I believe this is the property you were referring to.

I’m using a normal QWERT laptop keyboard