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

Keyboard input detection for arrow keys?

When detecting keyboard inputs i usually use scripts similar to the small example below:

function PressQ(key)
	if key == "q"
		then...
end


 
mouse.KeyDown:connect(PressQ)

but I'm wondering what I should replace the "q" on the second line with to detect the down arrow being pressed? I've only ever used letters...

Thank you for your time! :)

1 answer

✓ Accepted answer

KeyDown is deprecated and will no longer work, we now have to use userinputservice.

(Userinputservice is client only!)

local UserInputService = game:GetService('UserInputService')

function onInputBegan(input,gameProcessed)
	if input.KeyCode == Enum.KeyCode.Up then
		-- Up arrow 
	end
end

UserInputService.InputBegan:connect(onInputBegan)

You can see I use Enum for the keycode, you can find the keycodes here

But these are the ones you need

Enum.KeyCode.Up --> Up
Enum.KeyCode.Down --> Down
Enum.KeyCode.Left --> Left
Enum.KeyCode.Right --> Right

To do the normal stuff (letters) you can simply do this

Enum.KeyCode.A --> A
Enum.KeyCode.B --> B
Enum.KeyCode.C --> C

There are more things you can do with it, but these are the things you need.

If you have a question, just ask :)

2 comments

0

Perfect, exactly what I was looking for thank you! The link to the other KeyCodes was also very useful, much appreciated! :)

0

Np :)

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