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

Userinput service not detecting return being pressed?

-This is a local script

-wont even print hi when enter is pressed

mouse.KeyDown:Connect(function(key)
	if key == Enum.KeyCode.Return then
		print("hi")
		if bar:IsFocused() then
			print("wasfocused")
		else
			print("no")
		end
	end
end)

6 comments

0

Don't forget to accept my answer if it helps out.

0

Actually mine:)

0

forget feahren answer pls ya

0

I was here first, and you copied me

0

let’s not argue about this:)

0

was typing first bby

2 answers

✓ Accepted answer

KeyDown of mouse was deprecated. This also isn't UserInputService. If it were, it would've looked like this

local UserInputService = game:GetService("UserInputService"

UserInputService.InputBegan:Connect(function(InputObject)
	if (InputObject.KeyCode == Enum.KeyCode.Return) then
		if not (bar:IsFocused()) then
			print("was focused")
		else
			print("wasn't focused")
		end
	end
end)

This Service is quite useful though in most cases and can do much more than just Keyboard Input. Consider reading about it more!

Side note: If you're trying to make an Event fire off of TextBox's Focus being lost, simply use the FocusLost Boolean

bar.FocusLost:Connect(function(Return)
	if (Return) then
		print("TextBox's Focus was lost.")
	end
end)

It provides an argument that'll allow you to check whether Return was pressed too

You're using mouse.KeyDown. They key is a string. Not an enumeration. You shouldn't be using mouse.KeyDown anyway; it's deprecated. You would also have to use key:byte() to check for non-alphanumeric characters.

Use UserInputService

UserInputService.InputBegan:Connect(function(input, gpe)
    if input.KeyCode == Enum.KeyCode.Return then
        -- # ...
    end
end)

Since you are using a TextBox you can use the FocusLost event.

box.FocusLost:Connect(function(enterPressed)
    print(enterPressed and "wasfocused" or "no")
end)

enterPressed is a boolean that is true if the reason for focus lost was because you pressed the enter/return key.

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