On key press, focus a Textbot

Hi there, I was wondering if it was possible at all to toggle a textbox/focus it upon a key being pressed. I’ve looked around on the Documentation and Google but cant find any leads.

Any and all help is greatly appreciated

Are you asking for a way to make a text box a text box? can you elaborate…

sorry if you may not have understood. here is what im trying to accomplish. i have a custom messaging system.

i need it to be able to focus the textbox (so its got the flashing cursor meaning its ready to be used) via them pressing the “/” key.

hope this made more sense.

I think they mean that when you press a key (like a), it puts you into the textbox. So like for example it would put you into a search bar.

exactly that, but instead of a search bar a message bar

There is a UI object for that, called TextBox, and you can use these events to help you

Focused
FocusLost

yes im aware, i dont need to be taught what a textbox is since i already have it setup.
all i need to know is if its possible for the user to press a key, such as “/” and then it will TOGGLE/FOCUS the text box

Just googled it. You can use CaptureFocus() I think. So like:

script.Parent.TextBox:CaptureFocus()

(In localscript btw)

Documentation: TextBox | Documentation - Roblox Creator Hub

1 Like

Ohh so like the chat box, where if you press / it opens it. Gotcha

yeah, sorry if it didnt make much sense

will try this, didnt see it when i scoured the documentation for it

You can also use :ReleaseFocus() to well, unfocus.

Yes

Here’s an example from the docs:

local ContextActionService = game:GetService("ContextActionService")

local ACTION_NAME = "FocusTheTextBox"

local textBox = script.Parent --textbox

local function handleAction(actionName, inputState, _inputObject) --cas function
	if actionName == ACTION_NAME and inputState == Enum.UserInputState.Begin then
		textBox:CaptureFocus() -- focuses
	end
end

ContextActionService:BindAction(ACTION_NAME, handleAction, false, Enum.KeyCode.Q)

can confirm :CaptureFocus() worked.
however when its pressed, it also copies into the textbox.
image

You can just delete the last character whenever you focus on the textbox so like:

textbox.Text = string.sub(textbox.Text, 0, string.len(textbox.Text)-1)

will go and test iţ now thanks

didnt work, tried it in both PlayerGui and StarterGui versions

Can I see your code? Are there any errors?

Also wdym playergui version loll.

no errors

uis.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.Slash then
		script.Parent:CaptureFocus()
		script.Parent.Text = string.sub(script.Parent.Text, 0, string.len(script.Parent.Text)-1)
	end
end)

What happens when you press slash?