Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How to get if player clicked the enter key?

I want to find out when the player clicks the enter key after typing, this is what I have:

UIS.InputBegan:Connect(function(input, gameProcessed)
	print("Key Pressed after text wrote.")
	if input.KeyCode == Enum.KeyCode.KeypadEnter then
		print("Enter key pressed.")
		game.ReplicatedStorage.LightChange:FireServer(currentText)
		print("Event fired.")
	end
end)

Any help would be appreciated

1 answer

✓ Accepted answer

Use UserInputService's GameProcessed parameter. It indicates whether the game engine has internally observed the same InputObject and acted on it. This predominantly refers to UI processing, one of which will activate GameProcessed if typing in the default chat.

To ensure we're recognizing that they're typing in chat, we should filter the input processing for only keyboard InputObjects.

local UserInputService = game:GetService("UserInputService")

UserInputService.InputBegan:Connect(function(InputObject, GameProcessed)
	if (InputObject.UserInputType = Enum.UserInputType.Keyboard) then
		if (GameProcessed) then
			if (InputObject.KeyCode == Enum.KeyCode.Return) then
				--// Action
			end
		end
	end
end)

If you wish to act upon someone typing within a TextBox the code below will produce the same effect!

TextBox.Focused:Connect(function()
	print("Someone is typing!")
end)

TextBox.FocusLost:Connect(function(ReturnPressed)
	if (ReturnPressed) then
		print("Someone stopped typing!")
	end
end)

If this helps, don't forget to accept this answer!

This discussion is preserved as it was. New answers, comments, and votes are closed.