Ad
Still have questions? Join our Discord server and get real time help.
Log in to vote
1

What's the difference between KeyDown and InputBegan?

Asked by 11 months ago

I had noticed that "KeyDown" is deprecated and I was wondering why, because I use it for a lot of things. Is there really a difference between keydown and inputbegan?

3 answers

Log in to vote
1
Answered by 11 months ago
Edited 9 months ago

Hello, JojoMalRBX!

Welcome to the UserInputService tutorial! In this tutorial, you will be learning about this wonderful service and how it’s better to use its InputBegan event rather than the Mouse.KeyDown event.

This service is a very convenient way to get user input from players. It gets input from phones, tablets, computers, and Xbox console! The KeyDown event can’t do it. Its InputBegan event can run when a user doesn’t chat, unlike KeyDown. The KeyDown event runs regardless of a player chatting.

You can also use the ContextActionService to key bind actions to keys and get user input, but UserInputService can do something similar and we are not focusing on Context.

This script below will make a part pressing the 'Q' key.

--LocalScript

local UserInputService = game:GetService"UserInputService"

UserInputService.InputBegan:Connect(function(input, processed)
    if not processed then -- if not chatting then
        if input.KeyCode == Enum.KeyCode.Q then --  if the key is q
            local part = Instance.new("Part")
            part.Parent = workspace 
        end
    end
end)

What I like is that on line 7, you have to check the input code using an Enum. When you check it, the Enum.KeyCode.[KEY] will show a list of keys, and you can find any key! KeyDown can’t get keys like this.

Ad
Log in to vote
5
Answered by
RubenKan 3436 Moderation Voter Administrator Community Moderator
11 months ago
Edited 11 months ago

It's deprecated for many reasons, one is that it's extremely unreliable as you can't know if roblox did anything with the event, like typing in a TextBox. InputBegan includes an event for ANY type of input, may it be mouse, keyboard, controller, etc.

I'd suggest switching to InputBegan and InputEnded, simply because it's better than KeyUp and KeyDown, and those MIGHT be removed in the future.

2
An InputObject has much more information rather than just what key was pressed. kingdom5 5292 — 11mo
Log in to vote
-1
Answered by 11 months ago

UserInputService is more advanced

3
This should be a comment. RubenKan 3436 — 11mo

Answer this question