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

Keyboard input detection for arrow keys?

Asked by 6 years ago

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

1function PressQ(key)
2    if key == "q"
3        then...
4end
5 
6 
7 
8mouse.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

Log in to vote
1
Answered by 6 years ago
Edited 6 years ago

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

(Userinputservice is client only!)

1local UserInputService = game:GetService('UserInputService')
2 
3function onInputBegan(input,gameProcessed)
4    if input.KeyCode == Enum.KeyCode.Up then
5        -- Up arrow
6    end
7end
8 
9UserInputService.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

1Enum.KeyCode.Up --> Up
2Enum.KeyCode.Down --> Down
3Enum.KeyCode.Left --> Left
4Enum.KeyCode.Right --> Right

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

1Enum.KeyCode.A --> A
2Enum.KeyCode.B --> B
3Enum.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 :)

0
Perfect, exactly what I was looking for thank you! The link to the other KeyCodes was also very useful, much appreciated! :) AlexTheCreator 461 — 6y
0
Np :) User#20388 0 — 6y
Ad

Answer this question