CaptureFocus on KeyPress without adding in text

Keyboard.KeyDown:Connect(function(code)
	if (code == Enum.KeyCode.Slash) then
		TextField:CaptureFocus()
	end
end)

When I press the forward slash key to gain focus, it adds a forward slash onto the empty text. How would I stop this from happening?

2 Likes

Couldn’t you just set the text to an empty string when the forward slash key is pressed? It can’t be that complicated.

Keyboard.KeyDown:Connect(function(code)
	if (code == Enum.KeyCode.Slash and not TextField:IsFocused()) then
		TextField:CaptureFocus()
		TextField.Text = ""
	end
end)

Ah, yes. I wish it wasn’t. However, this does not work. It may have to do with TextBoxes getting updated at the end of the frame, after the text gets set to an empty string. I have no idea. I tried to set the text to an empty string in a spawn function since that takes place the next frame, but that did not work either.

Try using KeyUp instead of KeyDown.

7 Likes

Thank you. I can be really stupid sometimes.

1 Like