Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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

✓ 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
		--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.

2 comments

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

0

I thought the conditional was supposed to be "key.KeyCode == Enum.KeyCode.LeftAlt", since the argument "key" is an InputObject.

This discussion is preserved as it was. New answers, comments, and votes are closed.