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

Can I use the "Alt" key as an action key?

Asked by 9 years ago

Is it possible to use the "alt" key to act as an action key in an if function? Basically can I program this macro to be do something in my game and if I can, how would you call the key to be used? Would it just go as follows?

if key == "alt" then
end

1 answer

Log in to vote
2
Answered by
Goulstem 8144 Badge of Merit Moderation Voter Administrator Community Moderator
9 years ago

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
        --Your Code Here
    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.

0
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 Ghost4Man 25 — 9y
0
I thought the conditional was supposed to be "key.KeyCode == Enum.KeyCode.LeftAlt", since the argument "key" is an InputObject. Redbullusa 1580 — 9y
Ad

Answer this question