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

What's the difference between KeyDown and InputBegan?

Asked by 6 years 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?

2 answers

Log in to vote
1
Answered by 6 years ago
Edited 6 years 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 3615 Moderation Voter Administrator Community Moderator
6 years ago
Edited 6 years 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. User#5423 17 — 6y

Answer this question