Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Userinput service not detecting return being pressed?

Asked by
Plieax 66
5 years ago

-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)
0
Don't forget to accept my answer if it helps out. User#24403 69 — 5y
0
Actually mine:) Ziffixture 6913 — 5y
0
forget feahren answer pls ya User#24403 69 — 5y
0
I was here first, and you copied me Ziffixture 6913 — 5y
View all comments (2 more)
0
let’s not argue about this:) Ziffixture 6913 — 5y
0
was typing first bby User#24403 69 — 5y

2 answers

Log in to vote
2
Answered by
Ziffixture 6913 Moderation Voter Community Moderator
5 years ago
Edited 5 years ago

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

Ad
Log in to vote
1
Answered by 5 years ago

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.

Answer this question