-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)
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.