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

How to get if player clicked the enter key?

Asked by 4 years ago

I want to find out when the player clicks the enter key after typing, this is what I have:

UIS.InputBegan:Connect(function(input, gameProcessed)
    print("Key Pressed after text wrote.")
    if input.KeyCode == Enum.KeyCode.KeypadEnter then
        print("Enter key pressed.")
        game.ReplicatedStorage.LightChange:FireServer(currentText)
        print("Event fired.")
    end
end)

Any help would be appreciated

1 answer

Log in to vote
1
Answered by
Ziffixture 6913 Moderation Voter Community Moderator
4 years ago
Edited 4 years ago

Use UserInputService's GameProcessed parameter. It indicates whether the game engine has internally observed the same InputObject and acted on it. This predominantly refers to UI processing, one of which will activate GameProcessed if typing in the default chat.

To ensure we're recognizing that they're typing in chat, we should filter the input processing for only keyboard InputObjects.

local UserInputService = game:GetService("UserInputService")

UserInputService.InputBegan:Connect(function(InputObject, GameProcessed)
    if (InputObject.UserInputType = Enum.UserInputType.Keyboard) then
        if (GameProcessed) then
            if (InputObject.KeyCode == Enum.KeyCode.Return) then
                --// Action
            end
        end
    end
end)

If you wish to act upon someone typing within a TextBox the code below will produce the same effect!

TextBox.Focused:Connect(function()
    print("Someone is typing!")
end)

TextBox.FocusLost:Connect(function(ReturnPressed)
    if (ReturnPressed) then
        print("Someone stopped typing!")
    end
end)

If this helps, don't forget to accept this answer!

Ad

Answer this question