New: Nitro Boost our Discord server and receive full donation perks here on the website! Join the Scripting Helpers Discord Server to learn more! You can also Support on Patreon as always.
Still have questions? Join our Discord server and get real time help.
Log in to vote

What's the difference between KeyDown and InputBegan?

Asked by 1 year 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
Answered by 1 year ago
Edited 1 year 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.


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 ="Part")
            part.Parent = workspace 

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.

Log in to vote
Answered by
RubenKan 3570 Moderation Voter Administrator Community Moderator
1 year ago
Edited 1 year 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.

An InputObject has much more information rather than just what key was pressed. kingdom5 5726 — 1y

Answer this question