✓ Accepted answer
Since KeyDown doesn't detect most keys(other than alphas) on a keyboard, and KeyUp isn't exactly the most secure, I encourage you to use UserInputService with the InputBegan method.
The InputBegan method will allow you for better response, easier manipulation, and overall improved readability for your code.
So, if you looked at the link you may be asking, how do I detect if the Input was a key? since the InputBegan method returns an instance, rather than a string or byte code. You have to compare the returned statement with a KeyCode Enum.
So, we want to detect the Alt key?
game:GetService('UserInputService').InputBegan:connect(function(key)
if key == Enum.KeyCode.LeftAlt then
end
end)
Additionally, KeyCodes provide easy access to any key on the keyboard. And you don't have to look up some fancy chart on what the enum value would be.. want the right shift? Enum.KeyCode.RightShift, want A? Enum.KeyCode.A It's much easier to work with than KeyDown or KeyUp and referring to a byte table for character interpretations.
2 comments
Alright thanks! Also, do I need to supplement the InputBegan with a InputEnded as you would with a KeyDown and a KeyUp? Also in what instance could I use the InputChanged? Would the InputChanged be useful if I wanted a person to press left alt to, say, pull their arm back to prepare to punch then use input changed to right alt would then cause the character to push their arm forward to punch; but
I thought the conditional was supposed to be "key.KeyCode == Enum.KeyCode.LeftAlt", since the argument "key" is an InputObject.